Home > Blockchain >  Is there a way to create a for loop to give the sum of values in a range of cells in a column based
Is there a way to create a for loop to give the sum of values in a range of cells in a column based

Time:04-05

enter image description here

I want to regroup cells from V1 based on a condition: if values in V1 are between 0 and 3 calculate the sum of the same rows in V3. form the picture I uploaded. values from V1 are between 0 and 3 and therefore we can calculate the 3 first cells in V3 (sum=12).

Next, I want to do the same for the interval [3,6] then [6,9] ... [i,i 3].

I tried to make a for loop but I couldn't figure out how to specify my arguments.

CodePudding user response:

We could use dyplr:

library(dplyr)

df %>% 
  mutate(interval = rep(row_number(), each=3, length.out = n())) %>% 
  group_by(interval) %>% 
  summarise(sum = sum(V3))
  interval   sum
     <int> <int>
1        1    12
2        2    21
3        3    30
4        4    12

data:

V1 <- 1:10
V2 <- 2:11
V3 <- 3:12

df <- tibble(V1,V2,V3)

CodePudding user response:

Or another option is gl to create grouping column

library(dplyr)
df %>% 
   group_by(interval = as.integer(gl(n(), 3, n()))) %>% 
   summarise(V3 = sum(V3))

NOTE: data from @TarJae

  • Related