An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130101/2b29bccf/attachment-0001.pl>
Order variables automatically
6 messages · Anthony Damico, Debs Majumdar, Rui Barradas
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130101/4c661023/attachment-0001.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130101/61d6b91c/attachment-0001.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130101/984f74b3/attachment-0001.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130101/7edb6097/attachment-0001.pl>
Hello, Just one more thing, you don't need an explicit test to TRUE, the two lines below are equivalent. d[ , vars.to.order==TRUE ] d[ , vars.to.order ] Rui Barradas Em 01-01-2013 12:44, Debs Majumdar escreveu:
Thanks to both of you for your help. I think I have got what I wanted.
vars.to.order <- sapply(d, FUN = function(x){length(unique(x))<=10}) ## check no. of unique values for each variable
#d[ , vars.to.order ] <- lapply( d[ , vars.to.order==TRUE ], factor) ## I didn't need this line. Is this step necessary?
d[ , vars.to.order ] <- lapply( d[ , vars.to.order==TRUE ], ordered)
Debs
________________________________
From: Debs Majumdar <debs_stata at yahoo.com>
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: Anthony Damico <ajdamico at gmail.com>; "r-help at r-project.org" <r-help at r-project.org>
Sent: Tuesday, January 1, 2013 4:14 AM
Subject: Re: [R] Order variables automatically
Yes. That's true. All the variables are read in as numeric/integers.What I am looking for at this moment is if any variable has less than equals to 10 unique values (categories) then it is a factor. Can that be incorporated?
Thanks,
Debs
________________________________
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Debs Majumdar <debs_stata at yahoo.com>
Cc: Anthony Damico <ajdamico at gmail.com>; "r-help at r-project.org" <r-help at r-project.org>
Sent: Tuesday, January 1, 2013 4:04 AM
Subject: Re: [R] Order variables automatically
Hello,
You must have a way of telling whether the variables are
categorical. If they are factors, just not ordered factors, instead
of grep the following might work.
vars.to.order <- sapply(yourdata, is.factor)
And the rest should be the same.
Hope this helps,
Rui Barradas
Em 01-01-2013 10:13, Debs Majumdar escreveu:
Sorry for not being clear. I forgot to mention that the variable labels don't really say which are categorical/continuous. They are just I1, I2,...., I459. Out of these 459 variables, most are continuous and others are categorical. So, the grep command won't work here. Thanks, Debs ________________________________ From: Anthony Damico <ajdamico at gmail.com> Cc: "r-help at r-project.org" <r-help at r-project.org> Sent: Tuesday, January 1, 2013 12:24 AM
Subject: Re: [R] Order variables automatically # create an example data frame
yourdata <-
data.frame(
cat1 = c( 1 , 0 , 1 ) ,
cont1 = c( 0 , 1 , 0 ) ,
cat2 = c( 0 , 0 , 1 )
)
# if this doesn't work for you,
# please ?dput some example data in the future :)
# figure out which variables contain the word 'cat'
vars.to.order <- grep( 'cat' , names( yourdata ) ) # convert all of those columns to factor..
yourdata[ , vars.to.order ] <- lapply( yourdata[ , vars.to.order ], factor )
# ..and then to ordered factor
yourdata[ , vars.to.order ] <- lapply( yourdata[ , vars.to.order ], ordered ) # confirm the results of the new data frame
class( yourdata ) # yourdata is a data frame.. sapply( yourdata , class ) # here's the class of each column yourdata # here's the whole data set printed to the screen [[alternative HTML version deleted]]
______________________________________________ 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.