You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This works to rename the demand column to demand2. Note we are using plyr's rename, not dplyr's rename:
library(dplyr)
num <- 2
BOD %>% plyr::rename(list(demand = paste0("demand", num))) # ok
Since dplyr's rename uses new = old vs. plyr which uses old = new there is no easy way to do the above with dplyr's rename. This is pretty ugly but anyways it gives an error message:
library(dplyr)
num <- 2
BOD %>% do({ L <- setNames(list("demand"), paste0("demand", num)); rename_(., L) }) # bad
Also the following gives an error message since one cannot provide an expression where a name goes:
library(dplyr)
num <- 2
BOD %>% rename(list(paste0("demand", num) = demand)) # bad
Actually neither plyr's rename nor dplyr's rename are fully general since plyr can only easily handle expressions in the new name and dplyr in the old name. renameCol in the doBy package can handle expressions in both the old and the new name as it uses the syntax renameCol(indata, src, tgt) where src and tgt are character vectors of names and either of these or both can be complex expressions.
# rename column named paste0(base, num) to toupper(base)
# (In this case it renames column x2 to X)
library(dplyr)
base <- "x"
num <- 2
anscombe %>% doBy::renameCol(paste0(base, num), toupper(base))
The text was updated successfully, but these errors were encountered:
This works to rename the
demand
column todemand2
. Note we are using plyr'srename
, not dplyr'srename
:Since dplyr's
rename
uses new = old vs. plyr which uses old = new there is no easy way to do the above with dplyr'srename
. This is pretty ugly but anyways it gives an error message:Also the following gives an error message since one cannot provide an expression where a name goes:
Actually neither plyr's
rename
nor dplyr'srename
are fully general since plyr can only easily handle expressions in the new name and dplyr in the old name.renameCol
in the doBy package can handle expressions in both the old and the new name as it uses the syntaxrenameCol(indata, src, tgt)
wheresrc
andtgt
are character vectors of names and either of these or both can be complex expressions.The text was updated successfully, but these errors were encountered: