I've made a mistake, I should have used 'lapply' not 'sapply'
DF <- make.df()
str(DF)
DF[, ix] <- lapply(DF[, ix], as.factor)
str(DF)# crast1: Factor w/ 4 levels
I should have looked further...
Rui Barradas
Em 01-06-2012 10:10, Rui Barradas escreveu:
Hello,
Sorry, I've just answered to
Hello,
Sorry, I've just answered to Johannes without Cc to r-help.
Repeat:
See the difference.
# helper function
make.df <- function(){
x <- as.data.frame(matrix(1:24, ncol=6))
names(x) <- c(paste0("crast", 1:2), "A", "B", paste0("crast", 5:6))
x
}
DF <- make.df()
(ix <- grep("^cr
Hi,
I have a dataframe with around 100 columns. Now I want
to redefine some of the columns as factors (using as.factor).
Luckily all the names of the columns I want to redefine start with
"crast". Thus I thought I can use grep() for that purpose...
...I found an example for redefining a single co
3 matches
Mail list logo