help in RStudio

I have data as in the picture

I want to make a code in the RStudio program that takes the values from the column of the number of claims, and if this value is 1, it takes the first value in column group 1 and the first value in column group 2, and if the value is 2, it takes the first and second value and combines them together from the column group 1 and also does He takes the first and second values and collects them from the column of a group of 2 and so on. The values of one group column are stored separately from the sum of the values of the column of group 2.
Can anyone help me with that? Thank you in advance.

I am not sure I understand your goal correctly. Is this close to what you want?

DF <- data.frame(Number_of_Claims = c(1,3,2,5,5),
                 Group_1 = c(2.5, 1.5, 3.6, 4.6, 5),
                 Group_2 = c(1.6, 1.6, 2.4, 3.9, 5.9))
sumFunc <- function(C) {
  SUM1 <- sum(DF[1:C, "Group_1"])
  SUM2 <- sum(DF[1:C, "Group_2"])
  data.frame( Number_of_Claims = C, Grp1_Sum = SUM1, Grp2_Sum = SUM2)
}

library(purrr)

SUMS <- map_dfr(DF$Number_of_Claims,sumFunc)
SUMS
#>   Number_of_Claims Grp1_Sum Grp2_Sum
#> 1                1      2.5      1.6
#> 2                3      7.6      5.6
#> 3                2      4.0      3.2
#> 4                5     17.2     15.4
#> 5                5     17.2     15.4

Created on 2022-09-26 with reprex v2.0.2

1 Like

Thank you for your help.

Sorry dear, if I want to store the values taken from column (group 1) and column (group 2) before grouping them, how can I do that? Thank you.

Do you want something like this?

DF <- data.frame(Number_of_Claims = c(1,3,2,5,5),
                 Group_1 = c(2.5, 1.5, 3.6, 4.6, 5),
                 Group_2 = c(1.6, 1.6, 2.4, 3.9, 5.9))
sumFunc <- function(C) {
  SUM1 <- sum(DF[1:C, "Group_1"])
  SUM2 <- sum(DF[1:C, "Group_2"])
  data.frame( Grp1_Sum = SUM1, Grp2_Sum = SUM2)
}

library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
SUMS <- map_dfr(DF$Number_of_Claims,sumFunc)
SUMS
#>   Grp1_Sum Grp2_Sum
#> 1      2.5      1.6
#> 2      7.6      5.6
#> 3      4.0      3.2
#> 4     17.2     15.4
#> 5     17.2     15.4
DF <- bind_cols(DF,SUMS)
DF
#>   Number_of_Claims Group_1 Group_2 Grp1_Sum Grp2_Sum
#> 1                1     2.5     1.6      2.5      1.6
#> 2                3     1.5     1.6      7.6      5.6
#> 3                2     3.6     2.4      4.0      3.2
#> 4                5     4.6     3.9     17.2     15.4
#> 5                5     5.0     5.9     17.2     15.4

Created on 2022-09-27 with reprex v2.0.2

1 Like

Thank you very much.

This topic was automatically closed 7 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.