本文介绍了无法将tidyselecteverything()与group_by()和fill()结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

library(tidyverse)
df <- tibble(x1 = c("A", "A", "A", "B", "B", "B"),
             x2 = c(NA, 8, NA, NA, NA, 5),
             x3 = c(3, 6, 5, 9, 1, 9))
#> # A tibble: 6 x 3
#>   x1       x2    x3
#>   <chr> <dbl> <dbl>
#> 1 A        NA     3
#> 2 A         8    NA
#> 3 A        NA     5
#> 4 B        NA     9
#> 5 B        NA     1
#> 6 B         5     9

我在列<$ c $中显示了组'A'和'B' c> x1 。我需要列 x2 x3 列中的'NA'值来填充 中的值在同一组中,沿上移方向。这很简单,下面是代码:

I have groups 'A' and 'B' shown in column x1. I need the 'NA' values in columns x2 and x3 to populate only from values within the same group, in the updown direction. That's simple enough, here's the code:

df %>% group_by(x1) %>% fill(c(x2, x3), .direction = "updown")
#> # A tibble: 6 x 3
#>   x1       x2    x3
#>   <chr> <dbl> <dbl>
#> 1 A         8     3
#> 2 A         8     5
#> 3 A         8     5
#> 4 B         5     9
#> 5 B         5     1
#> 6 B         5     9

我的现实生活中的问题是我的数据框不只包含列 x1 x3 。更像是 x1 x100 。列名是非常随机的,没有逻辑顺序。为了避免自己输入所有〜100列的麻烦,我尝试了下面的tidyselect everything()参数。但这会产生一个可以理解的错误。我不知道该如何解决。

My real-life issue is that my data frame doesn't contain just columns x1 through x3. It's more like x1 through x100. And the column names are very random, in no logical order. To save myself the trouble of typing all ~100 columns in I tried the tidyselect everything() argument shown below. But that yields an understandable error. I don't know how to work around it.

df %>% group_by(x1) %>% fill(everything(), .direction = "updown")
#> Error: Column `x1` can't be modified because it's a grouping variable

,关于将 everything()参数,在我的方法中太简单了,结果造成了我对解决方案中所要查看内容的意图的困惑。建议的解决方案 您可以使用 select(-variable) ,在我上面概述的情况下不起作用(我相信)。因此,这个新问题。我该怎么办?

I asked a related question yesterday, about naming exceptions to the everything() argument, was too simple in my approach, and as a consequence caused confusion on the intent on what I wanted to see in a solution. The proposed solution, "you can use select(-variable)", won't work in my case outlined above (I believe). Hence, this new question. What do I do?

我还应该提到,只需选择数字列顺序(即 2:100 )无法使用,因为我需要按名称挑选一些列(例如 x45 x70 )。而且列的顺序可以每月更改,我必须按列名进行选择。因此,将 everything() everything_but(column.names = c(x45,x70))选项一起使用我真正想要的它存在吗?

I should also mention that simply selecting the numerical column sequence (ie 2:100) won't work because I need to cherry pick some columns out by name (eg x45, x70). And the order of the columns can change month to month, I have to cherry pick by column name. So using everything() with the option of everything_but(column.names = c(x45, x70)) would be what I really want. Does it exist?

推荐答案

您可以这样做:

df %>%
 group_by(x1) %>%
 fill(-x1, .direction = "updown")

  x1       x2    x3
  <chr> <dbl> <dbl>
1 A         8     3
2 A         8     6
3 A         8     5
4 B         5     9
5 B         5     1
6 B         5     9

此行为记录在 tidyr (还可以查看@Gregor的评论):

This behavior is documented in the documentation of tidyr (also look at the comment from @Gregor):

这篇关于无法将tidyselecteverything()与group_by()和fill()结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:07