Argument is neither numerical nor logical: returning NA

1

Searching for an answer about the error I encountered when trying to parse data in R, I found a case very similar, but not identical to what I came across. So I decided to write my question in a systematized way.

ROUTINE in 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 "Colocm" variable, I come across the problem below.

  

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

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

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

    
asked by anonymous 16.08.2018 / 17:06

2 answers

2

Hello, notice that your variable Colocm: is like Factor w / 159 levels , ie you need to convert it to numeric format, so R will be able to understand which are numeric values and will do the calculations you need.

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

options(stringsAsFactors = FALSE)
    
16.08.2018 / 17:30
0

Aside from the suggestion of @Fernandes, which should follow, there is another problem. If R is reading values as strings there should be non-numeric values in the file. One way to solve this is, after reading the base, run

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

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

    
16.08.2018 / 18:33