R,data.table,按列*数字*分组并对一列求和 [英] R, data.table, group by column *numbers* AND sum a column
本文介绍了R,data.table,按列*数字*分组并对一列求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有以下 data.table
> DT
# A B C D E N
# 1: J t X D N 0.07898388
# 2: U z U L A 0.46906049
# 3: H a Z F S 0.50826435
# ---
# 9998: X b R L X 0.49879990
# 9999: Z r U J J 0.63233668
# 10000: C b M K U 0.47796539
现在,我需要按一对列分组并计算总和N.如果您事先知道列名,那么这样做很容易:
Now I need to group by a pair of columns and calculate sum N. That's easy to do when you know column names in advance:
> DT[, sum(N), by=.(A,B)]
# A B V1
# 1: J t 6.556897
# 2: U z 9.060844
# 3: H a 4.293426
# ---
# 674: V z 11.439100
# 675: M x 1.736050
# 676: U k 3.676197
但是我必须在函数中执行此操作,该函数会接收列索引的向量进行分组.
But I must do that in a function, which receives a vector of column indices to group by.
> f <- function(columns = 1:2) {
DT[, sum(N), by=columns]
}
> f(1:2)
Error in `[.data.table`(DT, , sum(N), by = columns) :
The items in the 'by' or 'keyby' list are length (2). Each must be same
length as rows in x or number of rows returned by i (10000).
我也尝试过:
> f(list("A", "B"))
Error in `[.data.table`(DT, , sum(N), by = list(columns)) :
column or expression 1 of 'by' or 'keyby' is type list. Do not quote column
names. Usage: DT[,sum(colC),by=list(colA,month(colB))]
我如何使它工作?
推荐答案
这是我的处理方式:
f <- function(columns) {
Get <- if (!is.numeric(columns)) match(columns, names(DT)) else columns
columns <- names(DT)[Get]
DT[, sum(N), by = columns]
}
第一行( Get ..
)如果已经是数字,则将列"保留为数字;如果不是,则将其从字符转换为数字.
The first line (Get..
) keeps "columns" as numeric if it's already numeric or it converts it from characters to numeric if they are not.
使用一些示例数据对其进行测试:
Test it out with some sample data:
set.seed(1)
DT <- data.table(
A = sample(letters[1:3], 20, TRUE),
B = sample(letters[1:5], 20, TRUE),
C = sample(LETTERS[1:2], 20, TRUE),
N = rnorm(20)
)
## Should work with either column number or name
f(1)
f("A")
f(c(1, 3))
f(c("A", "C"))
这篇关于R,data.table,按列*数字*分组并对一列求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文