Search code examples
rif-statementcategories

If a value then return another value in a new column in R


I have a data set that has several columns. One of the columns is called "group" and has values like: 1124,1251,2615,3541. I want to insert a new column that looks at the value in a row in "group" and returns a category name. So, 1124 would return "Test" and 1251 would return "Coffee."


Solution

  • Like this?

    df$Category <- NA
    df$Category[df$Group==1124] <- "Test"
    df$Category[df$Group==1251] <- "Coffee"