[R] how to skip a numeric column for averaging other columns?

Steve Lianoglou mailinglist.honeypot at gmail.com
Wed Jul 29 21:27:16 CEST 2009


Hi,

On Jul 29, 2009, at 3:02 PM, sandsky wrote:

>
> Data has the first row for variable name and the first column for  
> sample
> name. I want to take "Log" for all data, but how to compute without  
> the
> first column for sample name.
>
> That is,
>
> column 1: sample ID
> column 2-10: data

I think one thing you could/should do in your case is to set your  
rownames to your (I assume) unique sample ID's:

rownames(raw_data) <- raw_data[,1]

Then just nuke that first column:

raw_data <- raw_data[,-1]

But even if you don't do that:

> I want to find an average on each column (2-10)
>
>> apply(raw_data,2,mean)
> Error in Math.data.frame(list(sample_id = c(1L, 2L, 3L, 4L, 5L, 6L,  
> 7L,  :
>  non-numeric variable in data frame: sample_id

You can just run your function over all of the columns *except* the  
first:

apply(raw_data[,-1], 2, mean)

But in this specific case, you can use the specialized colMeans  
function:

colMeans(raw_data[,-1])

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list