I am new to R and specially programming function.So, I would highly appreciate if you please try to help to understand the concepts.
I'd like to know how I can write a function to do the following acts:
1- add the values of certain variable(column) line by line (row by row) and 2- then if the sum of the value would be greater than 100; >100
3- then, stop and report all of the rows from begging till it meets the criteria (total sum <100) to stop?
As an example :
DT <- data.table(ID = c(1:21),Var1 = LETTERS[1:21], Var2 = seq(9, 19, .5))
> DT
ID Var1 Var2
1: 1 A 9.0
2: 2 B 9.5
3: 3 C 10.0
4: 4 D 10.5
5: 5 E 11.0
6: 6 F 11.5
7: 7 G 12.0
8: 8 H 12.5
9: 9 I 13.0
10: 10 J 13.5
11: 11 K 14.0
12: 12 L 14.5
13: 13 M 15.0
14: 14 N 15.5
15: 15 O 16.0
16: 16 P 16.5
17: 17 Q 17.0
18: 18 R 17.5
19: 19 S 18.0
20: 20 T 18.5
21: 21 U 19.0
ID Var1 Var2
I'd like to add all of the value in Var2 row by row until the total sum would be > 100 then it stops and report all the rows from beginning till it meet this criteria.
I this case I mean, it add all of the values in Var2 from ID 1:9 (9.0 +9.5+10+10.5+11+11.5+12+12.5+13) which the total would be 99 and if add the next row (13.5) the values would be 112.5.
therefore the expected output would be :
ID Var1 Var2
1: 1 A 9.0
2: 2 B 9.5
3: 3 C 10.0
4: 4 D 10.5
5: 5 E 11.0
6: 6 F 11.5
7: 7 G 12.0
8: 8 H 12.5
9: 9 I 13.0
I tried different things , but to me they were not useful at all, hence, I am not including them here .
Aucun commentaire:
Enregistrer un commentaire