2

在具有 NA 的数据集中创建因子级别适用于单个列,但我需要遍历更多列(均以“影响”开头)并且在 dplyr mutate(across) 中遇到了问题

我究竟做错了什么?

下面的代表

library(tribble)
library(dplyr)

df <- tribble(~id, ~tumour, ~impact.chemo, ~impact.radio,
        1,'lung',NA,1,
        2,'lung',1,NA,
        3,'lung',2,3,
        4,'meso',3,4,
        5,'lung',4,5)

# Factor labels
trt_labels <- c('Planned', 'Modified', 'Interrupted', 'Deferred', "Omitted")

# Such that factor levels match labels as, retaining NAs where present:
data.frame(level = 1:5,
           label = trt_labels)

# Create factor works for individual columns
factor(df$impact.chemo, levels = 1:5, labels = trt_labels)
factor(df$impact.radio, levels = 1:5, labels = trt_labels)

# But fails inside mutate(across)
df %>% 
  mutate(across(.cols = starts_with('impact'), ~factor(levels = 1:5, labels = trt_labels)))
4

1 回答 1

1

只是让@27φ9 的评论成为答案:purrr您在内部指定的 -style lambda 函数across不正确,因为它需要第一个参数,这是函数应该引用的对象(在这种情况下,由 选择的数据框列across)。

要解决您的问题,您应该.x在 lambda 函数中插入,这只是一个快捷方式function(x) x- 请参阅此页面purrr以获取有关-style lambda 函数的更多信息。

df %>% 
  mutate(across(.cols = starts_with('impact'), ~factor(.x, levels = 1:5, labels = trt_labels)))

# A tibble: 5 x 4
#      id tumour impact.chemo impact.radio
#   <dbl> <chr>  <fct>        <fct>       
# 1     1 lung   NA           Planned     
# 2     2 lung   Planned      NA          
# 3     3 lung   Modified     Interrupted 
# 4     4 meso   Interrupted  Deferred    
# 5     5 lung   Deferred     Omitted
于 2020-07-03T07:28:56.857 回答