Data Frame: join a groupby with a new column as a new index

Hey guys,

So I have a data frame


x=df (Products =c(1,2), Run Cal Week = (CW23,CW24) , Demand =c(20,20,20,20,20,20), Cal.Week =(CW23,CW24,CW25)

Product  Cal Week            Demand          Run Cal Week
1            CW23              20                 CW23
1            CW24              20                 CW23 
1            ,CW25             20
2
2
2

Ok so i have 11 run.cal.weeks and for each run i have a forecast of 20 calender weeks with demand for each product.
I need to calculate the Average of each Run Cal Week for each product.
This Average is the max of the average up to 10 weeks and average of the next 20.
So I had the strategy that i do a split and then add a counter and go with a groupby mean function. But it does not work how i did it:
.

data_count=c(1:26)

data_group <- data_test %>%group_by(Product, Run.Cal.Week)%>% cbind (data_test,data_count)


df1 <- df %>%
  group_by(Product, Run.Cal.Week) %>%
  summarize(mean26 = mean(Orders),
            mean13= mean(valueOrders[week<=13]))

I am new to R and I do think i am making a mistake here. Thanks a lot for the help

UPDATE: found the function
mutate(counter = row_number(ProjectID)) and it worked

1 Like

Glad you found the solution! :+1:

Can you mark your post as resolved ? Thanks!

Also on advice for next time: try to provide a reprex so that other can reproduce your code. Currently, your data.frame cannot be recreate easily. :wink:

1 Like