问题
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))
回答1:
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))
来源:https://stackoverflow.com/questions/63016193/r-dplyr-mutate-at-accessing-colnames