1
I have a multi-column dataframe. How do I calculate the average of one variable 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 species recorded. For this I must add the frequencies observed by the number of campaigns performed at each location, but the function I used
dadomean = dcast(dados, local ~ especie, mean)
calculates the average based only on the campaigns that the species was recorded and does not use the data in which the record was 0. as well as the function
dadomean = dados %>%
group_by(local, especie) %>%
summarise(mean(frequencia))
I also tried to
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 didn’t work.
dadomean = dcast(dados, local ~ especie, mean, na.rm=TRUE, margins = "campanha")
And also always has NA
to those places where it was to be 0
and I couldn’t turn into 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
Unfortunately it didn’t work. The values doubled
– Jussara
But didn’t you want for each species in each location? If it is only by species and indifferent location, just remove the local variable from the formula.
– Thiago Fernandes