Collapse data frame by group using different functions on each variable

Define df<-read.table(textConnection('egg 1 20 a egg 2 30 a jap 3 50 b jap 1 60 b')) s.t. > df V1 V2 V3 V4 1 egg 1 20 a 2 egg 2 30 a 3 jap 3 50 b 4 jap 1 60 b My data has no factors so I convert factors to characters: > df$V1 <- as.character(df$V1) > df$V4 <- as.character(df$V4) I would like to "collapse" the data frame by V1 keeping: - The max of V2 - The mean of V3 - The mode of V4 (this value does not actually change within V1 groups, so first, last, etc might do also.) Please note this is a general question, e.g. my dataset is much larger and I may want to use different functions (e.g. last, first, min, max, variance, st. dev., etc for different variables) when collapsing. Hence the functions argument could be quite long. In this case I would want output of the form: > df.collapse V1 V2 V3 V4 1 egg 2 25 a 2 jap 3 55 b

以上就是Collapse data frame by group using different functions on each variable的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准