[R] nested for loop with data table

Ek Esawi esawiek at gmail.com
Tue May 2 18:35:37 CEST 2017


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]]



More information about the R-help mailing list