我想聚合我的data.frame。
这是示例数据:
data <- structure(list(Charge = c(210133L, 210133L, 210133L, 210152L,
210152L, 210152L, 210152L, 210180L, 210180L, 210180L), Seq = c(1L,
2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 2L), x = c(NA, 1.5, 2,
1.5, 1, 0.67, 1.17, 1, 1, 1), y = c(0.5, 0.5, 1, NA, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5)), .Names = c("Charge", "Seq",
"x", "y"), row.names = c(NA, 10L), class = "data.frame")
*为便于解释(与上述相同,格式不同):
Charge Seq x y
1 210133 1 NA 0.5
2 210133 2 1.50 0.5
3 210133 3 2.00 1.0
4 210152 1 1.50 NA
5 210152 2 1.00 0.5
6 210152 3 0.67 0.5
7 210152 4 1.17 0.5
8 210180 1 1.00 0.5
9 210180 2 1.00 0.5
10 210180 2 1.00 0.5
对于每个唯一电荷,当Seq> 1时,必须执行x和y列行的中位数。
因此,例如对于此样本数据,我想获得的是seq> 1的x和y行中位数的其他行:
Charge Seq x y
1 210133 1 NA 0.5
2 210133 2 1.50 0.5
3 210133 3 2.00 1.0
4 210133 >1 1.75 0.75 #here is additional row with median of x and y
4 210152 1 1.50 NA
5 210152 2 1.00 0.5...
感谢帮助!
我们可以使用data.table
。将'data.frame'转换为'data.table'(setDT(data)
),按“ Charge”分组,遍历列(lapply(.SD,...
),根据'i'()中的条件获取median
指定的列中.SDcols
的Seq >1
,创建一个' Seq'列,其值“> 1”。将原始数据与新数据一起放在中list
,用于rbind
组合数据集,order
必要时使用。
library(data.table)
setDT(data)
res <- data[Seq > 1L, lapply(.SD, median, na.rm=TRUE),
by = Charge, .SDcols = x:y][, Seq := ">1"][]
ans <- setorder(rbind(data, res), Charge, Seq)
# Charge Seq x y
# 1: 210133 1 NA 0.50
# 2: 210133 2 1.50 0.50
# 3: 210133 3 2.00 1.00
# 4: 210133 >1 1.75 0.75
# 5: 210152 1 1.50 NA
# 6: 210152 2 1.00 0.50
# 7: 210152 3 0.67 0.50
# 8: 210152 4 1.17 0.50
# 9: 210152 >1 1.00 0.50
#10: 210180 1 1.00 0.50
#11: 210180 2 1.00 0.50
#12: 210180 2 1.00 0.50
#13: 210180 >1 1.00 0.50
使用类似的选项dplyr
将原始数据集中class
的“ Seq”转换为character
。那么,filter
对于“序列”不等于1,通过“充电”分组,我们得到的median
列有summarise_each
,请在输出“序列”新列,然后将其绑定使用与新的原始数据bind_rows
,并order
在必要时。
library(magrittr)
library(dplyr)
data %<>%
mutate(Seq = as.character(Seq))
data %>%
filter(Seq!="1") %>%
group_by(Charge) %>%
summarise_each(funs(median=median(., na.rm=TRUE)), x:y) %>%
mutate(Seq = ">1") %>%
bind_rows(data, .) %>%
mutate(Seq = factor(Seq, levels = c(unique(data$Seq), ">1"))) %>%
arrange(Charge, Seq)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句