eric s
eric s

Reputation: 237

R factor all columns containing string

I have many dataframe columns I wish to convert to factors without factoring each separate field. How can iterate over the column names and if any match a regular expression convert them to factors? I am coming from the Python world, and I don't yet understand looping and string substitutions in R.

Pseudo code:

    for name in df.columns.names:
        if name matches "regex":
            df$name <- factor(df$name)

Upvotes: 1

Views: 503

Answers (2)

akrun
akrun

Reputation: 887501

We can use type.convert

df[] <- lapply(df, function(x) type.convert(as.character(x)))

Upvotes: 0

IRTFM
IRTFM

Reputation: 263411

Probably:

 df[  , grepl('regex' , names(df)) ] <- 
                                   lapply( df[  , grepl('regex' , names(df)) ], factor)

Could also use grep in this case. The j argument of [ and [<- can take either logical or numeric arguments.

Upvotes: 1

Related Questions