Question :
I have a multi-column dataframe. How do I add the values of a column within an element of another variable?
I want to do this to summarize the data of each species within each campaign.
I have already tried using the summarize function of the plyr package but it did not work. It may be because I’ve factored in the function incorrectly.
campanha especie frequencia
1 A 2
1 A 1
1 A 3
1 A 5
1 B 1
1 B 2
1 B 1
1 B 6
1 B 1
1 C 3
1 C 1
1 C 8
1 C 4
2 A 2
2 A 8
2 A 4
2 A 5
2 B 4
2 B 2
2 B 6
2 B 1
2 C 3
2 C 1
2 C 9
Answer :
Another way to do this is with the dplyr
package:
library(dplyr)
dados %>%
group_by(campanha, especie) %>%
summarise(sum(frequencia))
# A tibble: 6 x 3
# Groups: campanha [?]
campanha especie 'sum(frequencia)'
<int> <fct> <int>
1 1 A 11
2 1 B 11
3 1 C 16
4 2 A 19
5 2 B 13
6 2 C 13
Note that I bundled the data with the group_by
function and indicating the collation variables. I then used summarise
to indicate that I would like to add the frequencia
variable within the created groupings.
This can be solved with the aggregate
function.
res <- aggregate(frequencia ~ campanha + especie, dados, sum)
res
# campanha especie frequencia
#1 1 A 11
#2 2 A 19
#3 1 B 11
#4 2 B 13
#5 1 C 16
#6 2 C 13
Data in dput () format.
dados <-
structure(list(campanha = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L),
especie = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L
), .Label = c("A", "B", "C"), class = "factor"), frequencia = c(2L,
1L, 3L, 5L, 1L, 2L, 1L, 6L, 1L, 3L, 1L, 8L, 4L, 2L, 8L, 4L,
5L, 4L, 2L, 6L, 1L, 3L, 1L, 9L)), .Names = c("campanha",
"especie", "frequencia"), class = "data.frame", row.names = c(NA,
-24L))