这是(也许:))我需要为老板解决的最后一个问题,所以请再次帮助我(最后一次;)) - 我想"分类&#34 ;按订单分类的订单:
" 0"如果物品尺寸和颜色和制造商不符合个人最有序的尺寸和颜色和制造商
" 1"如果物品尺寸等于个人最有序的尺寸
" 2"如果项目颜色等于个人最有序的颜色
" 3"如果物品制造商等于个人最有序的制造商
" 4"如果物品尺寸和颜色等于个人最有序的尺寸和颜色
" 5"如果物品尺寸和制造商等于个人最有序的尺寸和制造商
" 6"如果物品颜色和制造商等于个人最有序的尺寸和制造商
" 7"如果项目颜色和颜色和制造商等于个人最有序的尺寸和颜色和制造商
数据:
DB <- data.frame(orderID = c(1,2,3,4,5,6,7,8,9,10),
orderDate = c("1.1.14","1.1.14","1.1.14","1.1.14","2.1.14",
"2.1.14","2.1.14","2.1.14","2.1.14","2.1.14"),
itemID = c(2,3,2,5,12,4,2,3,1,5),
size = c("m", "l", 42, "xxl", "m", 42, 39, "m", "m", 44),
color = c("green", "red", "blue", "yellow", "red", "yellow",
"blue", "red", "green", "black"),
manufacturer = c("11", "12", "13", "12", "13", "13", "12", "11", "11", "13")
customerID = c(1, 2, 3, 1, 1, 3, 2, 2, 1, 1),
mostorderedsize = c("m", "-", "42", "m", "m", "42", "-", "-", "m", "m"),
mostorderedcolor = c("green", "red", "-", "green", "green", "-",
"red", "red", "green", "green"), stringsAsFactors=FALSE)
mostorderedmanufacturer = c("-", "12", "13", "-", "-", "13", "12", "12", "-", "-")
预期结果:
DB$AAPP = c("4", "6", "5", "0", "1", "5", "3", "2", "4", "0")
不幸的是我不知道如何解决问题 - 希望你们能帮助我......
THX!
答案 0 :(得分:1)
你可以尝试
indx <- DB[c('size', 'color')]==DB[c('mostorderedsize', 'mostorderedcolor')]
DB$AAPP <- as.numeric(factor(1+2*indx[,1]+4*indx[,2]))-1
DB$AAPP
#[1] 3 2 1 0 1 1 0 2 3 0
答案 1 :(得分:1)
这是一种方法:
DB <- transform(DB, AAPP = (as.character(size) == mostorderedsize) +
2 * (as.character(color) == mostorderedcolor))
结果:
orderID orderDate itemID size color customerID mostorderedsize mostorderedcolor AAPP
1 1 1.1.14 2 m green 1 m green 3
2 2 1.1.14 3 l red 2 - red 2
3 3 1.1.14 2 42 blue 3 42 - 1
4 4 1.1.14 5 xxl yellow 1 m green 0
5 5 2.1.14 12 m red 1 m green 1
6 6 2.1.14 4 42 yellow 3 42 - 1
7 7 2.1.14 2 39 blue 2 - red 0
8 8 2.1.14 3 m red 2 - red 2
9 9 2.1.14 1 m green 1 m green 3
10 10 2.1.14 5 44 black 1 m green 0
更新
您可以将此命令用于更新的问题:
transform(DB, AAPP = round(1.3 * (as.character(size) == mostorderedsize) +
2.3 * (as.character(color) == mostorderedcolor) +
3.3 * (as.character(manufacturer) == mostorderedmanufacturer)))