如何使用每行的模式(最常见)值聚合 R 中的数据?
How to aggregate data in R with mode (most common) value for each row?
我有一个数据集,例如,
Data <- data.frame(
groupname = as.factor(sample(c("a", "b", "c"), 10, replace = TRUE)),
someuser = sample(c("x", "y", "z"), 10, replace = TRUE))
groupname someuser
1 a x
2 b y
3 a x
4 a y
5 c z
6 b x
7 b x
8 c x
9 c y
10 c x
如何汇总数据以便获得:
groupname someuser
a x
b x
c x
这是每个组名最常用的值。
PS:根据我的设置,我只能使用 2 个 pakcages - plyr 和 lubridate
这可能适合您 - 使用基数 R
set.seed(1)
Data <- data.frame(
groupname = as.factor(sample(c("a", "b", "c"), 10, replace = TRUE)),
someuser = sample(c("x", "y", "z"), 10, replace = TRUE))
Data
groupname someuser
1 a x
2 b x
3 b z
4 c y
5 a z
6 c y
7 c z
8 b z
9 b y
10 a z
res <- lapply(split(Data, Data$groupname), function(x)
data.frame(groupname=x$groupname[1], someuser=names(sort(table(x$someuser),
decreasing=TRUE))[1]))
do.call(rbind, res)
groupname someuser
a a z
b b z
c c y
并使用 ddply
sort_fn2 <- function(x) {names(sort(table(x$someuser), decreasing=TRUE))[1]}
ddply(Data, .(groupname), .fun=sort_fn2)
groupname V1
1 a z
2 b z
3 c y
您可以将此 function 与聚合结合使用以找到模式。
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
aggregate(someuser ~ groupname, Data, Mode)
groupname someuser
1 a x
2 b x
3 c x
请注意,如果出现平局,它只会 return 第一个值。
多种选择。这里使用 table
来计算频率,which.max
到 select 最大值。在 data.table
框架内:
library(data.table)
setDT(Data)[,list(someuser={
tt <- table(someuser)
names(tt)[which.max(tt)]
}),groupname]
使用plyr
(几乎相同):
library(plyr)
ddply(Data,.(groupname),summarize,someuser={
tt <- table(someuser)
names(tt)[which.max(tt)]
})
我有一个数据集,例如,
Data <- data.frame(
groupname = as.factor(sample(c("a", "b", "c"), 10, replace = TRUE)),
someuser = sample(c("x", "y", "z"), 10, replace = TRUE))
groupname someuser
1 a x
2 b y
3 a x
4 a y
5 c z
6 b x
7 b x
8 c x
9 c y
10 c x
如何汇总数据以便获得:
groupname someuser
a x
b x
c x
这是每个组名最常用的值。
PS:根据我的设置,我只能使用 2 个 pakcages - plyr 和 lubridate
这可能适合您 - 使用基数 R
set.seed(1)
Data <- data.frame(
groupname = as.factor(sample(c("a", "b", "c"), 10, replace = TRUE)),
someuser = sample(c("x", "y", "z"), 10, replace = TRUE))
Data
groupname someuser
1 a x
2 b x
3 b z
4 c y
5 a z
6 c y
7 c z
8 b z
9 b y
10 a z
res <- lapply(split(Data, Data$groupname), function(x)
data.frame(groupname=x$groupname[1], someuser=names(sort(table(x$someuser),
decreasing=TRUE))[1]))
do.call(rbind, res)
groupname someuser
a a z
b b z
c c y
并使用 ddply
sort_fn2 <- function(x) {names(sort(table(x$someuser), decreasing=TRUE))[1]}
ddply(Data, .(groupname), .fun=sort_fn2)
groupname V1
1 a z
2 b z
3 c y
您可以将此 function 与聚合结合使用以找到模式。
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
aggregate(someuser ~ groupname, Data, Mode)
groupname someuser
1 a x
2 b x
3 c x
请注意,如果出现平局,它只会 return 第一个值。
多种选择。这里使用 table
来计算频率,which.max
到 select 最大值。在 data.table
框架内:
library(data.table)
setDT(Data)[,list(someuser={
tt <- table(someuser)
names(tt)[which.max(tt)]
}),groupname]
使用plyr
(几乎相同):
library(plyr)
ddply(Data,.(groupname),summarize,someuser={
tt <- table(someuser)
names(tt)[which.max(tt)]
})