R dplyr mutate_at accessing colnames

你离开我真会死。 提交于 2021-02-08 05:21:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!