Skip to content

rows_patch fails when y has key values that don't occur in x #5984

Closed
@melissagwolf

Description

@melissagwolf

I tried to use row_patch to fill in missing values in a data frame. It failed by design, because it didn't meet the requirement for the function: "key values in y must occur in x". However, I was able to successfully execute the row patch by using the match function from base R (see below).

This isn't a typical bug report because it is clearly stated that this is a limitation of the function, but I wanted to point out that it can be done using a different function from base R. Since row_patch is currently an experimental function in dplyr, I wanted to provide feedback to hopefully improve it. row_patch is much cleaner and I would prefer to use it if possible.

It would also be nice if row_patch could preserve other variables in y that don't occur in x. match can handle this as well.

D1 <- data.frame(
  id=seq(1,3),
  x=c("cow",NA,"sheep"))

D2 <- data.frame(
  id=seq(1,4),
  x=c("cow","turtle","parrot","frog"))

#rows_patch fails
D1 %>%
  rows_patch(D2, by = "id")

#match works
na <- is.na(D1$x)

D1$x[na] <- D2$x[match(D1$id[na],D2$id)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementtables 🧮joins and set operations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions