用另一列的值替换NA值

时间:2017-04-04 12:11:16

标签: r

我有两列调查答案:

subject_id | income | income unknown |
1          | 1000   | NA             |
2          | 15000  | NA             |
3          | NA     | 15000          |
4          | NA     | 10000          |
5          | 15000  | NA             |

我想合并这两列。如果第一个收入列没有得到回答(在大多数情况下至少是),则第二列有一个值。

我想用零替换NA s然后将两列相加,但这并没有真正起作用。

2 个答案:

答案 0 :(得分:2)

我们可以使用coalesce

library(tidyverse)
df1 %>%
    transmute(subject_id, income = coalesce(income, income_unknown))
#   subject_id income
#1          1   1000
#2          2  15000
#3          3  15000
#4          4  10000
#5          5  15000

其他base R个选项包括rowSumspmaxpmin等,如果每行只有一个非NA元素

df1$income <- rowSums(df1[-1], na.rm = TRUE)
df1$income <- do.call(pmax, c(df1[-1], na.rm = TRUE))

数据

df1 <- structure(list(subject_id = 1:5, income = c(1000L, 15000L, NA, 
NA, 15000L), income_unknown = c(NA, NA, 15000L, 10000L, NA)),
.Names = c("subject_id", 
"income", "income_unknown"), class = "data.frame",
 row.names = c(NA, -5L))

答案 1 :(得分:2)

您可以使用ifelse()

df$income.new <- ifelse(is.na(df$income), df$income_unknown, df$income)