合并R

时间:2019-04-28 19:38:50

标签: r

我正在为一个大学项目清理数据,我有两个变量,DidVote和WouldVote,指明了该人投票的对象(如果他们在实际选举中投票)以及他们将投票的对象(如果他们没有在选举中投票)。这两列显然是互补的,这意味着如果DidVote具有一定的价值,则WouldVote为NA,反之亦然。我想将这两个变量合并为一个,这意味着我想得到类似第三列的内容:

DidVote    WouldVote    Vote
x          NA           x
NA         z            z
NA         y            y
y          NA           y

我尝试执行以下操作:

data$Vote <- paste(data$DidVote,data$WouldVote)

但是我最终得到的是:

DidVote    WouldVote    Vote
x          NA           x NA
NA         z            NA z
NA         y            NA y
y          NA           y NA

如何合并两列,以使新变量从DidVote和WouldVote这两个变量的每一个中获取非NA值?

2 个答案:

答案 0 :(得分:1)

我们可以使用coalesce

library(dplyr)
df1 %>%
   mutate(Vote = coalesce(DidVote, WouldVote))
#   DidVote WouldVote Vote
#1       x      <NA>    x
#2    <NA>         z    z
#3    <NA>         y    y
#4       y      <NA>    y

数据

df1 <- structure(list(DidVote = c("x", NA, NA, "y"), WouldVote = c(NA, 
 "z", "y", NA), Vote = c("x", "z", "y", "y")), class = "data.frame", 
 row.names = c(NA, -4L))

答案 1 :(得分:0)

我们可以通过使用其索引输入非NA值来创建矢量投票。

df <- data.frame(DidVote = c("x", NA, NA, "y"),
                 WouldVote = c(NA, "z", "y", NA))
vote <- NULL

would_vote_indx <- !is.na(df$WouldVote)
vote[would_vote_indx] <- as.character(df$WouldVote[would_vote_indx])

did_vote_indx <- !is.na(df$DidVote)
vote[did_vote_indx] <- as.character(df$DidVote[did_vote_indx])

df$Vote <- vote