I have created some variables. I would like to duplicate these so that they exist twice, once with the name you see below, and once with Ireland_ in front of their name, i.e.,
c_PFS_Folfox = 307.81
would become:
Ireland_c_PFS_Folfox = 307.81
I initially define these as follows:
c_PFS_Folfox <- 307.81
c_PFS_Bevacizumab <- 2580.38
c_OS_Folfiri <- 326.02
administration_cost <- 365.00
c_AE1 <- 2835.89
c_AE2 <- 1458.80
c_AE3 <- 409.03
n_wtp = 45000
Then I put them together to rename all at once:
kk <- data.frame(c_PFS_Folfox, c_PFS_Bevacizumab, c_OS_Folfiri, administration_cost, c_AE1, c_AE2, c_AE3, n_wtp)
colnames(kk) <- paste("Ireland", kk, sep="_")
kk
Ireland_307.81 Ireland_2580.38 Ireland_326.02 Ireland_365 Ireland_2835.89 Ireland_1458.8
1 307.8 2580 326 365 2836 1459
Ireland_409.03 Ireland_45000
1 409 45000
Obviously this isn't the output I intended. These also don't exist as new variables in the environment.
What can I do?
If we want to create objects with Ireland_
as prefix, either use
list2env(setNames(kk, paste0("Ireland_", names(kk))), .GlobalEnv)
Once we created the objects in the global env, we may remove the original objects
> rm(list = names(kk))
> ls()
[1] "Ireland_administration_cost" "Ireland_c_AE1" "Ireland_c_AE2" "Ireland_c_AE3" "Ireland_c_OS_Folfiri"
[6] "Ireland_c_PFS_Bevacizumab" "Ireland_c_PFS_Folfox" "Ireland_n_wtp" "kk"
or with %=%
from collapse
library(collapse)
paste("Ireland", colnames(kk), sep="_") %=% kk
-checking
> Ireland_administration_cost
[1] 365
> Ireland_c_PFS_Folfox
[1] 307.81