[R] R for loop nested?

R. Michael Weylandt michael.weylandt at gmail.com
Tue Oct 25 16:47:56 CEST 2011


In what way doesn't this work?

You declare xx with one element and it prints.

Then on the next loop, you add another element, and it prints them both.

Then on the next loop, you add another element, and it prints all three.

And so on...

If you look at the output, you can see that it's a growing vector.
Perhaps you just mean to put print(xx) outside the loop to only get
the single final answer.

Michael

PS -- Those are multiple matrices, not just a single "crazy matrix."

On Tue, Oct 25, 2011 at 8:19 AM, Delia Shelton <delsshel at indiana.edu> wrote:
> Hi,
>
> I'm trying to execute the same R code on multiple data frames listed in a single directory. The code works fine if I use the code
> (below) for each file. However, I have several files and it becomes
> tedious to run each one, name it and then aggregate into a single
> dataframe.
>
> Name
> 0.0           1
> 21.15       2
> 2400.26   1
> 3222.14   2
>
>
> name = read.table(file.choose(),header=F) # opening a data file
> colnames(name)<-c("Time", "Behavior")
> name = data.frame(name$Behavior, name$Time)
> colnames(name)<-c("Behavior", "Time")
> name<-name[name$Time < 3600, ];
>
>
> x<-seq(0,3600, by = 60) # total time partition by time which is 60
>
> if (tail(name$Behavior, 1) == 1) {name<-rbind(name, c(4, 3600))} else
> {name<-rbind(name, c(1, 3600))}
>
> if (((length(name$Behavior))) %% 2 != 0)
>  {name <-name[-c(length(name$Behavior)), -c(length(name$Behavior))]}
>
> q<-c()
> for (y in (1: (length(name$Behavior))))
> {
>         if (y %% 2 == 0) {next} else
>         {q<-c(q, (c(name$Time[y]:name$Time[y +1])))}
> }
>
> b<-table(cut(q,x))
>
> sum(b)
>
>
> So, I tried to nest a for loop within another(code
> below). It solved the problem of selecting each data file. However, a
> problem was incurred with the second loop. It gave back a crazy matrix.
>
> setwd("/Users/deliashelton/Documents/Shelton
>  back-up 11:21/labs/ABL meetings/DS7 Flow/DS7.5/Observers/AA 7.5/AA
> PND2/AA PND 2 22C")
>
> a<- list.files()
> xx<- c()
> t<-seq(0,3600, by = 60)
>
> for (i in a){
>         name<- read.table(i,header=F)
>         colnames(name)=c("Time", "Behavior")
>         name<- data.frame(name$Behavior, name$Time)
>         colnames(name)<-c("Behavior", "Time")
>         name<-name[name$Time < 3600, ]
>
>         if (tail(name$Behavior, 1) == 1) {name<-rbind(name, c(4, 3600))} else
> {name<-rbind(name, c(1, 3600))}
>
> if (((length(name$Behavior))) %% 2 != 0)
>  {name <-name[-c(length(name$Behavior)), -c(length(name$Behavior))]}
>
>
> #xx <- rbind(xx, name)
>
>  # total time partition by time which is 60
>
>
>         q<-c()
>         for (y in (1: (length(name$Behavior))))
>         {
>                 if (y %% 2 == 0) {next} else
>                 {q<-c(q, (c(name$Time[y]:name$Time[y +1])))}
>
>
>                         }
>
>         b<-table(cut(q,x))
>         xx <- rbind(xx, sum(b))
>
>         print(xx)
> }
>
> Crazy matrix:
>
>  [,1]
> [1,] 4947
>      [,1]
> [1,] 4947
> [2,] 7318
>      [,1]
> [1,] 4947
> [2,] 7318
> [3,] 8598
>      [,1]
> [1,] 4947
> [2,] 7318
> [3,] 8598
> [4,] 9617
>       [,1]
> [1,]  4947
> [2,]  7318
> [3,]  8598
> [4,]  9617
> [5,] 11755
>       [,1]
> [1,]  4947
> [2,]  7318
> [3,]  8598
> [4,]  9617
> [5,] 11755
> [6,] 13762
>       [,1]
> [1,]  4947
> [2,]  7318
> [3,]  8598
> [4,]  9617
> [5,] 11755
> [6,] 13762
> [7,] 17363
>       [,1]
> [1,]  4947
> [2,]  7318
> [3,]  8598
> [4,]  9617
> [5,] 11755
> [6,] 13762
> [7,] 17363
> [8,] 19857
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
> [10,] 25509
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
> [10,] 25509
> [11,] 28071
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
> [10,] 25509
> [11,] 28071
> [12,] 31672
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
> [10,] 25509
> [11,] 28071
> [12,] 31672
> [13,] 35268
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
> [10,] 25509
> [11,] 28071
> [12,] 31672
> [13,] 35268
> [14,] 38440
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
> [10,] 25509
> [11,] 28071
> [12,] 31672
> [13,] 35268
> [14,] 38440
> [15,] 41796
>        [,1]
>  [1,]  4947
>  [2,]  7318
>  [3,]  8598
>  [4,]  9617
>  [5,] 11755
>  [6,] 13762
>  [7,] 17363
>  [8,] 19857
>  [9,] 23372
> [10,] 25509
> [11,] 28071
> [12,] 31672
> [13,] 35268
> [14,] 38440
> [15,] 41796
> [16,] 43142
>
>
> What I would like is for a recursive function/loop that
> follows the initial codes computation, but does it for all data files in the directory and binds the outputs into a single vector. If you have
> any tips on getting the code to work or can help identify the problem,
> your assistance would be appreciated.
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



More information about the R-help mailing list