简体   繁体   中英

R dplyr mutate_at accessing colnames

How could one access the column name being processed by dplyr::mutate_at ?

Let's say we would like to convert a column of a data frame into factors with levels stored in a separate list.

df <- data.frame("C1"=c("A","B","C"), "C2"=c("D","E","F"))
df
  C1 C2
1  A  D
2  B  E
3  C  F

lst <- list("C2"=c("F","E","D"), "C3"=c("G","H","I"))
lst
$C2
[1] "F" "E" "D"

$C3
[1] "G" "H" "I"

All of the following trigger error or replace all the column values by NA:

df %>%
mutate_at(vars(C2), function(x) factor(x, levels=lst$.))

df %>%
mutate_at(vars(C2), function(x) factor(x, levels=lst[[colnames(.)]]))

df %>%
mutate_at(vars(C2), function(x){col = as.name(.); factor(x, levels=lst$col))

You can use Map in base R or map2 from purrr after getting the common columns using intersect .

cols <- intersect(names(lst), names(df))
df[cols] <- Map(function(x, y) factor(x, levels = y), df[cols], lst[cols])

Or

df[cols] <- purrr::map2(df[cols], lst[cols], ~factor(.x, levels = .y))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM