I have a multi-column dataframe. How do I calculate the average of one of the variables based on the values of another variable? I have the frequency of several species found in 4 campaigns and I want to calculate the average of each recorded species. For this I must add the frequencies observed by the number of campaigns performed in each place, but the function I used
dadomean = dcast(dados, local ~ especie, mean)
calculates the average based on only the campaigns the species was registered and does not use the data where the record was 0. as well as the
dadomean = dados %>%
group_by(local, especie) %>%
summarise(mean(frequencia))
I've also tried
dadomean = dcast(dados, local ~ especie, mean, subset = .(campanha == 4)))
but did not accept the function and gave this error
Error in. (campaign == 4): could not find function "."
I also tried the following and it did not work.
dadomean = dcast(dados, local ~ especie, mean, na.rm=TRUE, margins = "campanha")
And also always has NA
for those places where it was meant to be 0
and could not convert to 0
.
campanha local especie frequencia
1 A aa 1
1 A bb 2
1 A cc 1
1 B bb 1
1 B dd 7
2 A aa 50
2 A bb 1
2 A dd 8
3 A aa 2
3 B aa 3
3 B dd 3
4 A aa 33
4 A bb 5
4 A cc 1
4 A dd 1
4 B aa 18
4 B bb 10
4 B dd 6