Applying the same function to multiple columns

Sometimes you want to change the data type, and not just one column, but many columns. For example, imagine you want to change the data types from Character to Factor or Date / Time.

You can use mutate_at or mutate_if to do this type of operation.

The command below will convert all the ‘character’ data type columns to ‘factor’ data type.

mutate_if(is.character, factor)

The command below will apply ‘ymd’ function to all the columns whose names end with ‘date’.

mutate_at(vars(ends_with("date")), funs(ymd(., tz = GMT)))

In Exploratory, you can run these commands in the command line mode, by clicking on ‘Command’ button.

Here’s an related video.

And here is a blog post that I talked about these functions some time back.

They are bit old though… ;(