________________________________
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.