Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve names when row-binding data frames #924

Merged
merged 8 commits into from
Mar 23, 2020

Conversation

lionel-
Copy link
Member

@lionel- lionel- commented Mar 17, 2020

Branched from #923.
Closes #689.

  • The internal vec_assign() function gains an assign_names parameter. When true, names are assigned row-recursively.

  • vec_c() and vec_rbind() set assign_names to true so that they preserve row names and inner names.

src/slice-assign.c Show resolved Hide resolved
proxy_nms = PROTECT(chr_assign(proxy_nms, index, value_nms));

proxy = PROTECT(r_maybe_duplicate(proxy));
vec_set_names(proxy, proxy_nms);
Copy link
Member

@DavisVaughan DavisVaughan Mar 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vec_set_names() works like vec_proxy_assign(). It might have to call back to names<- and duplicate the input, so it always returns it's input. So you won't need to duplicate the proxy, but I think you do need to do proxy = PROTECT(vec_set_names(proxy, proxy_nms))

src/slice-assign.h Show resolved Hide resolved
@@ -517,3 +517,27 @@ test_that("rbind repairs names of data frames (#704)", {
class = "vctrs_error_names_must_be_unique"
)
})

test_that("rbind supports names and inner names (#689)", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work with S3 objects like factors / POSIXct that vec_set_names() has to fall back to names<- on, but could you please add a test for that too? It would make me feel a bit better about it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would have caught the unassigned return you mentioned above, so that test is definitely needed!

@lionel- lionel- changed the base branch from fix-vec-set-names to master March 23, 2020 15:37
@lionel- lionel- merged commit 1350e43 into r-lib:master Mar 23, 2020
@lionel- lionel- deleted the fix-rbind-names branch March 23, 2020 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

vec_rbind() and names
2 participants