数据 table 中多行和多列的中位数并进行分组
Median from multiple rows and columns in a data table with grouping
我有一个数据 table,其中包含超过 90000 个观察值和 1201 个变量。除最后一列外的所有列都存储数值,最后一列是源文件名列(超过 100 个)。这是数据的一小部分 table:
library(data.table)
DT <- data.table(V1=sample(0:100,20,replace=TRUE),
V2=sample(0:100,20,replace=TRUE), V3=sample(0:100,20,replace=TRUE),
V4=sample(0:100,20,replace=TRUE), V5=sample(0:100,20,replace=TRUE),
V6=sample(0:100,20,replace=TRUE), V7=sample(0:100,20,replace=TRUE),
file=rep(c("A","B","C","D"), each = 5))
我想做的是计算每组 (file
) 中所有值的中位数。所以例如对于 A 组,将立即从第 1、2、3、4、5 行计算中位数。在下一步中,我想根据组(下面的预期输出)将中位数分配给每一行。
这个问题看起来很简单,我搜索了很多关于 median/mean 计算的类似问题取决于一个组(aggregate
作为最流行的解决方案之一)。然而,在所有情况下,只有一列被考虑用于中值计算。这里有 7(或在我的原始数据中为 1200)并且 median
不接受 - 我应该提供一个数值向量。
因此,我尝试了 unlist
、aggregate
、dplyr
包,tapply
运气好的话...
由于数据和组的数量(即 file
),代码应该非常自动和高效...非常感谢您的帮助!
如果代码明显失败,只是一个小例子:
DT_median <- setDT(DT)[, DT_med := median(DT[,1:7]), by = file]
预期结果应如下所示:
V1 V2 V3 V4 V5 V6 V7 file DT_med
42 78 9 0 60 46 65 A 37.5
36 36 46 45 5 96 64 A 37.5
83 31 92 100 15 2 9 A 37.5
36 16 49 82 32 4 46 A 37.5
29 17 39 6 62 52 97 A 37.5
37 70 17 90 8 10 93 B 47
72 62 68 83 96 77 20 B 47
10 47 29 2 93 16 30 B 47
69 87 7 47 96 17 8 B 47
23 70 72 27 10 86 49 B 47
78 51 13 33 56 6 39 C 51
28 92 100 5 75 33 17 C 51
71 82 9 20 34 83 22 C 51
62 40 84 87 37 45 34 C 51
55 80 55 94 66 96 12 C 51
93 1 99 97 7 77 6 D 41
53 55 71 12 19 25 28 D 41
27 25 28 89 41 22 60 D 41
91 25 25 57 21 98 27 D 41
2 63 17 53 99 65 95 D 41
因为我们要根据所有值计算 median
,按 'file'、unlist
Data.table 的子集分组(.SD
),获取 median
并分配 (:=
) 输出以创建新列 'DT_med'
library(data.table)
DT[, DT_med := median(unlist(.SD), na.rm = TRUE), by = file]
我有一个数据 table,其中包含超过 90000 个观察值和 1201 个变量。除最后一列外的所有列都存储数值,最后一列是源文件名列(超过 100 个)。这是数据的一小部分 table:
library(data.table)
DT <- data.table(V1=sample(0:100,20,replace=TRUE),
V2=sample(0:100,20,replace=TRUE), V3=sample(0:100,20,replace=TRUE),
V4=sample(0:100,20,replace=TRUE), V5=sample(0:100,20,replace=TRUE),
V6=sample(0:100,20,replace=TRUE), V7=sample(0:100,20,replace=TRUE),
file=rep(c("A","B","C","D"), each = 5))
我想做的是计算每组 (file
) 中所有值的中位数。所以例如对于 A 组,将立即从第 1、2、3、4、5 行计算中位数。在下一步中,我想根据组(下面的预期输出)将中位数分配给每一行。
这个问题看起来很简单,我搜索了很多关于 median/mean 计算的类似问题取决于一个组(aggregate
作为最流行的解决方案之一)。然而,在所有情况下,只有一列被考虑用于中值计算。这里有 7(或在我的原始数据中为 1200)并且 median
不接受 - 我应该提供一个数值向量。
因此,我尝试了 unlist
、aggregate
、dplyr
包,tapply
运气好的话...
由于数据和组的数量(即 file
),代码应该非常自动和高效...非常感谢您的帮助!
如果代码明显失败,只是一个小例子:
DT_median <- setDT(DT)[, DT_med := median(DT[,1:7]), by = file]
预期结果应如下所示:
V1 V2 V3 V4 V5 V6 V7 file DT_med
42 78 9 0 60 46 65 A 37.5
36 36 46 45 5 96 64 A 37.5
83 31 92 100 15 2 9 A 37.5
36 16 49 82 32 4 46 A 37.5
29 17 39 6 62 52 97 A 37.5
37 70 17 90 8 10 93 B 47
72 62 68 83 96 77 20 B 47
10 47 29 2 93 16 30 B 47
69 87 7 47 96 17 8 B 47
23 70 72 27 10 86 49 B 47
78 51 13 33 56 6 39 C 51
28 92 100 5 75 33 17 C 51
71 82 9 20 34 83 22 C 51
62 40 84 87 37 45 34 C 51
55 80 55 94 66 96 12 C 51
93 1 99 97 7 77 6 D 41
53 55 71 12 19 25 28 D 41
27 25 28 89 41 22 60 D 41
91 25 25 57 21 98 27 D 41
2 63 17 53 99 65 95 D 41
因为我们要根据所有值计算 median
,按 'file'、unlist
Data.table 的子集分组(.SD
),获取 median
并分配 (:=
) 输出以创建新列 'DT_med'
library(data.table)
DT[, DT_med := median(unlist(.SD), na.rm = TRUE), by = file]