R - Group data but apply different functions to different columns

若如初见. 提交于 2020-01-02 05:47:06

问题


I'd like to group this data but apply different functions to some columns when grouping.

ID  type isDesc isImage
1   1    1      0
1   1    0      1
1   1    0      1
4   2    0      1
4   2    1      0
6   1    1      0
6   1    0      1
6   1    0      0

I want to group by ID, columns isDesc and isImage can be summed, but I would like to get the value of type as it is. type will be the same through the whole dataset. The result should look like this:

ID  type isDesc isImage
1   1    1      2
4   2    1      1
6   1    1      1

Currently I am using

library(plyr)
summarized = ddply(data, .(ID), numcolwise(sum))

but it simply sums up all the columns. You don't have to use ddply but if you think it's good for the job I'd like to stick to it. data.table library is also an alternative


回答1:


Using data.table:

require(data.table)
dt <- data.table(data, key="ID")
dt[, list(type=type[1], isDesc=sum(isDesc), 
                  isImage=sum(isImage)), by=ID]

#    ID type isDesc isImage
# 1:  1    1      1       2
# 2:  4    2      1       1
# 3:  6    1      1       1

Using plyr:

ddply(data , .(ID), summarise, type=type[1], isDesc=sum(isDesc), isImage=sum(isImage))
#   ID type isDesc isImage
# 1  1    1      1       2
# 2  4    2      1       1
# 3  6    1      1       1

Edit: Using data.table's .SDcols, you can do this in case you've too many columns that are to be summed, and other columns to be just taken the first value.

dt1 <- dt[, lapply(.SD, sum), by=ID, .SDcols=c(3,4)]
dt2 <- dt[, lapply(.SD, head, 1), by=ID, .SDcols=c(2)]
> dt2[dt1]
#    ID type isDesc isImage
# 1:  1    1      1       2
# 2:  4    2      1       1
# 3:  6    1      1       1

You can provide column names or column numbers as arguments to .SDcols. Ex: .SDcols=c("type") is also valid.



来源:https://stackoverflow.com/questions/15434123/r-group-data-but-apply-different-functions-to-different-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!