The subset inside the function of the variables specified in ddply

Often I need to multiply data.frame inside a function with variables, which I multiply with another data.frame, to which I apply ddply. To do this, I explicitly write the variables inside the function again, and I wonder if there is a more elegant way to do this. Below I include a trivial example to show what my current approach is for this.

d1<-expand.grid(x=c('a','b'),y=c('c','d'),z=1:3)
d2<-expand.grid(x=c('a','b'),y=c('c','d'),z=4:6)

results<-ddply(d1,.(x,y),function(d) {
   d2Sub<-subset(d2,x==unique(d$x) & y==unique(d$y))
   out<-d$z+d2Sub$z
   data.frame(out)
 })
+3
source share
1 answer

The package plyroffers features that make it easy to create an entire split / apply / comb structure. As far as I know, you can only split one thing: a list, an array data.frame, an array.

, , , mapply ( Map), . plyr , R. , - , plyr:

# split
d1.split <- split(d1, list(d1$x, d1$y))
d2.split <- split(d2, list(d2$x, d2$y))

# apply
res.split <- Map(function(df1, df2) data.frame(x = df1$x, y = df1$y,
                                               out = df1$z + df2$z),
                 d1.split, d2.split, USE.NAMES = FALSE)

#  combine
res <- do.call(rbind, res.split)

, , , . , , , res <- do.call(rbind, Map(FUN, split(d1, ...), split(d2, ...), ...)), .

+2

Source: https://habr.com/ru/post/1533021/


All Articles