0

I would like to produce the names of my variables automatically in R. For example in this code below, I tried to generate the variable name using paste0 and associate the names in a vector inside a for structure.

names = c('john', 'marie', 'joe')


for(i in 1:length(names)){

paste0('summary_of_', names[i]) = 2

}

It does not work and this error was shown:

target of assignment expands to non-language object

Is it possible to create the variable names in a similar way?

Adelmo Filho
  • 386
  • 1
  • 2
  • 11
  • The hint of where to look is in the error. Look at `assign`. – A5C1D2H2I1M1N2O1R2T1 Apr 18 '17 at 02:23
  • That´s exactly what I was looking for! Thx! – Adelmo Filho Apr 18 '17 at 02:30
  • 6
    Also: don't do this. It's a terrible idea. Use data.table or dplyr or anything else to do your group summaries. Or at the very least store the results in a list. But don't dynamically create variables like that. It's messy and not worth it. – Dason Apr 18 '17 at 02:31
  • `lapply` (or `Map` if multivariate) usually makes this unnecessary. – alistaire Apr 18 '17 at 02:32
  • Agree with @Dason. It's a bad idea to write your variables as object in the user environment. Learn [how to deal with list](http://stackoverflow.com/questions/2050790/how-to-correctly-use-lists-in-r) instead. – Adam Quek Apr 18 '17 at 02:32
  • 1
    regardless of how its done, `names` is a function and reserved – Phi Apr 18 '17 at 04:34

0 Answers0