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

后端 未结 2 1162
萌比男神i
萌比男神i 2021-02-15 20:28

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 a

2条回答
  •  北海茫月
    2021-02-15 21:00

    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!

提交回复
热议问题