mardi 19 janvier 2016

switch if in R is not working properly

I want to make a sample data for R education.

function with if statement

make_quote <- function(type) {
  print(type)
  if (type == "IPHONE3") { 
        qty <- round(rnorm(10,mean =5000, sd=1000),-3)
        print(paste(type, qty))
        return(qty)
  } else if ( type == "IPHONE4") { 
       qty <- round(rnorm(10,mean =7000, sd=1000),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "IPHONE5") { 
       qty <- round(rnorm(10,mean =8000, sd=1000),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "IPHONE5S") { 
       qty <- round(rnorm(10,mean =10000, sd=3000),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "IPHONE6") { 
       qty <- round(rnorm(10,mean =20000, sd=3000),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "IPHONE6PLUS" ) { 
       qty <- round(rnorm(10,mean =30000, sd=3000),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "GALAXY4") { 
       qty <- round(rnorm(10,mean =5000, sd=500),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "GALAXY4NOTE") { 
       qty <- round(rnorm(10,mean =6000, sd=500),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "GALAXY5") { 
       qty <- round(rnorm(10,mean =10000, sd=1000),-3)
       print(paste(type, qty))
       return(qty)
  } else if (type == "GALAXY5NOTE") { 
       qty <- round(rnorm(10,mean =10000, sd=1000),-3)
       print(paste(type, qty))
       return(qty)
  }
}

to make a quote for order, I got a for loop like below.

for(i in 1:5000) {
  qty <- make_quote(sales_df[i,PrdName])
  sales_df[i,10] <- qty
}

I got a result like this, that mean if statement is not working properly.

[1] "IPHONE6PLUS"
 [1] "IPHONE6PLUS 27000" "IPHONE6PLUS 31000" "IPHONE6PLUS 29000" "IPHONE6PLUS 33000"
 [5] "IPHONE6PLUS 26000" "IPHONE6PLUS 27000" "IPHONE6PLUS 33000" "IPHONE6PLUS 30000"
 [9] "IPHONE6PLUS 32000" "IPHONE6PLUS 31000"
[1] "IPHONE5"
 [1] "IPHONE5 8000" "IPHONE5 9000" "IPHONE5 9000" "IPHONE5 8000" "IPHONE5 7000"
 [6] "IPHONE5 7000" "IPHONE5 7000" "IPHONE5 9000" "IPHONE5 7000" "IPHONE5 9000"
[1] "GALAXY5"
 [1] "GALAXY5 8000"  "GALAXY5 10000" "GALAXY5 12000" "GALAXY5 11000" "GALAXY5 10000"
 [6] "GALAXY5 10000" "GALAXY5 11000" "GALAXY5 9000"  "GALAXY5 9000"  "GALAXY5 10000"
[1] "IPHONE4"
 [1] "IPHONE4 8000" "IPHONE4 7000" "IPHONE4 7000" "IPHONE4 7000" "IPHONE4 8000"
 [6] "IPHONE4 8000" "IPHONE4 5000" "IPHONE4 8000" "IPHONE4 8000" "IPHONE4 7000"
[1] "IPHONE5S"
 [1] "IPHONE5S 11000" "IPHONE5S 12000" "IPHONE5S 9000"  "IPHONE5S 9000"  "IPHONE5S 10000"
 [6] "IPHONE5S 10000" "IPHONE5S 5000"  "IPHONE5S 11000" "IPHONE5S 10000" "IPHONE5S 10000"
Warning messages:
1: In `[<-.data.table`(`*tmp*`, i, 10, value = c(27000, 31000, 29000,  :
  Supplied 10 items to be assigned to 1 items of column 'FQty' (9 unused)
2: In `[<-.data.table`(`*tmp*`, i, 10, value = c(8000, 9000, 9000,  :
  Supplied 10 items to be assigned to 1 items of column 'FQty' (9 unused)
3: In `[<-.data.table`(`*tmp*`, i, 10, value = c(8000, 10000, 12000,  :
  Supplied 10 items to be assigned to 1 items of column 'FQty' (9 unused)
4: In `[<-.data.table`(`*tmp*`, i, 10, value = c(8000, 7000, 7000,  :
  Supplied 10 items to be assigned to 1 items of column 'FQty' (9 unused)
5: In `[<-.data.table`(`*tmp*`, i, 10, value = c(11000, 12000, 9000,  :
  Supplied 10 items to be assigned to 1 items of column 'FQty' (9 unused)

Please help me

Aucun commentaire:

Enregistrer un commentaire