Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R create new column based on if else condition

Tags:

dataframe

r

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

So, if data frame is 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 that?

like image 644
Bob Avatar asked May 23 '15 09:05

Bob


2 Answers

I would copy the variable and find which rows correspond to your criterion and replace the values only for those rows. I also added a new factor level for tidy 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
...
like image 59
Roman Luštrik Avatar answered Sep 20 '22 18:09

Roman Luštrik


What about this?

# df is your data frame
veg <- c("tomato", "apple", "potato")
df$category <- ifelse(df$food %in% veg, "vegetable", df$food)
like image 39
Benoit Lacherez Avatar answered Sep 21 '22 18:09

Benoit Lacherez