r - Allocate rows to columns from different data frames (lapply operations) -


i know how can allocate given row of data frame given column of data frame without merging, aggregating, etc. example, making row 1 of df1 associated column 1 of df 2, operations carried on column 1 of df 2 take values row 1 of df 1.

let´s create df1 (i.e. v) , df2 (i.e. m):

v<-data.frame(a=c(2,1),b=c(4,2),c=c(6,3)) m<-data.frame(v1=rep(1,10),v2=rep(2,10))   #> v    b c v1 2 4 6 v2 1 2 3  #> m    v1 v2 1   1  2 2   1  2 3   1  2 4   1  2 5   1  2 6   1  2 7   1  2 8   1  2 9   1  2 10  1  2 

and let´s create list of functions pass on m taking values v

funs<-list( f1<-function(x,a,b,c) a*x+b*x+c*x, f2<-function(x,a,b,c) a*x-b*x-c*x ) 

now want pass list of function on m, (but "ideally" i'd use values stored in row 1 column 1 , ones in row 2 column 2, , unfortunately haven´t found how.

res1<-list(data.frame(matrix(,10,2)),data.frame(matrix(,10,2))) for(i in seq_along(v[,1])){ res1[[i]]<-lapply(funs,function(f) f(m,v[i,1],v[i,2],v[i,3])) } 

this returns: 1) first function implemented on columns 1 , 2 set of values row 1 2) second function implemented on columns 1 & 2 set of values row 1 3) first function implemented on columns 1 & 2 set of values row 2 4) second function implemented on columns 1 & 2 set of values row 2

what want is: 1) first , second function implemented on column 1 values row 1 2) first , second function implemented on column 2 values row 2

> res1 [[1]] [[1]][[1]]    v1 v2 1  12 24 2  12 24 3  12 24 4  12 24 5  12 24 6  12 24 7  12 24 8  12 24 9  12 24 10 12 24  [[1]][[2]]    v1  v2 1  -8 -16 2  -8 -16 3  -8 -16 4  -8 -16 5  -8 -16 6  -8 -16 7  -8 -16 8  -8 -16 9  -8 -16 10 -8 -16   [[2]] [[2]][[1]]    v1 v2 1   6 12 2   6 12 3   6 12 4   6 12 5   6 12 6   6 12 7   6 12 8   6 12 9   6 12 10  6 12  [[2]][[2]]    v1 v2 1  -4 -8 2  -4 -8 3  -4 -8 4  -4 -8 5  -4 -8 6  -4 -8 7  -4 -8 8  -4 -8 9  -4 -8 10 -4 -8 

help appreciated!!! cheers

try

 resn <- lapply(seq_len(nrow(v)), function(i) {                v <- unlist(v[i,])               lapply(seq_along(funs), function(i)                    funs[[i]](m, v[1], v[2], v[3])) }) 

which same res1

edit

from description, seems want this:

  v1 <- as.data.frame(t(v))   resn1 <- map(function(.fun,y,z) do.call(cbind,                lapply(.fun, function(x) x(y, z[1],z[2],z[3]))) ,                                                 list(funs), m, v1)    resn1    #[[1]]   #     [,1] [,2]   #[1,]   12   -8   #[2,]   12   -8   #[3,]   12   -8   #[4,]   12   -8   #[5,]   12   -8   #[6,]   12   -8   #[7,]   12   -8   #[8,]   12   -8   #[9,]   12   -8  #[10,]   12   -8   #[[2]]  #     [,1] [,2]  # [1,]   12   -8  # [2,]   12   -8  # [3,]   12   -8  # [4,]   12   -8  # [5,]   12   -8  # [6,]   12   -8  # [7,]   12   -8  # [8,]   12   -8  # [9,]   12   -8  #[10,]   12   -8 

which same as

  m[,1]*v[1,1]+m[,1]*v[1,2]+m[,1]*v[1,3]    #[1] 12 12 12 12 12 12 12 12 12 12    m[,1]*v[1,1]-m[,1]*v[1,2]-m[,1]*v[1,3]    #[1] -8 -8 -8 -8 -8 -8 -8 -8 -8 -8    m[,2]*v[2,1]+m[,2]*v[2,2]+m[,2]*v[2,3]    #[1] 12 12 12 12 12 12 12 12 12 12    m[,2]*v[2,1]-m[,2]*v[2,2]-m[,2]*v[2,3]   #[1] -8 -8 -8 -8 -8 -8 -8 -8 -8 -8 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -