Re: [R] Having trouble converting a dataframe of character vectors to factors

2013-02-21 Thread Lopez, Dan
Hi Bert, Thanks for drawing my attention to simplify argument and for the examples. I understand know. Thanks. Dan -Original Message- From: Bert Gunter [mailto:gunter.ber...@gene.com] Sent: Wednesday, February 20, 2013 4:25 PM To: Lopez, Dan Cc: R help (r-help@r-project.org) Subject:

Re: [R] Having trouble converting a dataframe of character vectors to factors

2013-02-21 Thread William Dunlap
scs2-data.frame(lapply(scs2, factor)) Calling data.frame() on the output of lapply() can result in changing column names and will drop attributes that the input data.frame may have had. I prefer to modify the original data.frame instead of making a new one from scratch to avoid these

Re: [R] Having trouble converting a dataframe of character vectors to factors

2013-02-21 Thread Mark Lamias
Great point, William.  I agree your approach is the one to take to preserve attributes. Thanks for following up. From: William Dunlap wdun...@tibco.com elp (r-help@r-project.org) r-help@r-project.org Sent: Thursday, February 21, 2013 11:32 AM Subject: RE:

Re: [R] Having trouble converting a dataframe of character vectors to factors

2013-02-21 Thread Lopez, Dan
Hi Bill, Great info. The problem is what was originally given to me looks like DPUT1 below (random sample of 25). This is the only format they can give me this in and the data already looks molten. So I applied reshape2::dcast which resulted in a dataframe made of character vectors; except

Re: [R] Having trouble converting a dataframe of character vectors to factors

2013-02-21 Thread William Dunlap
#So I tried I tried this which had no effect keepcols- grepl(Q1_,names(scs.c2)) levels(scs.c2[,keepcols])-list(NoResp=,NotImportant=not important,SomewhatImpt=somewhat important,Important=important,VeryImpt=very important) #then this which also failed. It coerced a bunch of

[R] Having trouble converting a dataframe of character vectors to factors

2013-02-20 Thread Lopez, Dan
R Experts, I have a dataframe made up of character vectors--these are results from survey questions. I need to convert them to factors. I tried the following which did not work: scs2-sapply(scs2,as.factor) also this didn't work: scs2-sapply(scs2,function(x) as.factor(x)) After doing either of

Re: [R] Having trouble converting a dataframe of character vectors to factors

2013-02-20 Thread Bert Gunter
Pleaser re-read ?sapply and pay particular attention to the simplify argument. The following should help explain the issues: z - data.frame(a=letters[1:3],b=letters[4:6],stringsAsFactors=FALSE) sapply(z,class) a b character character z1 - sapply(z,as.factor)

Re: [R] Having trouble converting a dataframe of character vectors to factors

2013-02-20 Thread Mark Lamias
How about this? scs2-data.frame(lapply(scs2, factor)) From: Lopez, Dan lopez...@llnl.gov To: R help (r-help@r-project.org) r-help@r-project.org Sent: Wednesday, February 20, 2013 7:09 PM Subject: [R] Having trouble converting a dataframe of character vectors