I have a data set (test)
v1 v2 v3 v4 v5 v6
1 1 1 0 0 0
2 2 1 1 0 0
3 2 1 0 0 0
4 3 1 0 0 0
5 3 1 1 0 1
6 3 1 0 1 1
structure(list(V1 = 1:6, V2 = c(1L, 2L, 2L, 3L, 3L, 3L), V3 = c(1L,
1L, 1L, 1L, 1L, 1L), V4 = c(0L, 1L, 0L, 0L, 1L, 0L), V5 = c(0L,
0L, 0L, 0L, 0L, 1L), V6 = c(0L, 0L, 0L, 0L, 1L, 1L)), .Names = c("V1",
"V2", "V3", "V4", "V5", "V6"), class = "data.frame", row.names = c(NA,
-6L))
and I want to achieve this
v1 v2 v3 v4 v5 v6
1 1 1 0 0 0
5 2 2 1 0 0
15 3 3 1 1 2
I have tried this:
aggregate(test[c('v3', 'v4', 'v5','v6')], list('v2'), FUN=sum, na.rm=TRUE)
which is not working. I want to aggregate the data in (test) based on V2 and sum the other variables.
Change your
aggregatecommand to:Some things to note:
aggregate()or you need to be usingwith()or (not recommended)attach().