-1

I have this dataframe:

restaurant = c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5)
product = c("small", "medium", "large",
            "small", "medium", "large",
            "small", "medium", "large",
            "small", "medium", "large",
            "small", "medium", "large")
unitssold = c(30,25,59,20,10,50,10,15,20,5,6,12,25,67,100)
id = c(1,5,4,3,2,1,5,6,7,4,3,9,1,5,3)
df <- data.frame(restaurant,product,unitssold,id)

And I would like to define a new column names SaleKG. To calculate this number: - if a small portion than = unitssold * 5 - if a medium portion than = unitssold * 8 - if a large portion than = unitssold *10

How can I achieve this?

Afke
  • 899
  • 2
  • 10
  • 21

2 Answers2

1

With tidyverse:

df%>%
   mutate(SaleKG=case_when(
     product=="small"~unitssold*5,
     product=="medium"~unitssold*8,
     product=="large"~unitssold*10,
     T~unitssold))
   restaurant product unitssold id SaleKG
1           1   small        30  1    150
2           1  medium        25  5    200
3           1   large        59  4    590
4           2   small        20  3    100
5           2  medium        10  2     80
6           2   large        50  1    500
7           3   small        10  5     50
8           3  medium        15  6    120
9           3   large        20  7    200
10          4   small         5  4     25
11          4  medium         6  3     48
12          4   large        12  9    120
13          5   small        25  1    125
14          5  medium        67  5    536
15          5   large       100  3   1000
jyjek
  • 2,627
  • 11
  • 23
1

Here is a solution with base R:

df$SaleKG <- df$unitssold * sapply(as.character(df$product), switch, small=5, medium=8, large=10)

and a solution with recode from car:

library("car")
df$SaleKG <- df$unitssold * recode(as.character(df$product), "'small'=5; 'medium'=8; 'large'=10")
jogo
  • 12,469
  • 11
  • 37
  • 42