[R] Problem with rowMeans()

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Thu Jun 12 23:05:53 CEST 2008


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
>
> 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.
>
> 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
>
>   
sure, at least the first two columns are not numeric

> Can anybody suggest me how to get around this?
>
>   
you can compute row means based on only those columns which are numeric
as follows:

a = rowMeans(data[sapply(data, is.numeric)])

what you do with NAs is another story.

vQ



More information about the R-help mailing list