I want to merge two data frames, skipping rows based on a specific column value, but still keep the skipped rows in the final merged data frame. I can manage the first part (skipping), but not the second.
Here are the data frames:
# Data frame 1 values
ids1 <- c(1:3)
x1 <- c(100, 101, 102)
doNotMerge <- c(1, 0, 0)
# Data frame 2 values
ids2 <- c(1:3)
x2 <- c(200, 201, 202)
# Creating the data frames
df1 <- as.data.frame(matrix(c(ids1, x1, doNotMerge),
                            nrow = 3,
                            ncol = 3,
                            dimnames = list(c(),c("ID", "X1", "DoNotMerge"))))
df2 <- as.data.frame(matrix(c(ids2, x2),
                            nrow = 3,
                            ncol = 2,
                            dimnames = list(c(),c("ID", "X2"))))
# df1 contents:
#   ID  X1 DoNotMerge
# 1  1 100          1
# 2  2 101          0
# 3  3 102          0
# df2 contents:
#   ID  X2
# 1  1 200
# 2  2 201
# 3  3 202
I used merge:
merged <- merge(df1[df1$DoNotMerge != 1,], df2, by = "ID", all = T)
# merged contents:
#   ID  X1 DoNotMerge  X2
# 1  1  NA         NA 200
# 2  2 101          0 201
# 3  3 102          0 202
The skipping part I was able to do, but what I actually want is to keep the df1 row where DoNotMerge == 1, like so:
#   ID  X1 DoNotMerge  X2
# 1  1  NA         NA 200
# 2  1 100          1  NA
# 3  2 101          0 201
# 4  3 102          0 202
Can anyone please help? Thanks.
 
    