Answer by Mehmet Yildirim for Coerce multiple columns to factors at once
Here is a solution if you are trying to convert multiple columns with a matching pattern in data:library(dplyr)data <- data.frame(matrix(sample(0:1, 40, replace = TRUE), 4, 10, dimnames = list(1:4,...
View ArticleAnswer by GuedesBF for Coerce multiple columns to factors at once
As of 2021 (still current in early 2023), the current tidyverse/dplyr approach would be to use across, and a <tidy-select> statement.library(dplyr)data %>% mutate(across(*<tidy-select>*,...
View ArticleAnswer by cmoshe for Coerce multiple columns to factors at once
A simple and updated solutiondata <- data %>% mutate_at(cols, list(~factor(.)))
View ArticleAnswer by John Karuitha for Coerce multiple columns to factors at once
It appears that the use of SAPPLY on a data.frame to convert variables to factors at once does not work as it produces a matrix/ array. My approach is to use LAPPLY instead, as follows.## let us create...
View ArticleAnswer by Todd for Coerce multiple columns to factors at once
Here is another tidyverse approach using the modify_at() function from the purrr package.library(purrr)# Data frame with only integer columnsdata <- data.frame(matrix(sample(1:40), 4, 10, dimnames =...
View ArticleAnswer by Kayle Sawyer for Coerce multiple columns to factors at once
Here is a data.table example. I used grep in this example because that's how I often select many columns by using partial matches to their names.library(data.table)data <-...
View ArticleAnswer by neves for Coerce multiple columns to factors at once
You can use mutate_if (dplyr):For example, coerce integer in factor:mydata=structure(list(a = 1:10, b = 1:10, c = c("a", "a", "b", "b", "c", "c", "c", "c", "c", "c")), row.names = c(NA, -10L), class =...
View ArticleAnswer by user9333657 for Coerce multiple columns to factors at once
If you have another objective of getting in values from the table then using them to be converted, you can try the following way### pre processingind <- bigm.train[,lapply(.SD,is.character)]ind...
View ArticleAnswer by Janna Maas for Coerce multiple columns to factors at once
and, for completeness and with regards to this question asking about changing string columns only, there's mutate_if:data <- cbind(stringVar = sample(c("foo","bar"),10,replace=TRUE),...
View ArticleAnswer by Yun Ching for Coerce multiple columns to factors at once
The more recent tidyverse way is to use the mutate_at function:library(tidyverse)library(magrittr)set.seed(88)data <- data.frame(matrix(sample(1:40), 4, 10, dimnames = list(1:4, LETTERS[1:10])))cols...
View ArticleAnswer by akrun for Coerce multiple columns to factors at once
Here is an option using dplyr. The %<>% operator from magrittr update the lhs object with the resulting value.library(magrittr)library(dplyr)cols <- c("A", "C", "D", "H")data %<>%...
View ArticleAnswer by Rich Scriven for Coerce multiple columns to factors at once
Choose some columns to coerce to factors:cols <- c("A", "C", "D", "H")Use lapply() to coerce and replace the chosen columns:data[cols] <- lapply(data[cols], factor) ## as.factor() could also be...
View ArticleCoerce multiple columns to factors at once
I have a sample data frame like below:data <- data.frame(matrix(sample(1:40), 4, 10, dimnames = list(1:4, LETTERS[1:10])))I want to know how can I select multiple columns and convert them together...
View Article