Home > database >  Create cumulative ratio column with specific criteria
Create cumulative ratio column with specific criteria

Time:11-16

I would like to create a column from the accumulated proportion of the variables x1 and x2, generating a variable similar to var5.

For example, for observation 2 the value is given by (7723 5023)/(6193 10432).

For observation 3, (7723 5023 5309)/(6193 10432 6583)

And so on. Any idea how to do this?

I appreciate any help.

Here's the example:

structure(list(qtile = structure(1:5, levels = c("1", "2", "3", 
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", 
"16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", 
"27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", 
"38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", 
"49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", 
"71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", 
"82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", 
"93", "94", "95", "96", "97", "98", "99"), class = "factor"), 
    p = c(0.563602980118794, 0.563602980118794, 0.563602980118794, 
    0.563602980118794, 0.563602980118794), x1 = c(7723, 5023, 
    5309, 4820, 4731), x2 = c(10432, 6193, 6583, 6073, 5888), 
    var5 = c(0.7403183, 0.7666767, 0.7779645, 0.7812233, 0.7849527
    ), var6 = c(0.740318251533742, 0.811077022444696, 0.806471213732341, 
    0.793676930676766, 0.803498641304348)), row.names = c(NA, 
5L), class = "data.frame")

CodePudding user response:

transform(df, vv = cumsum(x1)/cumsum(x2))

  qtile        p   x1    x2      var5      var6        vv
1     1 0.563603 7723 10432 0.7403183 0.7403183 0.7403183
2     2 0.563603 5023  6193 0.7666767 0.8110770 0.7666767
3     3 0.563603 5309  6583 0.7779645 0.8064712 0.7779645
4     4 0.563603 4820  6073 0.7812233 0.7936769 0.7812233
5     5 0.563603 4731  5888 0.7849527 0.8034986 0.7849527

Notice that vv is equal to var5

  •  Tags:  
  • r
  • Related