apply

Is R's apply family more than syntactic sugar

...regarding execution time and / or memory. If this is not true, prove it with a code snippet. Note that speedup by vectorization does not count. The speedup most come from *apply (tapply, sapply ...) itself. Thanks in advance ...

Subset a data.frame by list and apply function on each part, by rows

This may seem as a typical plyr problem, but I have something different in mind. Here's the function that I want to optimize (skip the for loop). # dummy data set.seed(1985) lst <- list(a=1:10, b=11:15, c=16:20) m <- matrix(round(runif(200, 1, 7)), 10) m <- as.data.frame(m) dfsub <- function(dt, lst, fun) { # check whether dt is `...

Why is as.factor returns a character when used inside apply ?

I tried doing this: a <- data.frame(x1 = rnorm(100), x2 = sample(c("a","b"), 100, replace = T), x3 = factor(c(rep("a",50) , rep("b",50)))) apply(a2, 2,class) # why is column 3 not a factor ? a a2 <- apply(a, 2,as.factor) apply(a2, 2,class) # why are all columns not factors ? But don't understand why it doesn't have factors... Thank...

Row/column counter in 'apply' functions

What if one wants to apply a functon i.e. to each row of a matrix, but also wants to use as an argument for this function the number of that row. As an example, suppose you wanted to get the n-th root of the numbers in each row of a matrix, where n is the row number. Is there another way (using apply only) than column-binding the row num...

R : remove columns from dataframe where ALL values are NA

hello everybody! I'm having some trouble with my huge data frame and couldn't really resolve that question myself: The dataframe has some properties as columns and each row represents one data set. I've done some sanatizing to this dataframe (e.g. get rid of datasets which are not to be included in evaluation). (Whoever might be interest...

What is the best way to run a loop of regressions in R?

Assume that I have sources of data X and Y that are indexable, say matrices. And I want to run a set of independent regressions and store the result. My initial approach would be results = matrix(nrow=nrow(X), ncol=(2)) for(i in 1:ncol(X)) { matrix[i,] = coefficients(lm(Y[i,] ~ X[i,]) } But, loops are bad, so I could do it wi...

Last Observation Carried Forward In a data frame?

Hi all, I wish to implement a "Last Observation Carried Forward" for a data set I am working on which has missing values at the end of it. Here is a simple code to do it (question after it): LOCF <- function(x) { # Last Observation Carried Forward (for a left to right series) LOCF <- max(which(!is.na(x))) # the location of the...

Custom CSS for a 3rd-party site

Is it possible to apply a custom user CSS over a specific site? Say I created a custom CSS file that I wanted to use while browsing the google.com domain, how would I apply it / add it? I would like to know for Firefox and IE if its possible. ...

In Javascript, is it true that function aliasing works as long as the function being aliased doesn't touch "this"?

In Javascript, if we are aliasing a function (or, assign a "reference to a function" to another variable), such as in: f = g; f = obj.display; obj.f = foo; all the 3 lines above, they will work as long as the function / method on the right hand side doesn't touch this? Since we are passing in all the arguments, the only way it can me...

applying apply() to deeply embeded list elements only

I would like to apply my function to only elements that are deeper in the list structure. For example, I would like to apply a certain function to list elements of second order only. Is this feasible with apply()? > str(l) List of 3 $ :List of 2 ..$ : num 5 ..$ : num 10 $ :List of 2 ..$ : num 15 ..$ : num 20 $ :List of 2 ....

addEventListener using apply()

I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){ return function(){ console.log( 'add something to ' + old.name ); old.apply(this, arguments); } } addEventListener=rewrite(addEventListener); It doesn't work. The code works for normal JavaScript method, for...

Why doesn't (apply or [true false]) work in Clojure?

From what I understand about apply, it unpacks a list and turns the elements into arguments for a function. I see that (apply + [1 2 3]) works as expected, i.e: it's equivalent to (+ 1 2 3). Why then is (apply or [true false]) invalid? Isn't it equivalent to (or true false) ? ...

Algorithm to determine proper divisors

Hi all, I am interested in finding the numbers that exhibit the property of having the sum of their proper divisors equal to the number. The first example is 6, where the proper divisors are 1 + 2 + 3 = 6. I wrote the following code in R, but I feel it is pretty inefficient and can be improved upon significantly. propDivisor <- func...

This works in Chrome but not IE, why?

var shell = function (method) { window[method].apply(null, Array.prototype.slice.call(arguments, 1)); }; shell('alert', 'monkey!'); ...

How can I call a javascript constructor using call or apply?

Hi, how could I generalise the function below to take N arguments? (Using call or apply?) Is there a programmatic way to apply arguments to 'new'? I don't want the constructor to be treated like a plain function. All the best, Chris. /** * This higher level function takes a constructor and arguments * and returns a function, whic...

How to apply patches on the top of a git tree preventing duplication?

Hello everybody, I'm seeking advice for a problem that I thought to be simple, and it might be simple indeed by creating a small script, but I think there should already be a way to do that with git/quilt/stgit. I'm not exactly good at git and this is causing some issues to me. My problem: I've got a git tree (linux kernel) and a numbe...

R: apply over two data.frames

I'm using R, and I have two data.frames, A and B. They both have 6 rows, but A has 25000 columns (genes), and B has 30 columns. I'd like to apply a function with two arguments f(x,y) where x is every column of A and y is every column of B. So far it looks like this: i = 1 for (x in A){ j = 1 for (y in B){ out[i,j] <- f(x...

How to transform a dataframe of characters to the respective dates?

Dear all, I noticed already a couple of times that working with dates doesn't allow for using the usual tricks in R. Say I have a dataframe Data with Dates (see below), and I want to convert the complete dataframe to a date class. The only solution I could come up with until now is : for (i in 1:ncol(Data)){ Data[,i] <- as.Date(Dat...

Run time - using apply functions

I have two apply functions excecuting the average and standard deviation across the first two dimensions on a large three dimentional array (437216,8,3). It takes 16 minutes to complete on Rx32. It's the first of many large arrays in a database we are applying this script on a regular basis. Any thoughts on how to speed up runtime? ...

What's the .apply jQuery function?

I see that in different plugins and codes, but I don't understand what does that function... In the jQuery api isn't referenced! ...