使用多列数据替换NA [英] Replace NA's using data from Multiple Columns

查看:111
本文介绍了使用多列数据替换NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框看起来像这样:

I have a data-frame that looks as such:

ID   col2  col3   col4 
1      5    NA    NA
2     NA    NA    1 
3      5    NA    NA
4     19    NA    1        

如果col2有一个值,该单元格不应该改变(即使第3列和第4列包含值)。但是,如果col2包含NA值,我想从col3或col4返回任何非NA。

If col2 has a value, that cell should not change (even if columns 3 and 4 contains values). However, if col2 contains an "NA" value, I would like to return any non-NA's from col3 or col4, if they exist.

所需的输出如下所示,请注意现在第2行的1。

Desired output shown below, notice how row 2 has the "1" there now.

ID   col2  col3   col4 
1      5    NA    NA
2      1    NA    1 
3      5    NA    NA
4     19    NA    1   

我知道这可以通过引用每个列手动完成使用$或[],但是怎么可以使用for循环或应用?

I know this can be done manually by referencing each column using $ or [], but how can this be done using a for-loop or apply?

谢谢

推荐答案

我们可以使用 ifelse

df1$col2 <- with(df1, ifelse(is.na(col2), pmax(col3, col4, na.rm = TRUE), col2))
df1$col2
#[1]  5  1  5 19






或创建一个用于替换值的逻辑索引


Or create a logical index to replace the values

i1 <- is.na(df1$col2)
df1$col2[i1] <- do.call(pmax, c(df1[i1, 3:4], na.rm = TRUE))

这篇关于使用多列数据替换NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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