Getting "NA" when starting standard deviation

Quick question. I read my csv file in the data variable. It has a var column label that has numeric values.

When i run the command

 sd(data$var) 

I get

 [1] NA 

instead of my standard deviation.

Could you help me figure out what I'm doing wrong?

+7
source share
2 answers

Try sd(data$var, na.rm=TRUE) and then any NA in the var column will be ignored. It will also pay to check your data, to make sure that NA must be NA, and no errors were read there, commands such as head(data) , tail(data) and str(data) should help with this.

+22
source

You probably lost the values ​​in var , or the column is not numeric, or there is only one row.

Try to remove missing values ​​that will help in the first case:

 sd(dat$var, na.rm = TRUE) 

If this does not work, make sure that

 class(dat$var) 

is "numerical" (second case) and that

 nrow(dat) 

greater than 1 (third case).

Finally, data is a function in R, so it's better to use a different name, which I made here.

+5
source

All Articles