Re: [R] Problem with rowMeans()

From: Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk_at_idi.ntnu.no>
Date: Thu, 12 Jun 2008 23:05:53 +0200

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



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. Received on Thu 12 Jun 2008 - 22:50:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 Jun 2008 - 23:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive