4
votes

I am trying to get the mean of the data read from an excel sheet.

library(readxl)
URL <- "C:/Users/user/Documents/R/test.xlsx"
pivot <- read_excel(URL,sheet=2)
pivot
mean(pivot$Experimental)

Data in excel below. This data is shown on the console window when executing the code above.

 Experimental   Comparison
   -0.255      -0.324
   -0.213      -0.185
   -0.190      -0.299
   -0.185      -0.144
   -0.045      -0.027
   -0.025      -0.039
   -0.015      -0.264
    0.003      -0.077
    0.015      -0.017
    0.020      -0.169
    0.023      -0.096
    0.040      -0.330
    0.040      -0.346
    0.050      -0.191
    0.055      -0.128
    0.058      -0.182  

I am getting the below warning message and result of the mean function is NA.

Warning message:
In mean.default(pivot$Experimental) :
  argument is not numeric or logical: returning NA
1
Show us str(pivot), i.e. edit your question!jogo
Very likely Experimental column is factor for some reason. Coerce them through pivot$Experimental<-as.numeric(as.character(pivot$Experimental))nicola
@jogo chr " -0.255" etc etc result of str(pivot)Ayubx
@nicola There is a warning NAs introduced by coercion Ayubx
@Ayubx Why you don't just post the output of dput(pivot) to let us see what it actually contains? Every question should be reproducible, you won't receive any help if you don't provide the relevant information.nicola

1 Answers

4
votes

@Ayubx The question @nicola is posing to you is how do you take the mean of characters? You can't, so you need to convert the characters to numeric. The below is an example.

> d <- c("5","7")
> str(d)
chr [1:2] "5" "7"
> e <- as.numeric(d)
> str(e)
num [1:2] 5 7
> mean(d)
[1] NA
Warning message:
In mean.default(d) : argument is not numeric or logical: returning NA
> mean(e)
[1] 6