0

I'm very new to R and was wondering if it was possible to create a data frame in which values of columns are dependent on one another. I would like to start with a value of 0 for B, and use this to calculate C and D, then use C and D to calculate the next B value and so on for 51 rows. Code below is just to clarify what I'm trying to accomplish.

df=data.frame(
  "A"=c(0:50),
  "B"=c(B+C-D),
  "C"=c(B*2),
  "D"=c(B/2))
  • 2
    What you're asking for is a rolling calculation. There's no way with `data.frame` to define a "live relationship" where it is automatically updated, but a rolling calc is quite doable. – r2evans Sep 26 '21 at 02:22
  • 1
    tibbles can generally work when the new input is based on previous one. `tibble::tibble(A = 0:50, B = A * 2, C = B/2)` but for your example you have created a circular dependency. Can you show first few rows of expected output ? – Ronak Shah Sep 26 '21 at 02:22
  • 1
    I think you should better define your starting condition: all three columns `B`, `C`, and `D` start with and stay `0`. BTW, perhaps https://stackoverflow.com/a/69154263/3358272 can be helpful? – r2evans Sep 26 '21 at 02:27

0 Answers0