将数据框中重复条目的行相加(Excel 或 R)
Adding together rows from duplicate entries in a dataframe (Excel or R)
我有一个包含一些重复项的数据框,大约有 100 个,数据显示如下:
Data V1 V2 V3 V4
Cellulomonas uda 0.2 0.0 0.0 0.1
Cellulomonas uda 0.0 0.1 0.3 0.1
但我想找到数据框中的所有重复项并将它们加在一起,给出:
Data V1 V2 V3 V4
Cellulomonas uda 0.2 0.1 0.3 0.2
dplyr 中是否有函数可以帮助解决这个问题?或者甚至是一种将行添加到 Excel 中并手动删除其中一个重复行的方法。
您可以对每个 Data
值取 V 值的总和:
df1 <- read.table(text="Data V1 V2 V3 V4
'Cellulomonas uda' 0.2 0.0 0.0 0.1
'Cellulomonas uda' 0.0 0.1 0.3 0.1",h=T,string=F)
library(dplyr)
df1 %>% group_by(Data) %>% summarize_all(sum)
# # A tibble: 1 x 5
# Data V1 V2 V3 V4
# <chr> <dbl> <dbl> <dbl> <dbl>
# 1 Cellulomonas uda 0.2 0.1 0.3 0.2
有了基础 R 我们可以使用 aggregate
:
aggregate(. ~ Data, df1, sum)
Data V1 V2 V3 V4
1 Cellulomonas uda 0.2 0.1 0.3 0.2
并且 data.table
我认为我们可以做到:
library(data.table)
dt[, lapply(.SD, sum), by = Data]
Data V1 V2 V3 V4
1 Cellulomonas uda 0.2 0.1 0.3 0.2
我有一个包含一些重复项的数据框,大约有 100 个,数据显示如下:
Data V1 V2 V3 V4
Cellulomonas uda 0.2 0.0 0.0 0.1
Cellulomonas uda 0.0 0.1 0.3 0.1
但我想找到数据框中的所有重复项并将它们加在一起,给出:
Data V1 V2 V3 V4
Cellulomonas uda 0.2 0.1 0.3 0.2
dplyr 中是否有函数可以帮助解决这个问题?或者甚至是一种将行添加到 Excel 中并手动删除其中一个重复行的方法。
您可以对每个 Data
值取 V 值的总和:
df1 <- read.table(text="Data V1 V2 V3 V4
'Cellulomonas uda' 0.2 0.0 0.0 0.1
'Cellulomonas uda' 0.0 0.1 0.3 0.1",h=T,string=F)
library(dplyr)
df1 %>% group_by(Data) %>% summarize_all(sum)
# # A tibble: 1 x 5
# Data V1 V2 V3 V4
# <chr> <dbl> <dbl> <dbl> <dbl>
# 1 Cellulomonas uda 0.2 0.1 0.3 0.2
有了基础 R 我们可以使用 aggregate
:
aggregate(. ~ Data, df1, sum)
Data V1 V2 V3 V4
1 Cellulomonas uda 0.2 0.1 0.3 0.2
并且 data.table
我认为我们可以做到:
library(data.table)
dt[, lapply(.SD, sum), by = Data]
Data V1 V2 V3 V4
1 Cellulomonas uda 0.2 0.1 0.3 0.2