具有多个列和NA的R-Nested Ifelse

时间:2017-10-16 14:09:21

标签: r

我有比赛的数据框:

df <- data.frame(ID=c(1,2,3,4,5,6), Condition=c(1,2,2,1,1,2), White=c(1,1,NA,NA,NA,NA), Black=c(2,NA,NA,NA,2,NA), Asian=c(NA, NA, NA, 3, 3, 3), AmerIndian=c(NA,NA,4,NA,NA,NA), NatHawaiian=c(NA, NA, NA, 5, NA, NA))

我想为比赛开发一个新的领域,无论条件2是什么,都会填写新的领域。

这是我试过的:

df$var <-ifelse(as.numeric(df$White)==1&!is.na(df$White),"White", 
+     ifelse(as.numeric(df$Black)==2&!is.na(df$Black),"Black",
+        ifelse(as.numeric(df$Asian)==3&!is.na(df$Asian),"Asian",
+           ifelse(as.numeric(df$AmerIndian)==4&!is.na(df$AmerIndian),"AmerIndian",
+               ifelse(as.numeric(df$NatHawaiian)==5&!is.na(df$NatHawaiian),"NatHawaiian",NA)))))

我收到了这个错误:

Error in +ifelse(as.numeric(df$NatHawaiian) == 5 & !is.na(df$NatHawaiian),  : 
  invalid argument to unary operator

2 个答案:

答案 0 :(得分:1)

根据您输入的代码和生成的错误,我建议在每个ifelse语句的开头删除加号。完成后,您共享的代码:

df$var <- ifelse(
  as.numeric(df$White) == 1 & !is.na(df$White),
  "White", ifelse(
    as.numeric(df$Black) == 2 & !is.na(df$Black),
    "Black", ifelse(
      as.numeric(df$Asian) == 3 & !is.na(df$Asian),
      "Asian", ifelse(
        as.numeric(df$AmerIndian) == 4 & !is.na(df$AmerIndian),
        "AmerIndian", ifelse(
          as.numeric(df$NatHawaiian) == 5 &
            !is.na(df$NatHawaiian),
          "NatHawaiian",
          NA
        )
      )
    )
  )
)

将生成此输出:

  ID Condition White Black Asian AmerIndian NatHawaiian        var
1  1         1     1     2    NA         NA          NA      White
2  2         2     1    NA    NA         NA          NA      White
3  3         2    NA    NA    NA          4          NA AmerIndian
4  4         1    NA    NA     3         NA           5      Asian
5  5         1    NA     2     3         NA          NA      Black
6  6         2    NA    NA     3         NA          NA      Asian

答案 1 :(得分:0)

你可以试试tidyverse,如:

df %>%
  gather(key = "race", value = "val", 3:7) %>%
  mutate( rc = if_else((Condition == 2 & !is.na(val)), race, NULL)) %>%
  spread(race, val)