...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
...
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 `...
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...
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...
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...
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...
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...
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, 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...
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
....
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...
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) ?
...
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...
var shell = function (method) {
window[method].apply(null, Array.prototype.slice.call(arguments, 1));
};
shell('alert', 'monkey!');
...
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...
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...
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...
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...
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?
...
I see that in different plugins and codes, but I don't understand what does that function... In the jQuery api isn't referenced!
...