Last active
March 16, 2022 15:52
-
-
Save RaphaelS1/be9fe23f51a3d7e79fb251e5cce4b946 to your computer and use it in GitHub Desktop.
Multiple assignment of variable values in R
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
`%=%` <- function(l, r) { | |
l <- trimws(strsplit(l, ",", TRUE)[[1]]) | |
if (all(l == "USE.NAMES") || all(l == "*") || all(l == "?")) { | |
stopifnot(length(names(r)) > 0) | |
l <- names(r) | |
} else { | |
stopifnot(identical(length(l), length(r))) | |
which <- l == "?" | |
if (any(which)) { | |
stopifnot(length(names(r)) > 0) | |
l[which] <- names(r)[which] | |
} | |
} | |
invisible(Map(assign, l, r, MoreArgs = list(envir = parent.frame()))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2022-03-16 by the reprex package (v2.0.1)