apply

How to apply function over each matrix element's indices

我的梦境 提交于 2019-11-30 10:25:41
问题 I am wondering if there is a built-in function in R which applies a function to each element of the matrix (of course, the function should be computed based on matrix indices). The equivalent would be something like this: matrix_apply <- function(m, f) { m2 <- m for (r in seq(nrow(m2))) for (c in seq(ncol(m2))) m2[[r, c]] <- f(r, c) return(m2) } If there is no such built-in function, what is the best way to initialize a matrix to contain values obtained by computing an arbitrary function

Calculating the maximum of sub-vectors of a larger vector

五迷三道 提交于 2019-11-30 09:38:35
问题 I would like to find the largest element in a portion of a longer vector. I would also like to perform this calculation for multiple 'pieces' of this larger vector. The following code produces the result I am looking for, but it seems inefficient to use a loop. Suggestions? Note: I am not specifically limited to using the vector data structure to solve this problem. test.vec = as.vector(c(1,2,4,3,2,3,4,5,4,3,4,5)) output.vec = vector(mode = 'numeric', length = length(test.vec)) for(i in 1

Several or multiple timeseries plot outputs from a single data frame

南楼画角 提交于 2019-11-30 09:25:08
问题 Hello, I have been struggling with this problem for a while now and anyone who can help me out I would greatly appreciate it. First off, I am working with time series data in a single data frame containing multiple time series. Too many to output individually into graphs. I have tried passing qplot() through ddply() however r tells me it qplot is not a function and therefore it will not work. the structure of my data is like this... goodlocs <- Loc Year dir Artesia 1983 1490 Artesia 1984 1575

How to avoid implicit character conversion when using apply on dataframe

空扰寡人 提交于 2019-11-30 08:34:37
When using apply on a data.frame, the arguments are (implicitly) converted to character. An example: df <- data.frame(v=1:10, t=1:10) df <- transform(df, t2 = as.POSIXlt(t, origin = "2013-08-13")) class(df$t2[1]) ## [1] "POSIXct" "POSIXt" (correct) but: apply(df, 1, function(y) class(y["t2"])) ## [1] "character" "character" "character" "character" "character" "character" ## [7] "character" "character" "character" "character" Is there any way to avoid this conversion? Or do I always have to convert back through as.POSIXlt(y["t2"]) ? edit My df has 2 timestamps (say, t2 and t3) and some other

What's the meaning to chain call and apply together?

怎甘沉沦 提交于 2019-11-30 06:59:53
问题 I come across this code in jsGarden, and I cannot figure the meaning to chain call and apply together. Both will execute the function with a given context object, why it could be chained? function Foo() {} Foo.prototype.method = function(a, b, c) { console.log(this, a, b, c); }; // Create an unbound version of "method" // It takes the parameters: this, arg1, arg2...argN Foo.method = function() { // Result: Foo.prototype.method.call(this, arg1, arg2... argN) Function.call.apply(Foo.prototype

Applying a function to every combination of elements in a vector

泪湿孤枕 提交于 2019-11-30 05:30:01
问题 I would like to apply a certain (custom) function to all combinations of an array. I think its best to explain with an example: Matrix 1 : A B C 1 2 3 Matrix 2 : A B C 4 5 6 I would like to do the following: obtain all the combinations of Matrix two and apply a function to each as follows: Matrix 3 : AB AC BC CB CA BA 4/2 4/3 5/3 6/2 6/1 5/1 Where the function applied to Matrix 3 is the corresponding element of Matrix 2 (represented by the first letter in each column of Matrix 3)/the

split apply recombine, plyr, data.table in R

白昼怎懂夜的黑 提交于 2019-11-30 05:23:10
I am doing the classic split-apply-recombine thing in R. My data set is a bunch of firms over time. The applying I am doing is running a regression for each firm and returning the residuals, therefore, I am not aggregating by firm. plyr is great for this but it takes a very very long time to run when the number of firms is large. Is there a way to do this with data.table ? Sample Data: dte, id, val1, val2 2001-10-02, 1, 10, 25 2001-10-03, 1, 11, 24 2001-10-04, 1, 12, 23 2001-10-02, 2, 13, 22 2001-10-03, 2, 14, 21 I need to split by each id (namely 1 and 2). Run a regression, return the

How can I construct an object using an array of values for parameters, rather than listing them out, in JavaScript?

旧街凉风 提交于 2019-11-30 05:05:44
Is this possible? I am creating a single base factory function to drive factories of different types (but have some similarities) and I want to be able to pass arguments as an array to the base factory which then possibly creates an instance of a new object populating the arguments of the constructor of the relevant class via an array. In JavaScript it's possible to use an array to call a function with multiple arguments by using the apply method: namespace.myFunc = function(arg1, arg2) { //do something; } var result = namespace.myFunc("arg1","arg2"); //this is the same as above: var r = [

Use Pandas groupby() + apply() with arguments

懵懂的女人 提交于 2019-11-30 04:44:56
I would like to use df.groupby() in combination with apply() to apply a function to each row per group. I normally use the following code, which usually works (note, that this is without groupby() ): df.apply(myFunction, args=(arg1,)) With the groupby() I tried the following: df.groupby('columnName').apply(myFunction, args=(arg1,)) However, I get the following error: TypeError: myFunction() got an unexpected keyword argument 'args' Hence, my question is: How can I use groupby() and apply() with a function that needs arguments? pandas.core.groupby.GroupBy.apply does NOT have named parameter

Handling NA values in apply and unique

那年仲夏 提交于 2019-11-30 02:42:25
问题 I have a 114 row by 16 column data frame where the rows are individuals, and the columns are either their names or NA. For example, the first 3 rows looks like this: name name.1 name.2 name.3 name.4 name.5 name.6 name.7 name.8 name.9 name.10 name.11 name.12 name.13 name.14 name.15 1 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> Aanestad <NA> Aanestad <NA> Aanestad <NA> 2 <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> Ackerman <NA> Ackerman <NA> Ackerman <NA> Ackerman <NA> 3 <NA> <NA> <NA> <NA>