sandsky
2009-Jul-29 19:04 UTC
[R] how to skip a numeric column for averaging other columns?
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 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 Thank you in advance, Jin -- View this message in context: http://www.nabble.com/how-to-skip-a-numeric-column-for-averaging-other-columns--tp24723210p24723210.html Sent from the R help mailing list archive at Nabble.com.
Steve Lianoglou
2009-Jul-29 19:27 UTC
[R] how to skip a numeric column for averaging other columns?
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: dataI 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_idYou 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
John Kane
2009-Jul-29 19:27 UTC
[R] how to skip a numeric column for averaging other columns?
log.raw.data <- log(raw.data[,2:10]) I think but I don't understand the "Data has the first row for variable name" comment --- On Wed, 7/29/09, sandsky <realstone at hotmail.com> wrote:> From: sandsky <realstone at hotmail.com> > Subject: [R] how to skip a numeric column for averaging other columns? > To: r-help at r-project.org > Received: Wednesday, July 29, 2009, 3:01 PM > > 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 want to find an average on each column (2-10) > > > log.raw_data=log(raw_data,base=2) > Error in Math.data.frame(list(sample_id = c(1L, 2L, 3L, 4L, > 5L, 6L, 7L,? : > ? non-numeric variable in data frame: sample_id > > Thank you in advance, > > Jin > -- > View this message in context: http://www.nabble.com/how-to-skip-a-numeric-column-for-averaging-other-columns--tp24723210p24723210.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >__________________________________________________________________ Ask a question on any topic and get answers from real people. Go to Yahoo! Answers and share what yo
Ottorino-Luca Pantani
2009-Aug-19 15:54 UTC
[R] how to compute other columns without a column for sample name
sandsky ha scritto:> 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. > > >> log.raw_data=log(raw_data,base=2) >> > Error in Math.data.frame(list(sample_id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, : > non-numeric variable in data frame: sample_id > > Thank you in advance, > > Jin >You are trying to calculate a logarithm for a character variable. You most probably imported the data with read.table (....header = FALSE). try sapply(raw_data, is.numeric) to see which are the columns that contains "logarithmizable" items 8rino