[R] Problem with rowMeans()

Erik Iverson iverson at biostat.wisc.edu
Thu Jun 12 23:18:00 CEST 2008



ss wrote:
> Dear Erik,
> 
> Thanks! The 'data' is matrix but all(apply(data[,3:85], 2, class) == 
> "numeric")
> is false.
> 
>  > class(data)
> [1] "matrix"
>  > a<- rowMeans(data[,3:85],na.rm = TRUE)
> Error in rowMeans(data[, 3:85], na.rm = TRUE) : 'x' must be numeric
>  > all(apply(data[,3:85], 2, class) == "numeric")
> [1] FALSE
>  >
> 
> What else should I do?

try str(data)   , do you have a character matrix?  That would explain 
the error message.

I would consider storing your data in a data.frame, as you appear not to 
have homogeneous types, and then your analysis should be trivial.

Erik

> 
> I appreciate!
> 
> Allen
> 
> 
> 
> On Thu, Jun 12, 2008 at 4:55 PM, Erik Iverson <iverson at biostat.wisc.edu 
> <mailto:iverson at biostat.wisc.edu>> wrote:
> 
>     Hello -
> 
> 
>     ss wrote:
> 
>         Hi all,
> 
>         I have a matrix called 'data', which looks like:
> 
>             data[1:4,1:4]
> 
>              Probe_ID       Gene_Symbol  M1601           M1602
>         1 A_23_P105862    13CDNA73            -1.6            0.16
>         2  A_23_P76435      15E1.2            0.18            0.59
>         3 A_24_P402115      15E1.2            1.63           -0.62
>         4 A_32_P227764      15E1.2           -0.76           -0.42
> 
>             dim(data)
> 
>         [1] 23963    85
> 
> 
>     Do you really have a matrix, or a data.frame?
> 
>     Try
> 
>      > class(data)
> 
> 
> 
>         What I want to do is to make a new matrix called 'data2', which
>         would be
>         transformed
>         by subtracting the mean of each row from matrix 'data'. There
>         are some 'NA's
>         in the
>         matrix and I do want to keep it.
> 
> 
>     See ?scale
> 
> 
> 
>         I tried to take 'mean's from each row first by using:
> 
>         a<- rowMeans(data[,3:85],na.rm = FALSE)
> 
>         but I got:
> 
>             a<- rowMeans(data[,3:85],na.rm = FALSE)
> 
>         Error in rowMeans(data[, 3:85], na.rm = FALSE) : 'x' must be numeric
> 
>         Can anybody suggest me how to get around this?
> 
> 
>     Figure out what you are giving the rowMeans function.
>     If you really have a matrix, then
> 
>     all(apply(data[,3:85], 2, class) == "numeric") should be TRUE.
> 
> 
> 
> 
>         Thank you very much!
> 
>         Allen
> 
>                [[alternative HTML version deleted]]
> 
>         ______________________________________________
>         R-help at r-project.org <mailto:R-help at r-project.org> mailing list
>         https://stat.ethz.ch/mailman/listinfo/r-help
>         PLEASE do read the posting guide
>         http://www.R-project.org/posting-guide.html
>         and provide commented, minimal, self-contained, reproducible code.
> 
>



More information about the R-help mailing list