Dplyr 使用相同的函数和命名约定对几个不同的子集进行变异

问题描述 投票:0回答:1

假设我有一个非常宽的数据框和一些命名子集:

df <-  data.frame(matrix(ncol = 200, nrow = 200))
df <- as.data.frame(
                    apply(df, 1, function(x){return(sample(c(1,2,NA), size = 200, replace = TRUE))})
                    )

xHappy <- c("V22", "V39", "V17", "V2")
xSad <- c("V15", "V11", "V79", "V90", "V80", "V101")
xSilly <- c("V20", "V112")

对于每个子集,我想创建一个新变量,它是每行该子集中缺少的数字。

我可以将相同的方法复制粘贴三次:

df.new <- df %>% mutate(
                        missingHappy = rowSums(is.na(select(., all_of(xHappy))
                        missingSad = rowSums(is.na(select(., all_of(xSad))
                        missingSilly = rowSums(is.na(select(., all_of(xSilly))
                        )

但是我的 IRL 问题涉及数千个变量和数十个子集,因此这既乏味又危险,我真的更喜欢更简洁的东西。

有没有一种方法可以让我只创建一个保存的 mutate 方法,而我所要做的就是调用类似

makeNAColumns(xSad, xHappy, XSilly)
甚至
df %>% mutate [insert magic one liner]
之类的东西?感谢您的任何帮助。

r dplyr tidyverse mutate
1个回答
0
投票

使用

purrr
包,你可以做类似的事情:

library(dplyr)
library(purrr)

# Create list of your subset columns and define output column names
subs <- list(
  missingHappy = xHappy,
  missingSad = xSad,
  missingSilly = xSilly
)

# Apply rowSums across each list in subs, bind_cols() to output
df.new <- df |>
  bind_cols(
    map2_dfc(subs, names(subs),
             ~ rowSums(is.na(select(df, all_of(.x))))
  ))                                            

                                                        
head(df.new[,c("V22", "V39", "V17", "V2","V15",
               "V11", "V79", "V90", "V80", "V101",
               "V20", "V112", "missingHappy", "missingSad","missingSilly")])

#   V22 V39 V17 V2 V15 V11 V79 V90 V80 V101 V20 V112 missingHappy missingSad missingSilly
# 1   1   2   1  2   2   1   2   2   2    2   2    1            0          0            0
# 2  NA  NA  NA  2   1   2  NA   2  NA    1   1    2            3          2            0
# 3   2  NA  NA NA   1  NA   2  NA   1    1  NA    1            3          2            1
# 4   2  NA   2  1   2  NA   2   2   2    2   1    1            1          1            0
# 5   2   1  NA NA  NA   1   1   2   1    2  NA   NA            2          1            2
# 6   2   1   1  1   1   2   1   1  NA    1   1    2            0          1            0

创建 subs 对象仍然很费力,但我希望有一种更有效的方法来实现列命名。

© www.soinside.com 2019 - 2024. All rights reserved.