-
Notifications
You must be signed in to change notification settings - Fork 66
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
Expose vec_names2(), vec_names() and vec_set_names() #1173
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -193,15 +193,47 @@ detect_dot_dot <- function(names) { | |
grep("^[.][.](?:[.]|[1-9][0-9]*)$", names) | ||
} | ||
|
||
#' Extract repaired names from a vector | ||
#' Get or set the names of a vector | ||
#' | ||
#' Returns the repaired names from a vector, even if the vector is unnamed. | ||
#' @description | ||
#' These functions work like [rlang::names2()], [names()] and [names<-()], | ||
#' except that they return or modify the conceptual names of the vector. | ||
#' This is different for data frames and matrices (here, row names are used), | ||
#' and arrays (here, the dimension names of the first dimension are used). | ||
krlmlr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' | ||
#' `vec_names2()` returns the repaired names from a vector, even if it is unnamed. | ||
#' See [vec_as_names()] for details on name repair. | ||
#' | ||
#' `vec_names()` is a bare-bones version that returns `NULL` if the vector is | ||
#' unnamed. | ||
#' | ||
#' `vec_set_names()` sets the names. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps a note that you can set names to |
||
#' | ||
#' @param x A vector with names | ||
#' @inheritParams vec_as_names | ||
#' | ||
#' @return The names of x, repaired | ||
#' @noRd | ||
#' @return | ||
#' `vec_names2()` returns the names of `x`, repaired. | ||
#' `vec_names()` returns the names of `x` or `NULL` if unnamed. | ||
#' `vec_set_names()` returns `x` with names updated. | ||
#' | ||
#' @name vec_names | ||
#' @export | ||
#' @examples | ||
#' vec_names2(1:3) | ||
#' vec_names2(1:3, repair = "unique") | ||
#' vec_names2(c(a = 1, b = 2)) | ||
#' | ||
#' # `vec_names()` is different from `names()`: | ||
krlmlr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#' vec_names(data.frame(a = 1, b = 2)) | ||
#' names(data.frame(a = 1, b = 2)) | ||
#' vec_names(mtcars) | ||
#' names(mtcars) | ||
#' vec_names(Titanic) | ||
#' names(Titanic) | ||
#' | ||
#' vec_set_names(1:3, letters[1:3]) | ||
#' vec_set_names(data.frame(a = 1:3), letters[1:3]) | ||
vec_names2 <- function(x, | ||
..., | ||
repair = c("minimal", "unique", "universal", "check_unique"), | ||
|
@@ -247,6 +279,8 @@ unique_names <- function(x, quiet = FALSE) { | |
.Call(vctrs_unique_names, x, quiet) | ||
} | ||
|
||
#' @rdname vec_names | ||
#' @export | ||
vec_names <- function(x) { | ||
.Call(vctrs_names, x) | ||
} | ||
|
@@ -433,6 +467,8 @@ set_names_fallback <- function(x, names) { | |
x | ||
} | ||
|
||
#' @rdname vec_names | ||
#' @export | ||
vec_set_names <- function(x, names) { | ||
.Call(vctrs_set_names, x, names) | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the rowwise names of the vector. These are:
names()
for atomic vectors and listsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excuse the drive-by comment, but what is meant by "conceptual" names? If that term is defined and used elsewhere and I just don't have enough context, then never mind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed this is not a defined term. I'm suggesting to replace "conceptual" by "rowwise", which seems clearer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think since it is so related to the vctrs concept of size, they could be called "size consistent names"