AbsoluteBeginner
AbsoluteBeginner

Reputation: 505

Categorize orders into numbers

this is (maybe :) ) the last question for a longer period of time I need to solve for my boss, so please help me again (one last time ;) ) - I want to "categorize" the orders into classes by numbers:
"0" if the Item size AND color AND manufacturer is unequal to the personal most ordered size and color and manufacturer
"1" if the Item size is equal to the personal most ordered size
"2" if the Item color is equal to the personal most ordered color
"3" if the item manufacturer is equal to the personal most ordered manufacturer
"4" if the Item size AND color is equal to the personal most ordered size AND color
"5" if the Item size AND manufacturer is equal to the personal most ordered size AND manufacturer
"6" if the Item color AND manufacturer is equal to the personal most ordered size AND manufacturer
"7" if the Item color AND color AND manufacturer is equal to the personal most ordered size AND color AND manufacturer

Data:

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", "-", "-")

Expected outcome:

DB$AAPP = c("4", "6", "5", "0", "1", "5", "3", "2", "4", "0")    

Unfortunately I have no idea how to solve the problem- hope you guys are able to help me...

THX!

Upvotes: 0

Views: 55

Answers (2)

Sven Hohenstein
Sven Hohenstein

Reputation: 81733

Here's an approach:

DB <- transform(DB, AAPP = (as.character(size) == mostorderedsize) +
                             2 * (as.character(color) == mostorderedcolor))

The result:

   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

Update

You can use this command for the updated question:

transform(DB, AAPP = round(1.3 * (as.character(size) == mostorderedsize) +
                        2.3 * (as.character(color) == mostorderedcolor) +
                        3.3 * (as.character(manufacturer) == mostorderedmanufacturer)))

Upvotes: 1

akrun
akrun

Reputation: 887691

You could try

 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

Upvotes: 1

Related Questions