How to covert a character string into a list variable in R?

84 views Asked by At

I would like to convert a character string into a variable name for a list variable, Here is an example.

   x=rnorm(1:100)
   y=rnorm(1:100)*5+10
   OneDimModel = NULL; 
   OneDimModel$model =  lm(y~x)

I would like to reassign list variable OneDimModel for one individual case to a summarising variable paste("DataCentre$sitenumber", i, sep=""), where [i] can be varied across different cases.

So the end product would be something like for case 1:

  DataCentre$sitenumber1 = OneDimModel

I have tried function assign(), but it only works for single value, could someone help me.

1

There are 1 answers

1
romants On BEST ANSWER

You need to do use extract operator [[

text.index <- paste("sitenumber", i, sep="")
DataCentre[[text.index]] <- OneDimModel

Or simply

DataCentre[[paste("sitenumber", i, sep="")]] <- OneDimModel