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?
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
...
What about this?
# df is your data frame
veg <- c("tomato", "apple", "potato")
df$category <- ifelse(df$food %in% veg, "vegetable", df$food)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With