[R] recall variable in a loop

Marco Chiapello chiapello.m at gmail.com
Sat May 10 11:07:29 CEST 2014


I'd like to create several tables in which my data are stored.
I did the follow:
    
    x<-mydata
    splitted<-split(x[,1],x[,2])
    
    #Create my function for the tables
    fxdata<-function(){
    y.row=c("0","1","2","3","4","5")
    yy=c(rep(0,6))
    w=data.frame(A3=yy,A2=yy,A1=yy,A0=yy)
    rownames(w)<-y.row}

    #Create the tables
    xx<-NULL
    for (i in 1:dim(x)[2]){
    assign(paste("xx",i,sep=""),fxdata())}

So, I created, for example, 3 tables: xx1, xx2 and xx3. Now I have to populate the tables and here I have the problem. I do not know how to recall xx1, xx2 or xx3 in a loop. The idea is insert the follow loop in the previous one, in order to populate all the tables.

     #Populate the table
     for (ii in 1:length(splitted[[1]]{
     #IF 0
     if (splitted[[1]][ii] == "0"){
     y[1,4]=y[1,4]+1}
     #IF A0
     if (splitted[[1]][ii] == "1A0"){
     y[2,4]=y[2,4]+1}
     if (splitted[[1]][ii] == "2A0"){
     y[3,4]=y[3,4]+1}
     if (splitted[[1]][ii] == "3A0"){
     y[4,4]=y[4,4]+1}
     ...}

The question is: what I have to write in place of "y" in the last loop in order to refer to xx1, xx2 or xx3?? I want to write something like:
   
    for (i in 1:3) {
    xxi<-1}

Thank you for any advice.

Marco


More information about the R-help mailing list