5

I'm working on cleaning up my data for a university project, and I have two variables, DidVote and WouldVote, indicating who the person voted for (if they voted in the actual election) and who they would have voted for (if they did not vote in the election). These two columns are obviously complementary, meaning that if DidVote has some value, WouldVote is NA, and vice-versa. I want to merge these two variables into one, meaning I want to get something like the third column:

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

I have tried to do the following:

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

But what I end up getting is:

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

How can I merge the two columns such that the new variable takes the non NA value from each of the two variables DidVote and WouldVote?

vmar
  • 51
  • 2

2 Answers2

3

We can use 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

data

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))
akrun
  • 874,273
  • 37
  • 540
  • 662
0

We could create the vector vote by putting in the non-NA values using their indices.

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
mcz
  • 587
  • 2
  • 10