0

Excel function I want to replicate in R

In R I'm looking to create a row calculation that finds price standard deviation based on Product type and a date range based on Order_Date. In the example from Excel, I find the standard deviation of Price (column B) of Product B (row 4) based on other Product Bs that were ordered within a 30-day period of row 4's Order_Date (column C), meaning I want to find the standard deviation of all Product B price that occurred within +/- 15 days of row 4's order.

This post is similar to my question; however, I'm struggling to draw parallels between mine and the one asked here: R Dplyr mutate, calculating standard deviation for each row

Here's what I tried in R:

dfdata <- dfdata %>%
  arrange(Order_Date) %>%
  mutate(
    Price_STDEV = slide_index_dbl(
      Price, Order_Date, stdev, .before = days(15), .after = days(15)
    ),
    .by = Product
  )

I get the following error: ! object 'stdev' not found. Given that stdev isn't operational, I've looking into arrange with mutate solutions but haven't found any luck. Earlier I asked a question similar to this but looking at averages. Lastly, similar to the Excel function, if there's an error I'd like the function to remove the row or put zero.

Here's my dataset:

structure(list(Product = c("A", "B", "B", "F", "D", "F", "A", 
"F", "D", "A", "A", "D", "C", "C", "A", "B", "C", "A", "B", "A", 
"A", "E", "D", "F", "B", "B", "E", "F", "F", "E", "F", "A", "A", 
"D", "F", "C", "C", "C", "A", "D", "D", "E", "D", "B", "C", "B", 
"D", "F", "C", "A", "A", "F", "D", "E", "B", "B", "A", "E", "A", 
"D", "E", "C", "C", "C", "E", "D", "F", "E", "B", "E", "D", "B", 
"A", "B", "D", "F", "C", "C", "E", "A", "A", "F", "D", "D", "A", 
"F", "C", "A", "A", "F", "A", "B", "A", "D", "C", "C", "A", "B", 
"E", "B", "D", "B", "F", "F", "B", "C", "B", "B", "C", "F", "A", 
"B", "A", "E", "C", "E", "E", "E", "D", "B", "C", "D", "B", "C", 
"F", "F", "C", "F", "D", "F", "A", "D", "B", "B", "C", "E", "B"
), Price = c(6.502, 4.526, 2.272, 9.097, 0.594, 5.41, 9.85, 1.197, 
5.09, 7.343, 3.339, 1.107, 7.993, 7.922, 4.558, 4.75, 1.278, 
9.55, 8.223, 6.195, 0.668, 9.741, 9.679, 3.488, 4.159, 3.756, 
6.233, 7.658, 8.896, 9.724, 6.582, 7.422, 1.172, 2.734, 4.917, 
0.784, 9.284, 0.7, 6.869, 3.054, 9.945, 4.173, 5.217, 6.016, 
5.559, 5.247, 7.024, 8.845, 8.436, 7.482, 8.609, 3.675, 2.76, 
5.357, 4.125, 3.199, 7.736, 5.255, 5.581, 5.282, 2.753, 1.568, 
2.083, 8.938, 7.562, 7.513, 9.493, 8.404, 5.266, 9.992, 3.813, 
5.522, 6.295, 5.385, 1.91, 3.597, 4.105, 9.484, 6.697, 4.818, 
1.644, 2.699, 0.608, 9.6, 0.447, 4.123, 2.997, 5.085, 0.903, 
5.455, 1.869, 4.053, 8.843, 1.171, 8.491, 9.236, 5.642, 6.565, 
3.168, 4.367, 6.008, 6.267, 8.363, 0.318, 5.226, 6.597, 2.932, 
3.149, 8.578, 1.814, 9.288, 9.96, 8.44, 3.514, 2.832, 5.881, 
4.57, 7.646, 2.19, 5.446, 5.318, 3.674, 6.235, 9.414, 7.201, 
9.846, 0.824, 0.757, 4.928, 0.499, 9.165, 1.564, 6.944, 3.474, 
8.537, 5.412, 7.142), Order_Date = structure(c(19208, 19210, 
19337, 19288, 19339, 19318, 19352, 19290, 19245, 19317, 19352, 
19177, 19306, 19300, 19196, 19314, 19270, 19212, 19232, 19199, 
19238, 19274, 19265, 19311, 19257, 19344, 19294, 19331, 19181, 
19348, 19317, 19332, 19334, 19338, 19184, 19274, 19240, 19350, 
19195, 19306, 19198, 19283, 19260, 19263, 19177, 19197, 19186, 
19205, 19264, 19328, 19268, 19190, 19210, 19300, 19273, 19188, 
19280, 19264, 19211, 19241, 19296, 19350, 19260, 19201, 19254, 
19262, 19176, 19175, 19335, 19179, 19196, 19190, 19253, 19178, 
19190, 19234, 19253, 19310, 19323, 19325, 19258, 19292, 19213, 
19269, 19225, 19299, 19350, 19224, 19193, 19250, 19242, 19260, 
19340, 19276, 19211, 19222, 19305, 19305, 19273, 19268, 19306, 
19277, 19225, 19188, 19190, 19181, 19268, 19291, 19272, 19238, 
19184, 19228, 19199, 19237, 19205, 19251, 19357, 19238, 19288, 
19301, 19331, 19260, 19320, 19175, 19253, 19312, 19354, 19357, 
19176, 19187, 19225, 19281, 19352, 19315, 19246, 19308, 19356
), class = "Date"), Mean = c(6.075, 4.887, 4.56, 4.279, 1.664, 
6.597, 7.344, 4.279, 4.816, 6.541, 7.344, 3.742, 8.466, 8.466, 
6.042, 5.294, 4.232, 6.371, 9.092, 6.432, 3.922, 5.584, 6.142, 
5.89, 4.305, 5.076, 4.786, 6.55, 4.633, 7.147, 6.597, 6.18, 5.947, 
1.664, 4.633, 4.232, 7.309, 1.522, 6.108, 4.531, 4.343, 5.214, 
6.796, 4.56, 7.19, 4.744, 4.788, 6.26, 4.211, 6.18, 6.071, 5.235, 
4.282, 4.939, 4.627, 4.916, 8.173, 6.321, 6.169, 5.186, 4.786, 
1.522, 4.829, 6.754, 6.233, 6.142, 4.633, 9.198, 4.382, 9.198, 
5.09, 4.916, 3.817, 4.833, 4.788, 4.591, 6.489, 8.466, 6.055, 
6.18, 5.516, 4.279, 4.438, 5.488, 5.083, 5.075, 1.522, 5.083, 
6.108, 4.823, 2.944, 4.275, 6.132, 5.286, 7.374, 8.864, 6.493, 
4.937, 5.584, 4.56, 4.531, 4.476, 4.591, 4.633, 4.916, 7.19, 
4.56, 5.357, 4.232, 5.286, 6.042, 9.092, 6.432, 5.68, 6.754, 
5.972, 7.147, 5.68, 1.642, 4.677, 5.318, 6.796, 5.258, 7.19, 
4.823, 5.89, 1.522, 0.757, 3.742, 4.633, 5.083, 3.631, 5.029, 
5.294, 6.002, 5.29, 5.947), St_Dev = c(2.6129054299217, 0.509823989235501, 
2.00526398927091, 3.42698040068318, 1.51320851173921, 2.38520422605696, 
3.5047868694116, 3.42698040068318, 0.765331464138256, 1.23306277212476, 
3.5047868694116, 2.73724732471632, 0.882039870602986, 0.882039870602986, 
2.56431433330627, 1.23818031804742, 3.93046920863146, 2.91824423424673, 
1.22824447892103, 2.66853610514163, 3.50290438065329, 2.8992451172676, 
3.40603180646727, 2.51252327352405, 1.1069454819457, 2.08558720747899, 
1.3528288140042, 1.12434158510659, 3.96233188917839, 3.64442835023547, 
2.38520422605696, 2.77910107768681, 3.04113182877691, 1.51320851173921, 
3.96233188917839, 3.93046920863146, 2.79948429774724, 1.05524542958814, 
2.76245889432895, 2.08879343162506, 3.49933328888042, 2.8661126635218, 
2.52344025092729, 1.17816897890631, 1.99474133661485, 0.938771271396819, 
3.29840006164605, 3.65574205873445, 3.51589963830976, 2.77910107768681, 
3.10172930260954, 3.95193820440408, 4.00434301727512, 1.51123401563093, 
1.27601138970883, 0.967076367201677, 0.617304219975856, 2.4752699448747, 
2.79583591157357, 0.135764501987817, 1.3528288140042, 1.05524542958814, 
3.60210659264297, 3.40360901593196, 1.19302738163604, 3.40603180646727, 
3.96233188917839, 1.12288556852424, 1.73772271953842, 1.12288556852424, 
3.33479419155066, 0.967076367201677, 3.44733890123962, 1.09602767604959, 
3.29840006164605, 3.38583436295004, 3.19739855194813, 0.882039870602986, 
0.908632213824717, 2.77910107768681, 3.5472435777657, 3.42698040068318, 
4.88935745608084, 3.55836927605356, 3.94587230744568, 3.551001492537, 
1.05524542958814, 3.94587230744568, 2.76245889432895, 2.74848946392984, 
2.96352847126529, 0.992817539463655, 3.13019406822674, 4.08020339770785, 
3.04360492125155, 0.526794551983979, 1.20278863479832, 1.41184627350147, 
2.8992451172676, 1.17816897890631, 2.08879343162506, 1.40422419862357, 
3.38583436295004, 3.96233188917839, 0.967076367201677, 1.99474133661485, 
1.17816897890631, 1.54602164172002, 3.93046920863146, 2.64911400283189, 
3.02012697856674, 1.22824447892103, 2.66853610514163, 2.07329600716669, 
3.40360901593196, 1.72442085930321, 3.64442835023547, 2.07329600716669, 
0.513920551577123, 1.40918653839724, 0, 2.52344025092729, 1.23526940381441, 
1.99474133661485, 2.74848946392984, 2.51252327352405, 1.05524542958814, 
0, 2.73724732471632, 3.96233188917839, 3.94587230744568, 4.0012302587246, 
2.40510367066924, 1.23818031804742, 3.47154714548619, 1.52681361010439, 
1.90033084838755)), row.names = c(NA, -137L), class = c("tbl_df", 
"tbl", "data.frame"))
Nick
  • 15
  • 4

0 Answers0