I am working with a dataset that looks similar to this:
DataSet = data.frame(Item = c('A', 'B', 'C', "A", 'B', 'C', 'A', 'B', 'C'), Period = c(1, 1, 1, 2, 2, 2, 3, 3, 3), Produced = c(67,78,50,99,50,95,72, 75, 64), Sold = c(56,52,45,88,33,91,47, 68, 42)) %>% mutate(Surplus = (Produced - Sold))
So the resulting dataframe will be:
That is, a company is producing and selling three items (A, B, and C) over three periods (Periods 1 - 3) .
From this, I wanna create another column called "MinSurplus" showing the minimum amount of surplus in each period (I reckon that I'll have to use a mutate function?). More specifically, considering that the smallest surplus amounts in period 1, 2, and 3 are 5, 4, and 7, respectively, I wanna create something like:
But I am not sure how to approach this. Does anybody know how to do this? Just a note, the actual dataset I am using contains dozens more items and a few more periods. So the actual dataset is a lot bigger and slightly more complex than the example shown above. That being said, it would be great if your solution could be as general as possible. That is, it should not be too "rigid" that it can only be used to address this specific example. Again, I reckon that I'll have to use a mutate function, but if you know more efficient way to tackle this, please let me know!