[R] nested for loop with data table

Boris Steipe boris.steipe at utoronto.ca
Tue May 2 23:05:29 CEST 2017


There's a lot that doesn't make sense here. I think what you need to do is produce a small, reproducible example, post that with dput() and state your question more clearly - including what you have tried and what didn't work. You'll probably be amazed how quickly you will get good advice if _you_only_follow_the_posting_guide_.

B.




> On May 2, 2017, at 12:35 PM, Ek Esawi <esawiek at gmail.com> wrote:
> 
> I have a huge data file; a sample is listed below. I am using the package
> data table to process the file and I am stuck on one issue and need some
> feedback. I used fread to create a data table. Then I divided the data
> table (named File1) into 10 general subsets using common table commands
> such as:
> 
> 
> 
> AAA <- File1[Num<5&day>15]
> 
> BBB <- File1[Num>15&day<10]
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> …..
> 
> 
> 
> I wanted to divide and count each of the above subsets based on a set of
> parameters common to all subsets. I did the following to go through each
> subset and it works:
> 
> For (I in 1: length (AAA)) {
> 
>              aa <- c(AAA[color==”green”&grade==”a”,month==”Januray” .N],[
> AAA[color==”green”&grade==”b”& month==”June”’ .N])
> 
> }
> 
> 
> 
> The question: I don’t want to have a separate loop for each subset (10
> loops). Instead, I was hoping to have 2 nested loops in the form below:
> 
> 
> 
> For (I in 1:N)){
> 
>              For (j in 1:M){
> 
> 
> 
> }
> 
> }
> 
> 
> 
> Sample
> 
> 
> Num
> 
> Color
> 
> Grade
> 
> Value
> 
> Month
> 
> Day
> 
> 1
> 
> yellow
> 
> A
> 
> 20
> 
> May
> 
> 1
> 
> 2
> 
> green
> 
> B
> 
> 25
> 
> June
> 
> 2
> 
> 3
> 
> green
> 
> A
> 
> 10
> 
> April
> 
> 3
> 
> 4
> 
> black
> 
> A
> 
> 17
> 
> August
> 
> 3
> 
> 5
> 
> red
> 
> C
> 
> 5
> 
> December
> 
> 5
> 
> 6
> 
> orange
> 
> D
> 
> 0
> 
> January
> 
> 13
> 
> 7
> 
> orange
> 
> E
> 
> 12
> 
> January
> 
> 5
> 
> 8
> 
> orange
> 
> F
> 
> 11
> 
> February
> 
> 8
> 
> 9
> 
> orange
> 
> F
> 
> 99
> 
> July
> 
> 23
> 
> 10
> 
> orange
> 
> F
> 
> 70
> 
> May
> 
> 7
> 
> 11
> 
> black
> 
> A
> 
> 77
> 
> June
> 
> 11
> 
> 12
> 
> green
> 
> B
> 
> 87
> 
> April
> 
> 33
> 
> 13
> 
> black
> 
> A
> 
> 79
> 
> August
> 
> 9
> 
> 14
> 
> green
> 
> A
> 
> 68
> 
> December
> 
> 14
> 
> 15
> 
> black
> 
> C
> 
> 90
> 
> January
> 
> 31
> 
> 16
> 
> green
> 
> D
> 
> 79
> 
> January
> 
> 11
> 
> 17
> 
> black
> 
> E
> 
> 101
> 
> February
> 
> 17
> 
> 18
> 
> red
> 
> F
> 
> 90
> 
> July
> 
> 21
> 
> 19
> 
> red
> 
> F
> 
> 112
> 
> February
> 
> 13
> 
> 20
> 
> red
> 
> F
> 
> 101
> 
> July
> 
> 20
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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