gpt4 book ai didi

r - 你能列出一个异常(exception) tidyselect `everything()`

转载 作者:行者123 更新时间:2023-12-04 15:39:17 24 4
gpt4 key购买 nike

library(tidyverse)
iris %>% as_tibble() %>% select(everything())

#> # A tibble: 150 x 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <fct>
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
#> 7 4.6 3.4 1.4 0.3 setosa
#> 8 5 3.4 1.5 0.2 setosa
#> 9 4.4 2.9 1.4 0.2 setosa
#> 10 4.9 3.1 1.5 0.1 setosa
#> # ... with 140 more rows

假设我想选择 iris 中的所有内容数据框除外 Species .我如何在使用 tidyselect::everything() 时列出这个异常?

我的实际管道在下面,什么时候
... %>% 
group_by(`ID`) %>%
fill(everything, .direction = "updown") %>%
... %>%

我收到以下错误:

Error: Column ID can't be modified because it's a grouping variable

最佳答案

你会做

iris %>% as_tibble() %>% select(-Species)

但假设你有充分的理由不想要,这里有一种使用 everything() 的方法
iris %>% as_tibble() %>% select(setdiff(everything(), one_of("Species")))
#> # A tibble: 150 x 4
#> Sepal.Length Sepal.Width Petal.Length Petal.Width
#> <dbl> <dbl> <dbl> <dbl>
#> 1 5.1 3.5 1.4 0.2
#> 2 4.9 3 1.4 0.2
#> 3 4.7 3.2 1.3 0.2
#> 4 4.6 3.1 1.5 0.2
#> 5 5 3.6 1.4 0.2
#> 6 5.4 3.9 1.7 0.4
#> 7 4.6 3.4 1.4 0.3
#> 8 5 3.4 1.5 0.2
#> 9 4.4 2.9 1.4 0.2
#> 10 4.9 3.1 1.5 0.1
#> # ... with 140 more rows

(或者只是 iris %>% as_tibble() %>% select(setdiff(everything(), 5)) 如果可以接受)

关于r - 你能列出一个异常(exception) tidyselect `everything()`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58531982/

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