-
Notifications
You must be signed in to change notification settings - Fork 985
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
Added skip_absent()
feature to setnames()
#3111
Changes from 9 commits
baaf311
12f5576
ece7dd4
f16e15f
8b3139b
e213af8
fa11f45
bb75fe4
f7a1915
364e843
19dbcfd
21bb14c
4525d63
74e98ca
ad52224
c30963d
bfa1db0
543b9c7
ea36552
21d12a3
fee02b6
c5debe7
bf140dc
13e857a
f491437
66f311f
bf8db81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,4 @@ | |
^bus$ | ||
^Dockerfile$ | ||
^Dockerfile\.in$ | ||
^.*\.dll$ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
# Source: https://github.com/github/gitignore/blob/master/R.gitignore | ||
# History files | ||
.RData | ||
.Rhistory | ||
.Rapp.history | ||
|
||
# Package build process | ||
*-Ex.R | ||
data.table_*.tar.gz | ||
data.table.Rcheck | ||
|
||
# Emacs IDE files | ||
.emacs.desktop | ||
.emacs.desktop.lock | ||
|
||
# RStudio IDE files | ||
.Rproj.user | ||
data.table.Rproj | ||
|
||
# produced vignettes | ||
vignettes/*.html | ||
vignettes/*.pdf | ||
|
||
# object and shared objects | ||
*.o | ||
*.so | ||
|
||
*~ | ||
.DS_Store | ||
.idea | ||
*.sw[op] | ||
# Source: https://github.com/github/gitignore/blob/master/R.gitignore | ||
# History files | ||
.RData | ||
.Rhistory | ||
.Rapp.history | ||
|
||
# Package build process | ||
*-Ex.R | ||
data.table_*.tar.gz | ||
data.table.Rcheck | ||
|
||
# Emacs IDE files | ||
.emacs.desktop | ||
.emacs.desktop.lock | ||
|
||
# RStudio IDE files | ||
.Rproj.user | ||
data.table.Rproj | ||
|
||
# produced vignettes | ||
vignettes/*.html | ||
vignettes/*.pdf | ||
|
||
# object and shared objects | ||
*.o | ||
*.so | ||
*.dll | ||
|
||
*~ | ||
.DS_Store | ||
.idea | ||
*.sw[op] |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
dim.data.table <- function(x) | ||
{ | ||
.Call(Cdim, x) | ||
|
@@ -2497,7 +2496,7 @@ setattr <- function(x,name,value) { | |
invisible(x) | ||
} | ||
|
||
setnames <- function(x,old,new) { | ||
setnames <- function(x,old,new,skip_absent=FALSE) { | ||
# Sets by reference, maintains truelength, no copy of table at all. | ||
# But also more convenient than names(DT)[i]="newname" because we can also do setnames(DT,"oldname","newname") | ||
# without an onerous match() ourselves. old can be positions, too, but we encourage by name for robustness. | ||
|
@@ -2532,7 +2531,14 @@ setnames <- function(x,old,new) { | |
if (!is.character(old)) stop("'old' is type ",typeof(old)," but should be integer, double or character") | ||
if (any(duplicated(old))) stop("Some duplicates exist in 'old': ", paste(old[duplicated(old)],collapse=",")) | ||
i = chmatch(old,names(x)) | ||
if (anyNA(i)) stop("Items of 'old' not found in column names: ",paste(old[is.na(i)],collapse=",")) | ||
if (anyNA(i)){ if (skip_absent == TRUE){ | ||
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. either |
||
w <- old %chin% names(x) | ||
M-YD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
old = old[w] | ||
new = new[w] | ||
i = i[w] | ||
} else { | ||
stop("Items of 'old' not found in column names: ",paste(old[is.na(i)],collapse=",")) } | ||
} | ||
if (any(tt<-!is.na(chmatch(old,names(x)[-i])))) stop("Some items of 'old' are duplicated (ambiguous) in column names: ",paste(old[tt],collapse=",")) | ||
} | ||
if (length(new)!=length(i)) stop("'old' is length ",length(i)," but 'new' is length ",length(new)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12328,6 +12328,11 @@ test(1951.4, d1[d2, nomatch=3], error="nomatch= must be either NA or NULL .or 0 | |
test(1952.1, d1[a==2, which=3], error="which= must be a logical vector length 1. Either FALSE, TRUE or NA.") | ||
test(1952.2, d1[a==2, 2, which=TRUE], error="which==TRUE.*but j is also supplied") | ||
|
||
# skip values that are not present in old, #3030 | ||
DT <- data.table(a = 1, b = 2, d = 3) | ||
old <- c("a", "b", "c", "d") | ||
new <- c("A", "B", "C", "D") | ||
test(1953, setnames(DT, old, new, skip_absent = TRUE), DT <- data.table(A = 1, B = 2, D = 3)) | ||
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. please also test potential erroneous behavior/mis-use of 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. agree need more tests please. |
||
|
||
################################### | ||
# Add new tests above this line # | ||
|
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.
People don't normally add themselves here. There are 53 contributors who have done something, no matter how small: https://github.com/Rdatatable/data.table/graphs/contributors. You would be listed there, and in NEWS. When a contributor has made sustained contributions, or a few big ones, I add them to DESCRIPTION which is displayed on CRAN. The contributors listed in DESCRIPTION are roughly listed at the top of https://github.com/Rdatatable/data.table/graphs/contributors.
At least, this is the way this has been done to date. We could change so that every single contributor is listed in DESCRIPTION if people want to.
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 that something midway between the two would be ideal.
For example, those who make small, non-functional changes (such as correcting typos) wouldn't be included here but those who extend the functionality of
data.table
in some meaningful and/or useful way should be included.That way it would prevent this file from becoming too large, whilst still retaining its relevance and meaning.
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 the author field should be limited to those who have contributed code that the authors would not have ordinarily come up with. This PR is an enhancement but its implementation is quite elementary so should not qualify.
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'll remove this addition for now in this PR but have created #3144 to discuss further. If #3144 goes ahead, Mus would be added then along with other names. Trying to be utterly fair to everyone, past and present.