假设我具有以下数据框
data = data.frame(age=c("a","a","b","b"),cause=c("one","two","one","two"),value=c(1,2,5,8))
data
age cause value
1 a one 1
2 a two 2
3 b one 5
4 b two 8
我想创建一个新的原因,将两个年龄段的原因值相加,如下所示
data
age cause value
1 a one 1
2 a two 2
3 b one 5
4 b two 8
5 a onetwo 3
6 b onetwo 13
关于如何执行此操作的任何想法?
答案 0 :(得分:1)
一种选择是使用dplyr
:
library(dplyr)
data <- data.frame(age=c("a","a","b","b"),cause=c("one","two","one","two"),value=c(1,2,5,8),
stringsAsFactors = FALSE)
data <- bind_rows(data, data %>% group_by(age) %>%
summarise(cause = paste0(cause, collapse = ""),
value = sum(value)))
答案 1 :(得分:0)
带有final String timeRegist = user?.timestamp?.toDate() ?? '';
Text(AppLocalizations.of(context).translate("Member since_") + "$timeRegist )}",
style: TextStyle(color: Colors.black45))
data.table
答案 2 :(得分:0)
这是我自己的方法,根据一分钟前删除的回复,我不知道为什么。
data = data.frame(age=c("a","a","b","b"),cause=c("one","two","one","two"),value=c(1,2,5,8))
data_age = aggregate(value~ age + cause, data, sum)
causes = aggregate(value ~ age, data, sum)
causes$cause = "Both"
data_age <- rbind(data_age, causes)