Hi Friends,
Below is the r code that works , Please suggest inputs for some user inputs
- want to make the column selection to be user defined like from m1:m3 or from m1:m12. when we call the function. please suggest ways to do it. for the function
2.The standard deviation function in R ,sd gives results equivalent to stdev.s in excel, is there a func equivalent to stdev.p in excel or is it suggested to use only the sd function equivalent to stdev.s
x <- rep(c('a', 'b'), 5)
m1 <- rnorm(10, 100, 20)
m2 <- rnorm(10, 120, 30)
m3 <- rnorm(10, 140, 40)
df <- data.frame(x = x,
m1 = m1,
m2 = m2,
m3 = m3)
d1 <- df %>%
group_nest(x, keep = TRUE)
ABC <- function(dt)
{
dt %>%
mutate(Sales_3M = rowSums(across(m1:m3))) %>%
arrange(desc(Sales_3M)) %>%
mutate(
Cum_3M = cumsum(Sales_3M),
Cum_Pct_3M = Cum_3M / sum(Sales_3M),
ABC_3M = if_else(
Cum_Pct_3M > 0.95,
"C",
if_else(Cum_Pct_3M > 0.80 & Cum_Pct_3M < 0.95, "B", "A")
)
)
}
t1 <- map(d1$data, ABC) %>% bind_rows()
XYZ <- function(dt) {
dt %>%
rowwise() %>%
mutate(
SD_3M = sd(across(m1:m3)),
Mean_3M = rowMeans(across(m1:m3)),
COV_3M = SD_3M / Mean_3M,
XYZ_3M = if_else(COV_3M > 1, "Z", if_else(COV_3M > 0.5 &
COV_3M < 1, "Y", "X"))
)
}
t2 <- XYZ(t1)