change columns strings to numeric from a list

Hello,
I need to change from character to numeric a list of variables that are in a data frame.
I tried with a loop, but I failed.
Then I found and example similar to what I need, but It operates with column number and no column name. Here I will load mtcars and I will make the opposite, converting from numeric to character.

mtcars
dat=mtcars
list=c("cyl","carb","hp")
dat[, list] <- sapply(dat[, list], as.character)

The code I wrote is it ok? I need to confirm this.

What you have tried seems to work. You can also use the mutate_at() function from the dplyr package.

dat <- dplyr::mutate_at(dat, .vars = list, .funs = as.numeric)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.