将函数应用于R中的每个组

时间:2015-05-26 20:44:18

标签: r dplyr

     > head(m)
      X id1 q_following topic_followed topic_answered nfollowers nfollowing
    1 1   1          80             80            100        180        180
    2 2   1          76             76             95        171        171
    3 3   1          72             72             90        162        162
    4 4   1          68             68             85        153        153
    5 5   1          64             64             80        144        144
    6 6   1          60             60             75        135        135


> head(d)
  X id1 q_following topic_followed topic_answered nfollowers nfollowing
1 1   1          63            735            665        949        146
2 2   1          89            737            666        587        185
3 3   1         121            742            670        428        264
4 4   1         277            750            706        622        265
5 5   1         339            765            734        108        294
6 6   1         363            767            766        291        427


matcher <- function(x,y){  return(na.omit(m[which(d[,y]==x),y]))  }


max_matcher <- function(x) { return(sum(matcher(x,3:13)))    }

result <- foreach(1:1000, function(x) { 
    if(max(max_matcher(1:1000)) == max_matcher(x)) return(x)
})

我想在每个组中计算 结果 ,按id1数据框m分组。

m %>% group_by(id1) %>% summarise(result) #doesn't work
by(m, m[,"id1"], result)                  #doesn't work

我该怎么办?

0 个答案:

没有答案