Home > Net >  r data wrangling multiply and by group
r data wrangling multiply and by group

Time:03-21

I am aiming to estimate a scalar value based on the formula:

Z_t11(Z_t12   Z_t13   Z_t14)   Z_t12(Z_t13   Z_t14)   Z_t13(Z_t14)  
Z_t21(Z_t22   Z_t23   Z_t24   Z_t25)   Z_t22(Z_t23   Z_t24   Z_t25)   Z_t23(Z_t24   Z_t25)    Z_t24(Z_t25)  

Z_t11 is Z value at Time 1 for ID 1 is -1.5

Z_t13 is Z value at Time 3 for ID 1 is 0.5

 ID    Time   Z
 1     1     -1.5
 1     2     -1.5
 1     3      0.5
 1     4      0.5
 2     1     -0.5
 2     2     -0.5
 2     3     -2.0
 2     4     -1.5
 2     5      1.5

How do I caluclate:

  -1.5*(-1.5 0.5 0.5) - 1.5*(0.5 0.5)   0.5*(0.5)  
  -0.5*(-0.5-2.0-1.5 1.5) -0.5*(-2.0-1.5 1.5) -2.0*(-1.5 1.5) -1.5*1.5

CodePudding user response:

Grouped by 'ID', loop over the row_number(), use that as index to extract the 'Z' values that succeeds for each row, get the sum and multiply with the corresponding 'Z' value and then take the overall sum

library(dplyr)
library(purrr)
df1 %>% 
   group_by(ID) %>% 
   summarise(Z1 = sum(map_dbl(row_number(), 
            ~ Z[.x] * sum(Z[row_number() > .x]))))

NOTE: For the last row of each group, it returns 0 because there is no case for row_number() > n()

data

df1 <- structure(list(ID = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), Time = c(1L, 
2L, 3L, 4L, 1L, 2L, 3L, 4L, 5L), Z = c(-1.5, -1.5, 0.5, 0.5, 
-0.5, -0.5, -2, -1.5, 1.5)), class = "data.frame", row.names = c(NA, 
-9L))
  •  Tags:  
  • r
  • Related