gpt4 book ai didi

r - dplyr select one_of() 帮助程序返回警告?

转载 作者:行者123 更新时间:2023-12-02 20:05:04 26 4
gpt4 key购买 nike

我有两个主要数据框,其中有两组我想删除的列名称:

df1 <- structure(list(a = c(1, 2), b = c(3, 4), c = c(5, 6), d = c(7, 
8), e = c(9, 10)), row.names = c(NA, -2L), class = c("tbl_df",
"tbl", "data.frame"))

df2 <- structure(list(a = c(1, 2), b = c(3, 4), c = c(5, 6)), row.names = c(NA,
-2L), class = c("tbl_df", "tbl", "data.frame"))

我想删除以下列表中的任何列名称:“c”、“d”、“e”。

当我使用 one_of() select helper 执行此操作时,我收到警告:

> tibble(a = c(1,2), b = c(3,4), c = c(5,6)) %>% select(-one_of("c","d","e"))
# A tibble: 2 x 2
a b
<dbl> <dbl>
1 1 3
2 2 4
Warning message:
Unknown columns: `d`, `e`

较大的警告为 0。

请告知如何在不发出警告的情况下按列名称组进行过滤?如果我想忽略的列存在于 one_of() 中,则忽略它,否则保留它。

最佳答案

我希望我的回答能够解决您的问题,我使用select_if函数而不是使用select的辅助函数。如果您想了解有关 select_if 的更多信息,请在 RStudio 控制台中输入 ?select_if

tibble(a = c(1,2), b = c(3,4), c = c(5,6)) %>% select_if(colnames(.) %in% c("a","c","d","e"))

谢谢!

关于r - dplyr select one_of() 帮助程序返回警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54983094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com