1

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:

1. Cost of treatment in this country

c_PFS_Folfox <- 307.81
c_PFS_Bevacizumab <- 2580.38  
c_OS_Folfiri <- 326.02  
administration_cost <- 365.00

2. Cost of treating the AE conditional on it occurring

c_AE1 <- 2835.89
c_AE2 <- 1458.80
c_AE3 <- 409.03

3. Willingness to pay threshold

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?

Phil
  • 7,287
  • 3
  • 36
  • 66

2 Answers2

0

First put all your variables in a vector, then use sapply to iterate the vector to assign the existing variables to a new variable with the prefix "Ireland_".

your_var <- c("c_PFS_Folfox", "c_PFS_Bevacizumab", "c_OS_Folfiri", 
              "administration_cost", "c_AE1", "c_AE2", "c_AE3", "n_wtp")

sapply(your_var, \(x) assign(paste0("Ireland_", x), get(x), envir = globalenv()))
benson23
  • 16,369
  • 9
  • 19
  • 38
  • Thank you, I'm not sure I fully understand your code, as I get the following error when applying this: `Error: Incomplete expression: sapply(your_var, \(x) assign(paste0("Ireland_", x), get(x), envir = globalenv())` – James Moore Jan 14 '23 at 03:28
  • 1
    @JamesMoore I'm not sure what this error means, I re-run the code on my end without error. Can you try replacing `\(x)` with `function(x)`? Also not sure if it's your copy-paste typo or what, there should be three closing brackets at the very end of the command (you only have two). – benson23 Jan 14 '23 at 03:32
  • it was my typo, sorry. I was wondering if there is a way I could apply your code to similarly name a dataframe when I create it, i.e. `df <- data.frame(x=rnorm(100))`, but `paste0("df_", country_name[1], "_", sep = "") <- data.frame(x=rnorm(100))` – James Moore Jan 23 '23 at 18:31
0

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
akrun
  • 874,273
  • 37
  • 540
  • 662