Coerce Multiple Columns to Factors At Once

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 used

Check the result:

sapply(data, class)
# A B C D E F G
# "factor" "integer" "factor" "factor" "integer" "integer" "integer"
# H I J
# "factor" "integer" "integer"

How to Coerce multiple columns to factors at once for H2OFrame object?

The right way to do it is to use the H2OFrame apply() function, however, this produces the same error that @MKR mentioned. I have created a JIRA ticket here.

In theory, this should work:

data.hex[,cols] <- apply(X = data.hex[,cols], MARGIN = 2, FUN = as.factor)

For now, the workaround is:

for (col in cols) {
data.hex[col] <- as.factor(data.hex[col])
}

Convert multiple columns to factor and give them numerical values

We can use mutate with across

df <- df %>% 
mutate(across(contains('growth'), ~ ordered(.,
levels = c("NG", "SG", "LG", "MG", "HG"),
labels = c('0', '2.5', '12', '40', '100'))))

Or with lapply in base R

nm1 <- grep('growth', names(df), value = TRUE)
df[nm1] <- lapply(df[nm1], function(x) ordered(x,
levels = c("NG", "SG", "LG", "MG", "HG"),
labels = c('0', '2.5', '12', '40', '100')))

Or this can be also done with ftransform (ftransformv - for multiple columns) from collapse

library(collapse)
f1 <- function(x) {
ordered(x, levels = c("NG", "SG", "LG", "MG", "HG"),
labels = c('0', '2.5', '12', '40', '100'))
}

i1 <- grep('growth', names(df))
ftransformv(df, i1, f1)

-output

#   ABC_growth ZFG_growth
#1 40 <NA>
#2 40 <NA>
#3 40 <NA>
#4 40 <NA>
#5 40 <NA>
#6 12 12
#7 12 12
#8 12 12
#9 12 12
#10 12 12
#11 0 2.5
#12 0 2.5
#13 0 2.5
#14 0 2.5
#15 0 2.5

How to coerce multiple columns for multiple data.frames as character in R?

You must coerce the columns of each data.frame to character one by one.

lapply(target, function(x) {
x[] <- lapply(x, as.character)
x
}) -> target

df <- do.call(rbind, target)

R: Converting multiple binary columns into one factor variable whose factors are binary columns

I am guessing that you want to revert a "one-hot encoding" of a variable. Here is a quick way to do it.

apply(df ,1,\(x) names(which(x == "yes"))) |>
purrr::map_chr(~ifelse(length(.x) == 0, NA_character_, .x))

#+ [1] "v1" "v1" "v3" "v1" "v1" "v2" "v1" "v2" "v3" NA

A tidyverse approach would be:

df |>
mutate(ID = row_number()) |>
pivot_longer(cols = c(v1,v2,v3), names_to = "var") |>
filter(value == "yes")

##> ID var value
##> <int> <chr> <chr>
##> 1 1 v1 yes
##> 2 2 v1 yes
##> 3 3 v3 yes
##> 4 4 v1 yes
##> 5 5 v1 yes
##> 6 6 v2 yes
##> 7 7 v1 yes
##> 8 8 v2 yes
##> 9 9 v3 yes

For loop to convert multiple columns to factors in R

The syntax showed also use the python for loop and python list. Instead it would be a vector of strings in `R

for (col in c('col1','col2')) {
df[[col]] <- factor(df[[col]])
}

NOTE: here we use [[ instead of $ and the braces {}. The factor can be directly applied instead of as.character wrapping


Or with lapply where it can be done easily (without using any packages)

df[c('col1', 'col2')] <- lapply(df[c('col1', 'col2')], factor)

Or in dplyr, where it can be done more easily

library(dplyr)
df <- df %>%
mutate_at(vars(col1, col2), factor)

Coercing multiple time-series columns to factors in large dataframe

As you write, that you need to convert (all?) character variables to factors, you could use mutate_if from dplyr

library(dplyr)
mutate_if(df, is.character, as.factor)

With this you only operate on columns for which is.character returns TRUE, so you don't need to worry about the column positions or names.

R -apply- convert many columns from numeric to factor

Try

df[,cols] <- lapply(df[,cols],as.factor)

The problem is that apply() tries to bind the results into a matrix, which results in coercing the columns to character:

class(apply(df[,cols], 2, as.factor))  ## matrix
class(as.factor(df[,1])) ## factor

In contrast, lapply() operates on elements of lists.



Related Topics



Leave a reply



Submit