将具有交替缺失值的两个字符串列合并为一个 [英] Coalesce two string columns with alternating missing values to one

查看:59
本文介绍了将具有交替缺失值的两个字符串列合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两列a"和b"的数据框,其中包含交替的缺失值(NA)

I have a data frame with two columns "a" and "b" with alternating missing values (NA)

a      b
dog    <NA>
mouse  <NA>
<NA>   cat
bird   <NA>

我想将它们合并"/组合到一个看起来像这样的新列 c,即每行中的非 NA 元素被选中:

I want to "merge" / combine them to a new column c that looks like this, i.e. the non-NA element in each row is selected:

c
dog
mouse
cat
bird

我尝试了 mergejoin,但都没有达到我想要的效果.也许是因为我没有要合并的 ID?对于整数,我会绕过这个并添加两列,但在我的情况下如何?

I tried merge and join, but neither worked as I wanted. Maybe because I do not have an id with which to merge? For integers I would just circumvent this and add both columns, but how in my case?

推荐答案

你可以试试pmax

df$c <- pmax(df$a, df$b)
df
#       a    b     c
# 1   dog <NA>   dog
# 2 mouse <NA> mouse
# 3  <NA>  cat   cat
# 4  bird <NA>  bird

...或 ifelse:

df$c <- ifelse(is.na(df$a), df$b, df$a)

对于两列以上情况的更通用解决方案,您可以在 R 中找到几种实现合并的方法 这里.

For more general solutions in cases with more than two columns, you find several ways to implement coalesce in R here.

这篇关于将具有交替缺失值的两个字符串列合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆