In tidyverse, I want to convert several columns to numeric, is there a way to do it in…
- User
In tidyverse, I want to convert several columns to numeric, is there a way to do it in one pass?
Assistant
Yes, you can convert multiple columns to numeric in one pass using the
mutate_at()function from thetidyversepackage. Here's an example:rlibrary(tidyverse) df <- data.frame( id = c(1, 2, 3), x = c("1.2", "3.4", "5.6"), y = c("7.8", "9.0", "10.2"), z = c("11.4", "12.6", "13.8") ) # Convert columns x, y, and z to numeric in one pass df <- df %>% mutate_at(vars(x, y, z), as.numeric)In this example, the
mutate_at()function is used to apply theas.numeric()function to the columnsx,y, andz. The function is used to select the columns to be converted.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.