Addition problem in a for loop

Hi! I begin on Rstudio. I dispose of 20 years daily data of evapotranspiration. I would like to add the daily evapotranspiration values for each 10 days of each month, and thus have 3 values per month. That is:
1-10 ; 11-20 , 21-end of the month

Here is the loop I typed to perform this operation. It does not work but the code runs:

j=i10
k=0
for (i in 2:nrow(data4)){
j=i
10
for (j in 11:nrow(data4)){
if ((data4$month[j]==data4$month[j-10]) && (k<3)){
k=k+1
for (l in (1:10)){
d<-sum(data4$etp)
}
}else{
k=0
}
}
}

Someone could help me please? :slight_smile:

The code still work but i get a table with hundred thousand results.
Here is the current code:

j=i*10
k=0
d2<-0
for (i in 2:nrow(data4)){
  j=i*10
for (j in 11:nrow(data4)){
  if ((month[j]==month[j-10]) && (k<3)){
    k=k+1
    for (l in (1:10)){
      d<-sum(data4$etp[l])
      d2<-rbind(d2,d)
    }
  }else{
    k=0
  } 
}
}

in R community there is often a preference to work at table level in tidy ways using functional programming styles. This has been facilitated by tidyverse packages etc. I would think you could achieve the sort of calculations you have in mind through this style and avoid all for loops, the resulting code will likely be easier to follow.
However , its challenging to suggest solutions over unseen data and data structures.
Please follow this attached guide on how to make a reproducible example, which means data as well as code.


Short Version

You can share your data in a forum friendly way by passing the data to share to the dput() function.
If your data is too large you can use standard methods to reduce it before sending to dput().
When you come to share the dput() text that represents your data, please be sure to format your post with triple backticks on the line before your code begins to format it appropriately.

```
( example_df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.3, 0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")))
```

Thank you for your answer, here is a piece of my dataset

  year month day      mtime etp
1  2001     1   1 2001-01-01 0.7
2  2001     1   2 2001-01-02 0.6
3  2001     1   3 2001-01-03 0.7
4  2001     1   4 2001-01-04 0.8
5  2001     1   5 2001-01-05 0.5
6  2001     1   6 2001-01-06 0.5
7  2001     1   7 2001-01-07 0.5
8  2001     1   8 2001-01-08 0.3
9  2001     1   9 2001-01-09 0.3
10 2001     1  10 2001-01-10 0.3
11 2001     1  11 2001-01-11 0.3
12 2001     1  12 2001-01-12 0.4
13 2001     1  13 2001-01-13 0.3
14 2001     1  14 2001-01-14 0.2
15 2001     1  15 2001-01-15 0.3
16 2001     1  16 2001-01-16 0.2
17 2001     1  17 2001-01-17 0.1
18 2001     1  18 2001-01-18 0.1
19 2001     1  19 2001-01-19 1.0
20 2001     1  20 2001-01-20 0.9

I'll check tidyverse package, but I have not found informations to resolve my problem

Thanks but that is not how to share data in a forum friendly way.

I'm so sorry but i don't understand what you mean by the wrong way to publish my data. Something is missing?

etp are daily evapotranspiration values, I have 20 years of data. I would like to add the data on the same 10 days of each month, so that there are 3/month. (1-10, 11-20, 21-end of month)

You can share your data in a forum friendly way by passing the data to share to the dput() function.
If your data is too large you can use standard methods to reduce it before sending to dput().
When you come to share the dput() text that represents your data, please be sure to format your post with triple backticks on the line before your code begins to format it appropriately.

```
( example_df <- structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 
5, 4.4, 4.9), Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9, 3.4, 
3.4, 2.9, 3.1), Petal.Length = c(1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 
1.4, 1.5, 1.4, 1.5), Petal.Width = c(0.2, 0.2, 0.2, 0.2, 0.2, 
0.4, 0.3, 0.2, 0.2, 0.1), Species = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("setosa", "versicolor", "virginica"
), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame")))
```

ok i understand thank you

(df<-structure(list(year = c(2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 
2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L), month = c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), day = 1:31, 
    mtime = c("2001-01-01", "2001-01-02", "2001-01-03", "2001-01-04", 
    "2001-01-05", "2001-01-06", "2001-01-07", "2001-01-08", "2001-01-09", 
    "2001-01-10", "2001-01-11", "2001-01-12", "2001-01-13", "2001-01-14", 
    "2001-01-15", "2001-01-16", "2001-01-17", "2001-01-18", "2001-01-19", 
    "2001-01-20", "2001-01-21", "2001-01-22", "2001-01-23", "2001-01-24", 
    "2001-01-25", "2001-01-26", "2001-01-27", "2001-01-28", "2001-01-29", 
    "2001-01-30", "2001-01-31"), etp = c(0.7, 0.6, 0.7, 0.8, 
    0.5, 0.5, 0.5, 0.3, 0.3, 0.3, 0.3, 0.4, 0.3, 0.2, 0.3, 0.2, 
    0.1, 0.1, 1, 0.9, 1.4, 2.6, 5, 3, 0.6, 0.6, 0.7, 0.3, 0.3, 
    0.3, 0.4)), row.names = c(NA, 31L), class = "data.frame"))

one way to do it, under the assumption that your example data.frame is named 'df'

library(tidyverse)

(df2 <-df %>% 
    mutate(grouping=case_when(day<=10~"1 - 10",
                              day<=20~"11 - 20",
                              TRUE ~"21 - eom")) %>%
    group_by(year,
             month,
             grouping) %>% 
    summarise(sum_of_values = sum(etp,na.rm=TRUE)))
2 Likes

It works, Thank you very much!!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.