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@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]]


