Home > Software engineering >  Recode categorical variable as new variable in R
Recode categorical variable as new variable in R

Time:11-10

How would I add a new categorical column to this data based on the values in the 1st column in R? Like this:

> head(df)
          common_name
1       Sailfin molly
2 Hardhead silverside
3           Blue crab

if common_name = "Sailfin molly", "Hardhead silverside", put "Fish" else, put "Crab"

> head(df)
          common_name   category
1       Sailfin molly   Fish
2 Hardhead silverside   Fish
3           Blue crab   Crab

CodePudding user response:

Found this answer here (https://rstudio-pubs-static.s3.amazonaws.com/116317_e6922e81e72e4e3f83995485ce686c14.html#/9)

df <- mutate(df, cat = ifelse(grepl("Sailfin molly", common_name), "Fish",
                                      ifelse(grepl("Hardhead silverside", common_name), "Fish", "Crab")))

CodePudding user response:

Use dput() to provide a sample of your data, don't just list the printed output because that can hide important details:

df <- structure(list(common_name = c("Sailfin molly", "Hardhead silverside", 
"Blue crab")), class = "data.frame", row.names = c(NA, -3L))

Now we need a list of the common names:

Names <- unique(df$common_name)
Names
# [1] "Sailfin molly"       "Hardhead silverside" "Blue crab"     
Fish <- unique(df$common_name)[1:2]

The first two names are the fish. Your complete data will have more names, but you will have to create a variable listing the fish. Then add your new column:

df$category <- ifelse(df$common_name %in% Fish, "Fish", "Crab")
df
          common_name category
1       Sailfin molly     Fish
2 Hardhead silverside     Fish
3           Blue crab     Crab

If you have more than two categories it will be easier to create a 2-column data frame with each common_name and category and then use merge().

df2 <- df[, 1, drop=FALSE]
table <- data.frame(common_name=Names, category=df$category)
merge(df2, table)
#           common_name category
# 1           Blue crab     Crab
# 2 Hardhead silverside     Fish
# 3       Sailfin molly     Fish
  • Related