R Dataframe:当其他两个值匹配时合并行/值
R Dataframe: Combine rows / values when two other values match
我有一个如下所示的数据框:
Name Fruit Cost
Adam Orange 2
Adam Apple 3
Bob Orange 3
Cathy Orange 4
Cathy Orange 5
数据框创建:
df=data.frame(Name=c("Adam","Adam","Bob","Cathy","Cathy"),Fruit=c("Orange","Apple","Orange","Orange","Orange"),Cost=c(2,3,3,4,5))
我想编写一个组合脚本,说明当 Name 和 Fruit 匹配时,添加 Cost 并删除 other 行。例如,结果看起来像这样,两个 Cathy 成本合并在一起,因为 Name 和 Fruit 相同:
Name Fruit Cost
Adam Orange 2
Adam Apple 3
Bob Orange 3
Cathy Orange 9
我想写一个for循环,逐行比较,逐值比较,比较加删除。但我不得不想象有一种 faster/cleaner 方式。
您要做的是在一个组内求和 Cost
。
在基础 R 中:
aggregate(Cost ~ Name + Fruit, df, sum)
或使用dplyr
:
library(dplyr)
df %>%
group_by(Name, Fruit) %>%
summarize(Cost = sum(Cost), .groups = "drop")
我们可以使用
library(data.table)
setDT(df)[, .(Cost = sum(Cost)), .(Name, Fruit)]
我有一个如下所示的数据框:
Name Fruit Cost
Adam Orange 2
Adam Apple 3
Bob Orange 3
Cathy Orange 4
Cathy Orange 5
数据框创建:
df=data.frame(Name=c("Adam","Adam","Bob","Cathy","Cathy"),Fruit=c("Orange","Apple","Orange","Orange","Orange"),Cost=c(2,3,3,4,5))
我想编写一个组合脚本,说明当 Name 和 Fruit 匹配时,添加 Cost 并删除 other 行。例如,结果看起来像这样,两个 Cathy 成本合并在一起,因为 Name 和 Fruit 相同:
Name Fruit Cost
Adam Orange 2
Adam Apple 3
Bob Orange 3
Cathy Orange 9
我想写一个for循环,逐行比较,逐值比较,比较加删除。但我不得不想象有一种 faster/cleaner 方式。
您要做的是在一个组内求和 Cost
。
在基础 R 中:
aggregate(Cost ~ Name + Fruit, df, sum)
或使用dplyr
:
library(dplyr)
df %>%
group_by(Name, Fruit) %>%
summarize(Cost = sum(Cost), .groups = "drop")
我们可以使用
library(data.table)
setDT(df)[, .(Cost = sum(Cost)), .(Name, Fruit)]