我有这个数据框:
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)
我想定义一个新的列名称SaleKG。要计算此数字: -如果少于= =已售出* 5 -如果中位数大于等于=售出* 8 -如果很大一部分= =已售* 10
我该如何实现?
答案 0 :(得分:1)
使用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
答案 1 :(得分:1)
以下是基于R
的解决方案:
df$SaleKG <- df$unitssold * sapply(as.character(df$product), switch, small=5, medium=8, large=10)
以及来自recode
的{{1}}的解决方案:
car