样本数据
data=data.frame("group"=c(rep(0:1,10)),
"value1" = c(1:10),
"value2" = seq(11:20),
"value3" = as.factor(rep(1:3,length=10)))
我有一个数据框“ data”,目标是创建一个新的数据框,如图所示。请注意,值3是一个因变量。因此,我想报告n和百分比。对于图片中的每个组。
我知道分别估算最小值和最大值,如下所示
value1_min0 = min(data$value1)[data$group==1]
value1_max0 = max(data$value1)[data$group==1]
value1_min1 = min(data$value1)[data$group==0]
value1_max1 = max(data$value1)[data$group==0]
但是有更好的方法更有效地做到这一点吗?
答案 0 :(得分:3)
对数字变量使用汇总,对因子使用表/属性表。
ag <- aggregate(cbind(value1, value2) ~ group, data,
function(x) c(min = min(x), max = max(x)))
tab12 <- as.data.frame.matrix(setNames(as.data.frame(t(ag[-1])),
ag[[1]]))
counts <- table(data$group)
tab3 <- prop.table(table(group = data$value3, value3 = data$group), 2)
rownames(tab3) <- paste("value3", rownames(tab3), sep = " = ")
rbind(tab12, n = counts, as.data.frame.matrix(tab3))
给出以下内容
0 1
value1.min 1.0 2.0
value1.max 9.0 10.0
value2.min 1.0 2.0
value2.max 9.0 10.0
n 10.0 10.0
value3 = 1 0.4 0.4
value3 = 2 0.2 0.4
value3 = 3 0.4 0.2
这种选择有点乏味,但是很简单:
library(sqldf)
res <- sqldf('select
[group],
min(value1) [value1.min],
max(value1) [value1.max],
min(value2) [value2.min],
max(value2) [value2.max],
count(*) n,
avg(value3 = 1) [value3 == 1],
avg(value3 = 2) [value3 == 2],
avg(value3 = 3) [value3 == 3]
from data
group by [group]')
setNames(as.data.frame(t(res[-1])), res$group)
给予:
0 1
value1.min 1.0 2.0
value1.max 9.0 10.0
value2.min 1.0 2.0
value2.max 9.0 10.0
n 10.0 10.0
value3 == 1 0.4 0.4
value3 == 2 0.2 0.4
value3 == 3 0.4 0.2
使用Skimr软件包,我们可以这样做:
library(dplyr)
library(skimr)
library(tidyr)
# L <- list("fraction = 1" = function(x) mean(x == 1),
# "fraction = 2" = function(x) mean(x == 2),
# "fraction = 3" = function(x) mean(x == 3))
levs <- levels(data$value3)
L <- lapply(levs, function(lv) function(x) mean(x == lv))
names(L) <- paste("fraction =", levs)
skim_with(integer = list(min = min, max = max),
factor = c(L, n = length), append = FALSE)
data %>%
group_by(group) %>%
skim %>%
ungroup %>%
select(group, variable, stat, value) %>%
spread(group, value)
给出以下内容:
# A tibble: 8 x 4
variable stat `0` `1`
<chr> <chr> <dbl> <dbl>
1 value1 max 9 10
2 value1 min 1 2
3 value2 max 9 10
4 value2 min 1 2
5 value3 fracion = 1 0.4 0.4
6 value3 fracion = 2 0.2 0.4
7 value3 fracion = 3 0.4 0.2
8 value3 n 10 10
修订后的基本解决方案;添加了sqldf和Skimr解决方案。改进的撇油器解决方案。
答案 1 :(得分:1)
使用dplyr
(> = 0.8.0语法):
library(dplyr)
df %>%
group_by(group) %>%
summarise_all(list(~min(.),~max(.)))
结果:
# A tibble: 2 x 7
group value1_min value2_min value3_min value1_max value2_max value3_max
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 0 1 1 1 9 9 3
2 1 2 2 1 10 10 3
答案 2 :(得分:0)
使用dplyr
和tidyr
:
library(dplyr)
library(tidyr)
data %>%
group_by(group) %>%
summarize(value1_min = min(value1),
value1_max = max(value1),
value2_min = min(value2),
value2_max = max(value2),
value3_n = length(unique(value3)),
value3_perc1 = length(sum(value3==1))/length(unique(value3)),
value3_perc2 = length(sum(value3==2))/length(unique(value3)),
value3_perc3 = length(sum(value3==3))/length(unique(value3))
) %>%
gather(review, value, -group) %>%
spread(group, value)
结果:
# A tibble: 7 x 3
review `0` `1`
<chr> <dbl> <dbl>
1 value1_max 9.00 10.0
2 value1_min 1.00 2.00
3 value2_max 9.00 10.0
4 value2_min 1.00 2.00
5 value3_n 3.00 3.00
6 value3_perc1 0.333 0.333
7 value3_perc2 0.333 0.333
8 value3_perc3 0.333 0.333