R create a new column based on if else condition

I have a data frame consisting of column n , and one of them is food . food Possible column values: apple , tomato , cabbage , sausage , beer , vodka , potato . I want to create a new column in the data data frame as follows: if food==apple or food==tomato or food==potato , then assign vegetables , otherwise assign only the value data$food .

So, if the data frame looks like this:

 ID ..(some other columns).. food 1 apple 2 sausage 3 tomato 4 cabbage 5 vodka 

then the result should be as follows:

 ID ..(some other columns).. food category 1 apple vegetable 2 sausage sausage 3 tomato vegetable 4 cabbage vegetable 5 vodka vodka 

How can i do this?

+5
source share
3 answers

I would copy the variable and find which rows match your criteria and replace the values โ€‹โ€‹only for those rows. I also added a new factor level for accurate bookkeeping.

 xy <- data.frame(food = sample(c("apple", "tomato", "cabbage", "sausage", "beer", "vodka", "potato"), 50, replace = TRUE)) xy$newcol <- xy$food levels(xy$newcol) <- c(levels(xy$newcol), "veggy") xy[xy$food %in% c("apple", "tomato", "potato"), "newcol"] <- "veggy" xy food newcol 1 apple veggy 2 vodka vodka 3 sausage sausage 4 cabbage cabbage 5 vodka vodka 6 potato veggy 7 cabbage cabbage 8 cabbage cabbage ... 
+4
source

You can use recode from car , which will work both with the column "symbol" and with the "factor". For factor columns, unused levels are discarded when new levels are added.

 library(car) xy$newcol <- recode(xy$food, "c('apple', 'tomato', 'potato')='veggy'") 

NOTE: xy from @Roman Lustrik post

If you use data.table , this can be done with (single column update)

 library(data.table) setkey(setDT(xy), food)[J(c('apple', 'tomato', 'potato')), food:='veggy'] 
+1
source

How about this?

 # df is your data frame veg <- c("tomato", "apple", "potato") df$category <- ifelse(df$food %in% veg, "vegetable", df$food) 
+1
source

All Articles