How to create a chart of averages and standard deviation in a data set that includes missing values (Nas)

Asked

Viewed 2,020 times

2

Hello!

I work with bioacoustics and I’m trying to create a graph of averages per species using the lineplot.CI function of the sciplot package, but I can’t make R ignore the missing values (NA). When I graph for note durations, everything is fine because the variable Deltatime does not inlui Nas:

library(sciplot)
# Note duration
lineplot.CI(dados$especie, dados$DeltaTime, las=1, type="p",
        xlab="Espécie", ylab="Duração nota", main="Duração nota",
        ci.fun= function(x) c(mean(x)-sd(x), mean(x)+sd(x)))

Duração da nota por espécie

When I try to use the same function for the Peakfreq variable, on the other hand, standard deviation bars only appear for the species that has no NA:

lineplot.CI(dados$especie, dados$PeakFreq, las=1, type="p",
        xlab="Espécie", ylab="Frequência dominante", main="Frequência 
        dominante",
        ci.fun= function(x) c(mean(x)-sd(x), mean(x)+sd(x)))  

inserir a descrição da imagem aqui

I tried to change the function in several ways and searched a lot on the internet, but I was not successful. Any idea how to solve this problem?

Obs.: The function has no problem ignoring Nas when we consider only the standard error

1 answer

1


You have to explicitly ask for the functions mean() and sd() ignore the NA, passing the argument na.rm = TRUE:

lineplot.CI(dados$especie, dados$DeltaTime, las=1, type="p",
            xlab="Espécie", ylab="Duração nota", main="Duração nota",
            ci.fun= function(x) c(mean(x, na.rm = TRUE)-sd(x, na.rm = TRUE), 
                                  mean(x, na.rm = TRUE)+sd(x, na.rm = TRUE)))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.