【问题标题】:Can you list an exception to tidyselect `everything()`你能列出一个 tidyselect `everything()` 的例外吗
【发布时间】:2019-10-23 22:58:57
【问题描述】:
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") %>% 
... %>% 

我收到以下错误:

错误:无法修改列 ID,因为它是一个分组变量

【问题讨论】:

  • 你有什么理由不做iris %&gt;% as_tibble() %&gt;% select(-Species)
  • 另外,如果您尝试删除分组变量,您可能需要在运行select 命令之前ungroup()
  • @Mako212 我不得不重新创建问题以表明我的意图。感谢这些线索。其他问题在这里-> stackoverflow.com/questions/58542413/…

标签: r dplyr tidyr tidyselect


【解决方案1】:

你会的

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 %&gt;% as_tibble() %&gt;% select(setdiff(everything(), 5))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多