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
...
You could use recode
from car
, which will work with both 'character' and 'factor' column. For 'factor' columns, the unused levels are dropped while new levels are added.
library(car)
xy$newcol <- recode(xy$food, "c('apple', 'tomato', 'potato')='veggy'")
NOTE: xy
from @Roman Lustrik's post
If you are using data.table
, this can be done with (updating the same column)
library(data.table)
setkey(setDT(xy), food)[J(c('apple', 'tomato', 'potato')), food:='veggy']
What about this?
# df is your data frame
veg <- c("tomato", "apple", "potato")
df$category <- ifelse(df$food %in% veg, "vegetable", df$food)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.