Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a parameter as a variable name inside a function in R

Tags:

r

I am trying to incorporate the name of the month in the name of the variable being stored.

import <- function(month) {
  dataobj <- letters
  assign("x", dataobj)
  save("x", file="data.rda")
}

works. But the following doesn't work -

import <- function(month) {
  dataobj <- letters
  assign(substr(month, 1, 3), dataobj)
  save(substr(month, 1, 3), file="data.rda")
}

It seems that save() will accept "x" but not substr(month, 1, 3).

Any ideas how to fix this?

like image 541
Soumendra Avatar asked Dec 20 '22 22:12

Soumendra


1 Answers

Use the list argument of save():

save(list=substr(month,1,3), file="data.rda")
like image 181
baptiste Avatar answered Jan 20 '23 22:01

baptiste