I have the following data and I am trying to extract the final value of the cumsum
column. For instance I would like to take row 8 from the dataframe with cumsum
value 0.7810417
then do the same for 1115
with cumsum
value 0.7282639
and 1116
... 1117
etc.
structure(list(WEEK = c(1114L, 1114L, 1114L, 1114L, 1114L, 1114L,
1114L, 1114L, 1115L, 1115L, 1115L, 1115L, 1115L, 1115L, 1115L,
1116L, 1116L, 1116L, 1116L, 1116L, 1116L, 1117L, 1117L, 1117L,
1117L, 1118L, 1118L, 1118L, 1118L, 1118L, 1118L, 1118L, 1119L,
1119L, 1119L, 1119L, 1119L, 1119L, 1119L, 1119L, 1120L, 1120L,
1120L, 1120L, 1120L, 1120L, 1120L, 1121L, 1121L, 1121L), price_per_ounce = c(0.124583333333333,
0.0832638888888889, 0.100972222222222, 0.100972222222222, 0.10375,
0.0832638888888889, 0.100972222222222, 0.0832638888888889, 0.124583333333333,
0.0855555555555556, 0.109166666666667, 0.0832638888888889, 0.110972222222222,
0.10375, 0.110972222222222, 0.124583333333333, 0.0841666666666667,
0.110972222222222, 0.110972222222222, 0.0832638888888889, 0.110972222222222,
0.0838888888888889, 0.110972222222222, 0.0832638888888889, 0.110972222222222,
0.124583333333333, 0.0786111111111111, 0.110972222222222, 0.110972222222222,
0.10375, 0.110972222222222, 0.0832638888888889, 0.124583333333333,
0.0925, 0.110972222222222, 0.0832638888888889, 0.110972222222222,
0.10375, 0.110972222222222, 0.0832638888888889, 0.124583333333333,
0.0844444444444444, 0.110972222222222, 0.0832638888888889, 0.10375,
0.110972222222222, 0.0832638888888889, 0.124583333333333, 0.0694444444444444,
0.110972222222222), Total = c(0.124583333333333, 0.207847222222222,
0.308819444444444, 0.409791666666667, 0.513541666666667, 0.596805555555556,
0.697777777777778, 0.781041666666667, 0.124583333333333, 0.210138888888889,
0.319305555555556, 0.402569444444444, 0.513541666666667, 0.617291666666667,
0.728263888888889, 0.124583333333333, 0.20875, 0.319722222222222,
0.430694444444444, 0.513958333333333, 0.624930555555556, 0.0838888888888889,
0.194861111111111, 0.278125, 0.389097222222222, 0.124583333333333,
0.203194444444444, 0.314166666666667, 0.425138888888889, 0.528888888888889,
0.639861111111111, 0.723125, 0.124583333333333, 0.217083333333333,
0.328055555555556, 0.411319444444444, 0.522291666666667, 0.626041666666667,
0.737013888888889, 0.820277777777778, 0.124583333333333, 0.209027777777778,
0.32, 0.403263888888889, 0.507013888888889, 0.617986111111111,
0.70125, 0.124583333333333, 0.194027777777778, 0.305)), .Names = c("WEEK",
"price_per_ounce", "Total"), class = c("data.table", "data.frame"
), row.names = c(NA, -50L), .internal.selfref = <pointer: 0x0000000008450788>)
EDIT: The results I obtain when I set the dataframe to be df <- head(df, 100)
structure(list(df = c(0.781041666666667, 0.728263888888889, 0.624930555555556,
0.389097222222222, 0.723125, 0.820277777777778, 0.70125, 0.658611111111111,
0.769583333333333, 0.759027777777778, 0.751666666666667, 0.741597222222222,
0.519930555555556, 0.712152777777778)), .Names = "df", row.names = c(NA,
-14L), class = "data.frame")
However when I run in on the whole dataframe I get the following.
structure(list(df = c(220.124649739256, 199.217289598068, 199.774511556463,
206.738587849235, 205.766197136359, Inf, Inf, Inf, 205.103350187295,
199.567357907284, 212.900103648094, 200.477169383407, Inf, 203.441435413023
)), .Names = "df", row.names = c(NA, 14L), class = "data.frame")
I am unsure why there are Inf
values now appearing, also the values change significantly, however all I did was skip this command df <- head(df, 100)
Here is the code I am applying (The same thing happens when I apply all the solutions below)
Price <- data %>%
select(WEEK, price_per_ounce)
test <- transform(Price, Total = ave(price_per_ounce, WEEK, FUN = cumsum))
test <- head(test, 100)
#dput(test)
df <- test[, Total[.N], WEEK]$V1
df <- as.data.frame(df)
df <- head(df, 14)
dput(df)