mean( ,na.rm=TRUE) still returns NA
Asked Answered
E

2

9

I'm very new to R (moving over from SPSS). I'm using RStudio on a Mac running Mavericks. Please answer my question in words of 2 syllables as this is my first real attempt at anything like this. I've worked through some basic tutorials and can make things work on all the sample data.

I have a data set with 64,000-ish rows and about 20 columns. I want to get the mean of the variable "hold_time", but whatever I try I get either NA or NA and a warning message

I have tried all of the following:

> summary(data_Apr_Jun$hold_time,na.rm=TRUE)
      5       6       7       4       8       2       1       3      10 
   9596    9191    3192    1346    1145     977     940     655     534 
     11       9      12       0      13      15      14      16      17 
    490     444     249     128     106      86      73      68      40 
     98     118     121     128     125      97     101     188      86 
     31      29      28      28      27      27      26      26      26 
    102     105     113      81     119     139     127     134     152 
     25      25      25      25      24      24      23      23      23 
     18      69      96     106     110     111     120     190      76 
     23      23      23      22      22      22      22      22      22 
     82     132     135     156     166      94     115     116     117 
     22      21      21      21      21      21      20      20      20 
    142     153     165      19      93     100     104     112     126 
     20      20      20      20      20      19      19      19      19 
    131     138     143     157     177     189      61      87     103 
     19      19      19      19      19      19      19      19      18 
    108     148     176     212      54      56      64      74      79 
     18      18      18      18      18      18      18      18      18 
     99     107     129     163     168     171     178     226     236 
     18      17      17      17      17      17      17      17      17 
     59      71      78      95     114     122     123     130 (Other) 
     17      17      17      17      16      16      16      16    2739 
   NA's 
  29807 
> mean(as.numeric(data_Apr_Jun$hold_time,NA.rm=TRUE))
[1] NA
> data_Apr_Jun$hold_time[data_Apr_Jun$hold_time=="NA"]<-0
> mean(as.numeric(data_Apr_Jun$hold_time))
[1] NA
> mean(data_Apr_Jun$hold_time)
[1] NA
Warning message:
In mean.default(data_Apr_Jun$hold_time) :
  argument is not numeric or logical: returning NA
> mean(as.numeric(data_Apr_Jun$hold_time,na.rm=TRUE))
[1] NA
> colMeans(data_Apr_Jun$hold_time)
Error in colMeans(data_Apr_Jun$hold_time) : 
  'x' must be an array of at least two dimensions
> colMeans(data_Apr_Jun)
Error in colMeans(data_Apr_Jun) : 'x' must be numeric
> mean(data_Apr_Jun$hold_time,na.omit)
[1] NA
Warning message:
In mean.default(data_Apr_Jun$hold_time, na.omit) :
  argument is not numeric or logical: returning NA

So even though I am removing the NAs they don't seem to be being removed. I am flummoxed.

Elastin answered 13/8, 2014 at 23:13 Comment(4)
mean has an parameter na.rm. It does not have an argument NA.rm. R is case sensitive.Procarp
Perhaps you should look at the structure of data before coercing to numeric or any other forms. Output from str(data_Apr_Jun$hold_time) will help. BTW it should be na.rm not NA.rm in the first call.Pfennig
Try to fix the question header and be more clear as for what you're trying to achiveAmmon
parameter, argument, sensitive, and coercing all have > 2 syllables, alex brown and won782Jaggery
L
18

Hello Rnovice unfortunatly there are several errors... Lets resolve them one by one:

> mean(as.numeric(data_Apr_Jun$hold_time,NA.rm=TRUE))
[1] NA

This is because you use na.rm in a wrong manner: it should be

mean(as.numeric(data_Apr_Jun$hold_time),na.rm=TRUE)
  1. na.rm is an argument of mean, not of as.numeric (caution with the brackets)
  2. is na.rm R is case sensitive

==================================================================================

> data_Apr_Jun$hold_time[data_Apr_Jun$hold_time=="NA"]<-0

R does not allow comparison with NA as i pointed our here: Something weird about returning NAs
What you mean is

data_Apr_Jun$hold_time[which(is.na(data_Apr_Jun$hold_time))] <- 0

One more remark =="NA" is comparing with a string "NA". Try is.na("NA") and is.na(NA) to see the difference.

==================================================================================

colMeans(data_Apr_Jun$hold_time)
Error in colMeans(data_Apr_Jun$hold_time) : 
  'x' must be an array of at least two dimensions

try data_Apr_Jun$hold_time and you will see, that it returns a vector. This is why a colwise mean (computed by colMeans) makes no sence.

Hope the rest is understandable/solveable with these hints. One very importent thing that you already realized:
Use R! you are on the right track!

Legault answered 13/8, 2014 at 23:20 Comment(2)
If data_Apr_Jun$hold_time is a factor just converting with as.numeric may lead to wrong answersIrita
as user20650 pointed out, have a look at: #3418628Legault
L
3

unfortunately, as.numeric makes an implicit coercion, which leads to wrong answers. don't imply it on factors.

Luisluisa answered 29/5, 2017 at 23:58 Comment(1)
This is a good point; the other answer didn't mention that the variable hold_time appears to be a factor, and using as.numeric on a factor won't give the intended result.Gleeman

© 2022 - 2024 — McMap. All rights reserved.