Hi There,
This is a variation on this topic that was solved by FJCC.
I have a master data frame (DF) and a DF with corrected values. I need to override the values in the master DF with the corrections DF by matching the records with the character IDs.
df <- tibble::tribble(
~id, ~chr_id, ~var, ~group,
1, "B02-Y0B47HF", 2500, "a",
2, "B05-IBBKX57", 1500, "b",
3, "B09-HZ4EQGK", 3000, "a",
4, "B05-08J03UJ", 250, "a"
)
corrections <- tibble::tribble(
~id, ~chr_id, ~var,
1, "B02-Y0B47HF", 250,
2, "B05-IBBKX57", 150,
3, "B09-HZ4EQGK", 300
)
The following solution was provided by FJCC to my initial request:
df[corrections$id, "var"] <- corrections$var
df
#> # A tibble: 4 x 4
#> id chr_id var group
#> <dbl> <chr> <dbl> <chr>
#> 1 1 B02-Y0B47HF 250 a
#> 2 2 B05-IBBKX57 150 b
#> 3 3 B09-HZ4EQGK 300 a
#> 4 4 B05-08J03UJ 250 a
However, I tried to apply FJCCs solution using chr_id instead of id which leads to an unexpected result:
df[corrections$chr_id, "var"] <- corrections$var
df
#> # A tibble: 7 x 4
#> id chr_id var group
#> * <dbl> <chr> <dbl> <chr>
#> 1 1 B02-Y0B47HF 2500 a
#> 2 2 B05-IBBKX57 1500 b
#> 3 3 B09-HZ4EQGK 3000 a
#> 4 4 B05-08J03UJ 250 a
#> 5 NA <NA> 250 <NA>
#> 6 NA <NA> 150 <NA>
#> 7 NA <NA> 300 <NA>
I also tried the following code which resulted in an error:
df$var <- corrections$var[corrections$chr_id == df$chr_id]
#> Warning in corrections$chr_id == df$chr_id: longer object length is not a
#> multiple of shorter object length
#> Error in `$<-.data.frame`(`*tmp*`, var, value = c(250, 150, 300)): replacement has 3 rows, data has 4
The above error is resolved by ensuring that the number of rows in both data frames are equel:
df <- tibble::tribble(
~id, ~chr_id, ~var, ~group,
1, "B02-Y0B47HF", 2500, "a",
2, "B05-IBBKX57", 1500, "b",
3, "B09-HZ4EQGK", 3000, "a"
)
corrections <- tibble::tribble(
~id, ~chr_id, ~var,
1, "B02-Y0B47HF", 250,
2, "B05-IBBKX57", 150,
3, "B09-HZ4EQGK", 300
)
df$var <- corrections$var[corrections$chr_id == df$chr_id]
df
#> # A tibble: 3 x 4
#> id chr_id var group
#> <dbl> <chr> <dbl> <chr>
#> 1 1 B02-Y0B47HF 250 a
#> 2 2 B05-IBBKX57 150 b
#> 3 3 B09-HZ4EQGK 300 a
Is there a way to use any unique ID to match records in different data frames and overide the related values?
Thanks!
André