3

I have been using "For loops" before this. But the variable is usually k that refer to row numbers.

Example:

for (k in 1:n) { 
    expression
}

My question is, is it possible for the variable to be a certain column? Example:

for ("column no" in 1:n) { 
    expression
}

I have had several trials and errors and a bit stuck now. Here is my data:

date    mold    no
22-May  1.35436 1
23-May  0.88592 1
24-May  0.81316 1
25-May  0.80856 1
26-May  0.84646 1
27-May  0.81762 1
28-May  0.79828 1
03-Jan  1.09158 2
04-Jan  0.86661 2
05-Jan  0.81908 2
06-Jan  0.7555  2
07-Jan  0.66577 2
08-Jan  0.66706 2
09-Jan  0.67133 2
05-Feb  20.4366 3
06-Feb  5.77923 3
06-Feb  3.12323 3
05-Feb  2.25436 3
06-Feb  1.74551 3
06-Feb  1.52744 3
05-Feb  1.45483 3
28-Jul  1.55148 4
29-Jul  1.18882 4
30-Jul  1.10595 4
31-Jul  1.14101 4
01-Aug  1.1453  4
02-Aug  1.10113 4
03-Aug  1.09152 4
30-Nov  8.3254  5
01-Dec  4.03003 5
02-Dec  2.18026 5
03-Dec  1.40028 5
04-Dec  1.02901 5
05-Dec  0.85859 5
06-Dec  0.7776  5

I would like to as R to sum up the values in the mold column for each group (1 to 5) in the no column. For example, for no=1, it would be

1.35436 + 0.88592 + 0.81316 + 0.80856 + 0.84646 + 0.81762 + 0.79828 = 6.32436

Then repeat for no = 2, 3, 4 etc.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Candice
  • 67
  • 7
  • 1
    you can use `aggregate` without any `for` loop , `aggregate(mold~no, df, sum)` – Ronak Shah Sep 21 '19 at 03:58
  • Thanks. I tried to search. Can you provide any links to similar previously asked questions? – Candice Sep 21 '19 at 04:01
  • @Candice Yes, you can use `for` loops by getting the unique values of that columns and looping thrugh tit – akrun Sep 21 '19 at 04:02
  • 1
    yes, this has been asked before. There are lot of options here https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group using various packages and base R. – Ronak Shah Sep 21 '19 at 04:03
  • 1
    It is not a dupe of the questtion linked as it is specifically about `for` loop – akrun Sep 21 '19 at 04:04
  • 1
    Yes. For some reasons, i prefer this to be in a loop instead of using aggregate. The function that i am going to use will output a list of results therefore making it messy for aggregate to display the results.. The sum function that i have used in my example is just a simplified version for asking. – Candice Sep 21 '19 at 04:10
  • you can use `ave` without any `for` loop, `df$mold_sum <- with(df, ave(mold, no, FUN=sum))` – Parfait Sep 21 '19 at 14:08

1 Answers1

1

We can loop through the unique elements, compare (==) and get the sumof the 'mold' elements that correspond to the boolean vector

un1 <- unique(df1$no)
v1 <- numeric(length(un1))

for(i in seq_along(v1)) v1[i] <- sum(df1$mold[df1$no== un1[i]])
v1
#[1]  6.32436  5.53693 36.32120  8.32521 18.60117

It is the same as rowsum

rowsum(df1$mold, df1$no)[,1]
#        1        2        3        4        5 
#  6.32436  5.53693 36.32120  8.32521 18.60117 

data

df1 <- structure(list(date = c("22-May", "23-May", "24-May", "25-May", 
"26-May", "27-May", "28-May", "03-Jan", "04-Jan", "05-Jan", "06-Jan", 
"07-Jan", "08-Jan", "09-Jan", "05-Feb", "06-Feb", "06-Feb", "05-Feb", 
"06-Feb", "06-Feb", "05-Feb", "28-Jul", "29-Jul", "30-Jul", "31-Jul", 
"01-Aug", "02-Aug", "03-Aug", "30-Nov", "01-Dec", "02-Dec", "03-Dec", 
"04-Dec", "05-Dec", "06-Dec"), mold = c(1.35436, 0.88592, 0.81316, 
0.80856, 0.84646, 0.81762, 0.79828, 1.09158, 0.86661, 0.81908, 
0.7555, 0.66577, 0.66706, 0.67133, 20.4366, 5.77923, 3.12323, 
2.25436, 1.74551, 1.52744, 1.45483, 1.55148, 1.18882, 1.10595, 
1.14101, 1.1453, 1.10113, 1.09152, 8.3254, 4.03003, 2.18026, 
1.40028, 1.02901, 0.85859, 0.7776), no = c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L)),
class = "data.frame", row.names = c(NA, 
-35L))
akrun
  • 874,273
  • 37
  • 540
  • 662