Dplyr跨越+变异+条件以选择列

问题描述

我确定解决方案是使用一根衬纸,但是我将头撞在墙上。 请参阅帖子结尾处的简短说明。我如何告诉dplyr我只想将没有NA的列加倍?

非常感谢

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter,lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect,setdiff,setequal,union


df <- tibble(x=1:10,y=101:110,w=c(6,NA,4,5,8,17 ),z=c(2,3,10,22,34,58,7 ),k=rep("A",10))


df
#> # A tibble: 10 x 5
#>        x     y     w     z k    
#>    <int> <int> <dbl> <dbl> <chr>
#>  1     1   101     6     2 A    
#>  2     2   102    NA     3 A    
#>  3     3   103     4     4 A    
#>  4     4   104    NA    NA A    
#>  5     5   105     5     5 A    
#>  6     6   106     0    10 A    
#>  7     7   107    NA    22 A    
#>  8     8   108     4    34 A    
#>  9     9   109     8    58 A    
#> 10    10   110    17     7 A


df %>% mutate(across(where(is.numeric),~.x*2))
#> # A tibble: 10 x 5
#>        x     y     w     z k    
#>    <dbl> <dbl> <dbl> <dbl> <chr>
#>  1     2   202    12     4 A    
#>  2     4   204    NA     6 A    
#>  3     6   206     8     8 A    
#>  4     8   208    NA    NA A    
#>  5    10   210    10    10 A    
#>  6    12   212     0    20 A    
#>  7    14   214    NA    44 A    
#>  8    16   216     8    68 A    
#>  9    18   218    16   116 A    
#> 10    20   220    34    14 A


##now double the value of all the columns without NA. How to fix this...

df %>% mutate(across(where(sum(is.na(.x))==0),~.x*2))
#> Error: Problem with `mutate()` input `..1`.
#> ✖ object '.x' not found
#> ℹ Input `..1` is `across(where(sum(is.na(.x)) == 0),~.x * 2)`.

reprex package(v0.3.0.9001)于2020-10-27创建

解决方法

这是您要寻找的一线客

df %>% mutate(across(where(~is.numeric(.) && all(!is.na(.))),~.x*2))

输出

# A tibble: 10 x 5
       x     y     w     z k    
   <dbl> <dbl> <dbl> <dbl> <chr>
 1     2   202     6     2 A    
 2     4   204    NA     3 A    
 3     6   206     4     4 A    
 4     8   208    NA    NA A    
 5    10   210     5     5 A    
 6    12   212     0    10 A    
 7    14   214    NA    22 A    
 8    16   216     4    34 A    
 9    18   218     8    58 A    
10    20   220    17     7 A 
,

请注意,其目的是选择不包含数字的NA的列。回想一下where的输入必须是一个函数。就您而言:

df %>% mutate(across(where(~is.numeric(.) & sum(is.na(.x))==0),~.x*2))

为您提供其他方式:

df %>% mutate(across(where(~!anyNA(.) & is.numeric(.)),~.*2))
# A tibble: 10 x 5
       x     y     w     z k    
   <dbl> <dbl> <dbl> <dbl> <chr>
 1     2   202     6     2 A    
 2     4   204    NA     3 A    
 3     6   206     4     4 A    
 4     8   208    NA    NA A    
 5    10   210     5     5 A    
 6    12   212     0    10 A    
 7    14   214    NA    22 A    
 8    16   216     4    34 A    
 9    18   218     8    58 A    
10    20   220    17     7 A

如果您知道如何使用求反函数:

df %>% mutate(across(where(~Negate(anyNA)(.) & is.numeric(.)),~.*2))

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...