Skip to content

rows_patch(x, y, ...) shows unhelpful error message when nrow(y) > nrow(x) #5699

@SteadyGiant

Description

@SteadyGiant

When I try to patch a data frame (x in the below example) using another data frame with more rows (y), an error is thrown with an unhelpful message. It took some time to figure out that the row number difference was the source of the error. rlang::last_error() and rlang::last_trace() didn't help.

Is there any reason a data frame should not be patched by a data frame with more rows? I could be missing some theory here. If there is a good reason why this should not be allowed, can I add that to the documentation for rows_patch() and friends?

Example:

suppressPackageStartupMessages(library(dplyr))

x = data.frame(id = c(1, 2, 3), foo = c("a", "b", NA))
y = data.frame(id = c(1, 3, 5, 7), foo = c("a", "c", "e", "g"))
dplyr::rows_patch(x, y, by = "id")
#> Error: Attempting to patch missing rows.

Created on 2021-01-20 by the reprex package (v0.3.0)

Session info
sessionInfo()
#> R version 4.0.3 (2020-10-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19041)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_United States.1252 
#> [2] LC_CTYPE=English_United States.1252   
#> [3] LC_MONETARY=English_United States.1252
#> [4] LC_NUMERIC=C                          
#> [5] LC_TIME=English_United States.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] dplyr_1.0.3
#> 
#> loaded via a namespace (and not attached):
#>  [1] knitr_1.30       magrittr_2.0.1   tidyselect_1.1.0 R6_2.5.0        
#>  [5] rlang_0.4.10     stringr_1.4.0    highr_0.8        tools_4.0.3     
#>  [9] xfun_0.20        DBI_1.1.1        htmltools_0.5.1  ellipsis_0.3.1  
#> [13] yaml_2.2.1       digest_0.6.27    assertthat_0.2.1 tibble_3.0.5    
#> [17] lifecycle_0.2.0  crayon_1.3.4     purrr_0.3.4      vctrs_0.3.6     
#> [21] glue_1.4.2       evaluate_0.14    rmarkdown_2.6    stringi_1.5.3   
#> [25] compiler_4.0.3   pillar_1.4.7     generics_0.1.0   pkgconfig_2.0.3

Expected output:

data.frame(id = c(1, 2, 3), foo = c("a", "b", "c"))
#>   id foo
#> 1  1   a
#> 2  2   b
#> 3  3   c

Metadata

Metadata

Assignees

No one assigned

    Labels

    tables 🧮joins and set operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions