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 <ajdam...@gmail.com> > > Cc: "r-help@r-project.org" <r-help@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@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. [[alternative HTML version deleted]] ______________________________________________ R-help@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.