In the spirit of sharing, you can also use the sqldf
and data.table
packages quite easily:
Your data:
df <- read.table(text=" sample value
1 a 1
2 a 2
3 b 3
4 b 4
5 b 5
6 c 6",header=TRUE)
The sqldf
alternative:
library(sqldf)
sqldf("select sample, sum(value) `value` from df group by sample")
# sample value
# 1 a 3
# 2 b 12
# 3 c 6
The data.table
alternative:
library(data.table)
DT <- data.table(df, key="sample")
DT[, list(value = sum(value)), by=key(DT)]
# sample value
# 1: a 3
# 2: b 12
# 3: c 6