0

I'd like to define a series of variables in a for loop. (create a array as dictionary. Convert tops to d1 as shown below)
Firstly, I assign values to them (d1~d11); then I try to define the names of these variables.
How should I call specific variables in the names() function to make it work like "names(d1)<-..."

for (i = 1:11) 
{
    assign(paste("d",i,sep=""),tops[,2*i])
    names(eval(parse(text=paste("d",i,sep=""))))<-tops[,2*i-1]
}

> tops[,c(1,2)]

         V1   V2       V3   V4       V5   V6
1     shift 2136    shift 2211    shift 2324
2       bed 1463        k 1551    plant 1664
3       run 1338      bed 1527      run 1466
4     plant 1309      run 1504        k 1456
5         k 1294       hr 1484      bed 1390
6        hr 1285    clean 1464       hr 1366
7     check 1255    plant 1386    clean 1359
8     clean 1203    check 1261        s 1254
9         s 1052        s 1205    check 1048
10   unload 1024    start 1115      end 1028
11    chang 1023     fine 1113     fine 1020
12     fine  960    chang 1104    start 1006
13      end  924      end 1050    chang  977
14    start  905     stop  974     stop  950
15   pellet  878   pellet  915   pellet  897
16     work  866     work  907    remov  874
17      due  856   screen  900   sinter  862
18     stop  853      bwr  888     side  841
19  complet  772     side  888      due  809
20    remov  750      due  861 conveyor  792
21   requir  726  complet  841     work  777
22   sinter  711   sinter  834    north  771
23    south  710 conveyor  775    south  760
24     side  688    north  768     west  738
25     issu  682    remov  764     belt  737
26        t  675       ok  759    carri  735
27     belt  672        t  753   screen  727
28    carri  668   requir  750    stock  725
29   strand  649   unload  749   unload  719
30 conveyor  646    chute  747    chute  688
> d1
   shift      bed      run    plant        k       hr    check    clean        s 
    2136     1463     1338     1309     1294     1285     1255     1203     1052 
  unload    chang     fine      end    start   pellet     work      due     stop 
    1024     1023      960      924      905      878      866      856      853 
 complet    remov   requir   sinter    south     side     issu        t     belt 
     772      750      726      711      710      688      682      675      672 
   carri   strand conveyor 
     668      649      646 

> length(d1)
[1] 30

I hope I make it clear. if not, please free to ask me

user3552733
  • 37
  • 1
  • 9
  • 1
    1- [This is not reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). 2- [Don't use assign](http://stackoverflow.com/questions/17559390/why-is-assign-bad). 3- Try to avoid [`for` loops](http://stackoverflow.com/questions/7142767/why-are-loops-slow-in-r). In other words, add a `dput` of your data sets and show us what is your desired output – David Arenburg Jul 10 '14 at 08:32
  • Thanks for your reply.1.ok 2. If I do not use assign, how could I add values to a series of variable. 3. If I use sapply, the value will not keep when the loop finishes. Which functions do you recommend to the 2 and 3 problems? Thank you very much! – user3552733 Jul 10 '14 at 08:45
  • Again, please provide data sets and desired result. Btw, you can use `<<-` in `sapply` instead of `<-` – David Arenburg Jul 10 '14 at 08:47
  • @DavidArenburg, are you suggesting `<<-` over `assign`? – A5C1D2H2I1M1N2O1R2T1 Jul 10 '14 at 08:54
  • @AnandaMahto, not explicitly, just saying it is possible to do in `sapply` too – David Arenburg Jul 10 '14 at 09:00
  • @DavidArenburg I've already upload these data. I want to convert "tops" into several arrays (d1) as dictionary. Hoping for your advices. – user3552733 Jul 10 '14 at 09:10

1 Answers1

0

As David mentioned, don't assign 11 different variables; create a list with 11 elements. This will simplify your code considerably.

d <- lapply(1:11, function(i) tops[, 2 * i = 1])
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360