Dear All,
I have a list of dataframes, each cell in every
dataframe (after I have cleaned up the dataframes) is
either real or NA but have class character (I think).
I would like to know how to change the class of every
cell without using a for-loop. I currently have this
dataframes <- sapply(1: no.of.subs, function(k)
apply(dataframes[[k]], 2, function(x) {
if(class(x)=="character") x <- as(x, "numeric"); x }))
but this neither changes the cells to numeric nor
keeps the dataframes in a list.
It creates one dataframe with number of rows=(no of
rows in a dataframe*no of colums) and number of
columns = no.of.subs
Can someone please help? Thanks in advance for any
help.
Dave
Datadrames don't have "cells" -- class, type, etc. is determined on a column by column basis (which is the same as a component by component basis, since datframes are also lists) Please read "An Introduction to R" before proceeding and posting so that you understand R's basic data structures and conventions. BTW -- what do you have against for loops? One should vectorize calculations whenever possible,of course, but for loops are often useful and essentially instantaneous. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Dave Evens > Sent: Thursday, May 26, 2005 9:31 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Q: changing the class of an object > > > Dear All, > > I have a list of dataframes, each cell in every > dataframe (after I have cleaned up the dataframes) is > either real or NA but have class character (I think). > I would like to know how to change the class of every > cell without using a for-loop. I currently have this > > dataframes <- sapply(1: no.of.subs, function(k) > apply(dataframes[[k]], 2, function(x) { > if(class(x)=="character") x <- as(x, "numeric"); x })) > > but this neither changes the cells to numeric nor > keeps the dataframes in a list. > > It creates one dataframe with number of rows=(no of > rows in a dataframe*no of colums) and number of > columns = no.of.subs > > Can someone please help? Thanks in advance for any > help. > > Dave > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Dave Evens wrote:> Dear All, > > I have a list of dataframes, each cell in everywhat is a cell?> dataframe (after I have cleaned up the dataframes) is > either real or NA but have class character (I think). > I would like to know how to change the class of every > cell without using a for-loop. I currently have this > > dataframes <- sapply(1: no.of.subs, function(k) > apply(dataframes[[k]], 2, function(x) { > if(class(x)=="character") x <- as(x, "numeric"); x })) > > but this neither changes the cells to numeric nor > keeps the dataframes in a list. > > It creates one dataframe with number of rows=(no of > rows in a dataframe*no of colums) and number of > columns = no.of.subs > > Can someone please help? Thanks in advance for any > help.A simple way is data.frame(sapply(X, as.numeric)) but I guess the real problem is some steps before. You should take a look why you got character vectors in your data.frame rather than numeric ones. Uwe Ligges> Dave > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html