I have this tibble
我有这个东西
library(tidyverse)
data_frame(first = c("a", NA, "b"),
second = c(NA, "b", NA),
third = c("a", NA, NA))
I would like to use coalesce()
row-wise to grab only values that are not NA.
我想使用coalesce()行方式来仅获取非NA的值。
The desired output would be a vector of the first non NA values that we can find inspecting the dataframe rowwise from left to righ
期望的输出将是第一个非NA值的向量,我们可以找到从左到右逐行检查数据帧
[1] "a" "b" "b"
1 个解决方案
#1
4
Use do.call
with coalesce
:
使用do.call with coalesce:
do.call(coalesce, df)
# [1] "a" "b" "b"
do.call
passes columns in df
to coalesce
in order and thus equivalent to coalesce(df$first, df$second, df$third)
.
do.call传递df中的列以按顺序合并,因此相当于合并(df $ first,df $ second,df $ third)。
df <- data_frame(
first = c("a", NA, "b"),
second = c(NA, "b", NA),
third = c("a", NA, NA)
)
#1
4
Use do.call
with coalesce
:
使用do.call with coalesce:
do.call(coalesce, df)
# [1] "a" "b" "b"
do.call
passes columns in df
to coalesce
in order and thus equivalent to coalesce(df$first, df$second, df$third)
.
do.call传递df中的列以按顺序合并,因此相当于合并(df $ first,df $ second,df $ third)。
df <- data_frame(
first = c("a", NA, "b"),
second = c(NA, "b", NA),
third = c("a", NA, NA)
)