1

I have a data set called one_plot, the head of which can be seen below. I am trying to add a column to label rows based on their subCategory_id using the dplyr package.

For example subCategory_id %in% c(1:4) to be labeled as "Premium", subCategory_id %in% c(4:12) to be labeled "Base" and subCategory_id %in% c(12:20) to be labeled "Other".

Data looks like this:

product_id product_origin product_price subCategory_id    GBP
          1      Australia    0.36154597              1 371.31
          2            USA    0.14425684              1 148.15
          3            USA    0.09020571              1  92.64
          5            USA    0.35793051              1 367.59
          6            USA    0.19523482              1 200.51

I tried using the command

one_plot$cat_type <- filter(one_plot, subCategory_id %in% c(1:4) = "Premium")
M--
  • 25,431
  • 8
  • 61
  • 93
rkras
  • 121
  • 4
  • 16
  • check this out - http://stackoverflow.com/questions/7658316/create-new-column-based-on-4-values-in-another-column – user2510479 Mar 27 '17 at 19:07
  • This is a good place to use `cut`. Something like `df$newVar <- cut(df$subCategory_id, breaks=c(-Inf, 4, 12), labels=c("Prem", "base")`. – lmo Mar 27 '17 at 19:50
  • I haven't tested the code since you haven't provided us with reproducible code; use `dput` for providing sample data set. Also read this http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610 – M-- Mar 28 '17 at 15:20

1 Answers1

0

You can do something like

df <- data.frame(id=1:12, otherStuff = letters[1:12])

df %>% 
    mutate(Label = lapply(id, function(x) {
        if (x %in% c(1:4)) {
            'Premium'
        } else if (x %in% c(5:10)) {
            'Base'
        } else if (x %in% c(11:12)) {
            'other'
        }
    }))
Kipras Kančys
  • 1,617
  • 1
  • 15
  • 20