subset inside a function by the variables specified in ddply

前端 未结 1 1370
Happy的楠姐
Happy的楠姐 2021-01-14 11:30

Often I need to subset a data.frame inside a function by the variables that I am subsetting another data.frame to which I apply ddply. To do that I explicitly write again th

相关标签:
1条回答
  • 2021-01-14 11:46

    The plyr package offers functions to make the whole split/apply/combine construct easy. To my knowledge, however, you can only split one thing: a list, a data.frame, an array.

    In your case, what you are trying to do is split two objects, then mapply (or Map), then recombine. Since plyr does not have a ready solution for this more complicated construct, you could do it in base R. That's how I assume people were doing things before plyr came out:

    # 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)
    

    Up to you to decide if it is more elegant or not than you current approach. The assignments I made were to help comprehension, but you can write the whole thing as a single res <- do.call(rbind, Map(FUN, split(d1, ...), split(d2, ...), ...)) statement if you prefer.

    0 讨论(0)
提交回复
热议问题