如何用dplyr替换多列中的NA [英] How to replace NAs in multiple columns with dplyr
问题描述
我想使用当前 dplyr将以 v
开头的列中的 NA
替换为 x
列中的值(1.0.2)代码.
I would like to replace NA
s in the columns that begin with v
with the values in column x
using current dplyr (1.0.2) code.
同一问题发布在此处,但
The same question is posted here, but the answer is outdated.
我一栏没问题:
suppressMessages(library(dplyr))
df <- data.frame(v1 = c(NA, 1, 2), v2 = c(3, NA, 4), v3 = c(5, 6, NA), x = c(7, 8, 9))
df %>% mutate(v1 = coalesce(v1, x))
#> v1 v2 v3 x
#> 1 7 3 5 7
#> 2 1 NA 6 8
#> 3 2 4 NA 9
由 reprex软件包(v0.3.0)创建于2020-11-03 sup>
Created on 2020-11-03 by the reprex package (v0.3.0)
但无法弄清楚如何使其能够在多列中使用.
but can't figure out how to get it to work across multiple columns.
以下是我尝试不了的一些事情:
Here are a few things I've tried to no avail:
suppressMessages(library(dplyr))
df <- data.frame(v1 = c(NA, 1, 2), v2 = c(3, NA, 4), v3 = c(5, 6, NA), x = c(7, 8, 9))
df %>% mutate(across(starts_with("v")), . = coalesce(., x))
#> Error in list2(...): object 'x' not found
由 reprex软件包(v0.3.0)创建于2020-11-03 sup>
Created on 2020-11-03 by the reprex package (v0.3.0)
suppressMessages(library(dplyr))
df <- data.frame(v1 = c(NA, 1, 2), v2 = c(3, NA, 4), v3 = c(5, 6, NA), x = c(7, 8, 9))
df %>% mutate(across(starts_with("v")), . = coalesce(., df$x))
#> Error: Can't combine `..1` <data.frame> and `..2` <double>.
由 reprex软件包(v0.3.0)创建于2020-11-03 sup>
Created on 2020-11-03 by the reprex package (v0.3.0)
感谢您的帮助.
推荐答案
您与 across()
非常接近.您想要的方法是:
You were very close with across()
. The approach you want is:
df %>%
mutate(across(starts_with("v"), coalesce, x))
请注意, coalesce
在 across()
内 ,并且 x
(可以将 coalesce()
作为第三个参数提供.
Notice that the coalesce
goes inside the across()
, and that x
(the second argument to coalesce()
can be provided as a third argument. Result:
v1 v2 v3 x
1 7 3 5 7
2 1 8 6 8
3 2 4 9 9
如果您更喜欢使用 coalesce(.,x)
的方法,还可以使用〜
将其作为匿名函数传递:
If you prefer something closer to your approach with coalesce(., x)
, you can also pass that as an anonymous function with a ~
:
df %>%
mutate(across(starts_with("v"), ~ coalesce(., x)))
在其他情况下,这可以更加灵活(例如,如果.
不是该函数的第一个参数).
In other situations, this can be more flexible (for instance, if .
is not the first argument to the function).
这篇关于如何用dplyr替换多列中的NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!