Argument is neither numerical nor logical: returning NA

Asked

Viewed 1,914 times

1

Searching for answers about the mistake I came across while trying to analyze data in the R, I found a very similar case, but not identical to the one I came across. So I decided to write my question in a systematized way.

ROUTINE IN THE R:

# pr é o nome do arquivo de dados, em .txt 
> str(pr)
'data.frame':   280 obs. of  11 variables:
 $ Trat  : Factor w/ 17 levels "ta","tb","tb ",..: 1 1 1 1 1 3 2 3 3 2 ...
 $ Colocm: Factor w/ 159 levels "0.168","0.173",..: 158 53 34 57 38 53 13 2 8 24 ...
 $ Compcm: Factor w/ 73 levels "10","10.1","10.2",..: 73 30 29 38 42 30 31 36 26 35 ...
 $ NF    : int  7 4 7 2 11 4 6 5 4 7 ...
 $ CRcm  : Factor w/ 99 levels "10","10.2","10.5",..: 39 56 45 68 59 55 78 82 73 57 ...
 $ MFR   : Factor w/ 205 levels "1.34","1.57",..: 153 32 15 54 177 32 126 22 80 85 ...
 $ MFCg  : Factor w/ 52 levels "0.13","0.14",..: 29 1 1 27 16 1 18 6 1 9 ...
 $ MFFg  : Factor w/ 98 levels "0.0436","0.12",..: 83 28 47 11 58 28 54 21 40 27 ...
 $ MSRg  : Factor w/ 117 levels "0.3","0.32","0.34",..: 108 18 47 107 87 51 88 23 58 62 ...
 $ MSCg  : Factor w/ 178 levels "0.00854","0.00986",..: 169 11 131 84 14 12 102 19 9 31 ...
 $ MSFg  : Factor w/ 102 levels "0.0086","0.01206",..: 95 73 52 80 59 69 80 59 78 64 ...

PROBLEM: When trying to examine the average of the variable "Colocm" I come across the problem below.

Mean(pr$Colocm) # arithmetic mean of the neck [1] NA Warning message: In Mean.default(pr$Colocm) : argument is neither numerical nor logical: returning NA

COMMENT: Since there are ten variables and I need to do the exploratory analysis of them, I need to understand where I am going wrong.

I am interested in adopting R as a tool for analyzing experimental data, notably in the agronomic area, but the lack of experience in using R has taken a long time with the many errors that arise when trying to use this tool for data analysis.

2 answers

2


Hello, note that your variable $Colocm: is as Factor w/ 159 levels, that is you need to convert it to numerical format, so R will be able to understand that they are numerical values and will make the calculations you need.

Before reading your . txt put this command below, this will prevent R from creating the levels in your BD.

options(stringsAsFactors = FALSE)
  • Dear Fernando, I will follow your guidance. Gratitude for your help and attention.

1

In addition to the @Fernandes suggestion, which you should follow, there is another problem. If R is reading values as strings there should be non-numerical values in the file. One way to solve this is, after reading the database, to run

pr[-c(1, 4)] <- lapply(pr[-c(1, 4)], function(x) as.numeric(as.character(x)))

This works even if the columns are class factor.
Then you can inspect the class columns numeric to see if there are values NA.
If there is, it may also be useful to inspect the file and see what values are causing problems. It is possible to get rid of them immediately in reading with the argument na.strings. See the page help("read.table") to see how to use na.strings.

  • Dear Rui, thank you so much for the tip!

  • Rui! Your suggestion worked, in the case of my comic. What was previously a factor, went to Numeric and so I could follow the exploratory analysis. Gratitude.

Browser other questions tagged

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