0

I want to create a loop to produce variable names with index. I think 2i-1 will do as I wanted but it generates error.

Week1Day1 is a list with two rows and 6 columns.

for ( i in 1:6 ) {
  paste("Week1Day1Plot",2*i-1,sep="")=conversion(Week1Day1[1,i])
}

Error in paste("Week1Day1Plot", 2 * i - 1, sep = "") = conversion(Week1Day1[1, : target of assignment expands to non-language object

Kane
  • 163
  • 10

1 Answers1

2

Use assign.

for ( i in 1:6 ) {
  assign(paste("Week1Day1Plot",2*i-1,sep=""), i)
}
ls()
[1] "i"               "Week1Day1Plot1"  "Week1Day1Plot11" "Week1Day1Plot3"
[5] "Week1Day1Plot5"  "Week1Day1Plot7"  "Week1Day1Plot9"
rawr
  • 20,481
  • 4
  • 44
  • 78
Josh
  • 1,248
  • 12
  • 25