Reputation: 97
I have data that looks like this.
I wish to sum up the value column for rows that have the same name, time, and site. In this case, rows 3 and 4 would be summed, and rows 5 and 7 would be summed up.
I wish for the resulting data frame to look like this.
example data:
name = c('a', 'a', 'b' , 'b', 'c', 'c', 'c', 'd')
time = c(1,2,1,1,3,3,3,4)
site = c('A', 'A', 'A', 'A','B', 'D','B', 'E')
value = c(5,8,1,0,7,0,8,10)
mock = data.frame(name, time,site,value)
Upvotes: 0
Views: 48
Reputation: 1771
I really like the data.table
way to do this :
library(data.table)
data[, .(value = sum(value)), by = list(name, time, site)]
name time site value
1: a 1 A 5
2: a 2 A 8
3: b 1 A 1
4: c 3 B 15
5: c 3 D 0
6: d 4 E 10
The nice thing with data.table
is that the order of your rows in the first column isn't change while aggregate()
change it.
Upvotes: 1
Reputation: 102609
You can use base R aggregate
to make it, i.e.,
> aggregate(value~.,mock,sum)
name time site value
1 a 1 A 5
2 b 1 A 1
3 a 2 A 8
4 c 3 B 15
5 c 3 D 0
6 d 4 E 10
Upvotes: 0
Reputation: 1928
Here's a tidyverse answer:
mock <- mock %>%
group_by(name, time, site) %>%
summarize(value = sum(value))
name time site value
<fct> <dbl> <fct> <dbl>
1 a 1 A 5
2 a 2 A 8
3 b 1 A 1
4 c 3 B 15
5 c 3 D 0
6 d 4 E 10
Upvotes: 0