[R] Need to compare two columns in two data.frames and return all rows from df where rows values are missing

Jake Elmstedt j@ke@e|m@tedt @end|ng |rom gm@||@com
Mon Jun 14 12:42:00 CEST 2021


set.seed(123)

df1 <- data.frame(name = sample(letters[1:4], 12, TRUE), score = runif(12))
head(df1)
#>   name      score
#> 1    c 0.67757064
#> 2    c 0.57263340
#> 3    c 0.10292468
#> 4    b 0.89982497
#> 5    c 0.24608773
#> 6    b 0.04205953
table(df1[["name"]])
#>
#> a b c d
#> 1 5 5 1

df2 <- data.frame(name = sample(letters[3:6], 12, TRUE), score = runif(12))
head(df2)
#>   name     score
#> 1    c 0.7584595
#> 2    e 0.2164079
#> 3    f 0.3181810
#> 4    d 0.2316258
#> 5    e 0.1428000
#> 6    d 0.4145463
table(df2[["name"]])
#>
#> c d e f
#> 3 4 3 2

df3 <- rbind(df1[!df1[["name"]] %in% df2[["name"]], ],
             df2[!df2[["name"]] %in% df1[["name"]], ])
head(df3)
#>    name      score
#> 4     b 0.89982497
#> 6     b 0.04205953
#> 7     b 0.32792072
#> 8     b 0.95450365
#> 10    a 0.69280341
#> 12    b 0.99426978
table(df3[["name"]])
#>
#> a b e f
#> 1 5 3 2



More information about the R-help mailing list