简体   繁体   中英

Create a new categorical “comparison detection” column based on two other columns in R (nine option answers)

This is a bit different than other conditional column creations. I want to build a column, "vs", based on two other categorical columns, "matched_apple" and "matched_pear", which each have 3 answer choices or "levels", like so:

matched_apple: C, CG, G, C, CG, G, C, CG, G
matched_pear: G, C, CG, CG, G, C, C, CG, G

desired outcome (i do not want "vs" output to contain the entire column names of matched_apple and matched_pear, just "apple" and "pear"):

vs: apple=C/pear=G, apple=CG/pear=C, apple=G/pear=CG, apple=C/pear=CG, apple=CG/pear=G, apple=G/pear=C, apple=C/pear=C, apple=CG/pear=CG, apple=G/pear=G

basically, i want to create a column that detects answers of matched_apple and matched_pear, and spits out a simple detection of apple/pear answers in a new column.

I can figure out how to create a binary conditional variable, but cannot figure out how best to create a multiple level conditional code to get my desire output. this is my current code:

library(dplyr)
df <-df %>%
     mutate(vs = c("", "apple=G/pear=CG")[(apple == "G" & 
           pear == "CG")+1])

which essentially only creates a one level variable. i tried adding to the mutate() equation, but then i end up getting an error...i cannot think of another better way to approach this other than simply doing it manually. any help/suggestions would be very appreciated!!

according to convesation in the comments this seems to be the answer:

library(dplyr)
    df %>% dplyr::mutate(vs = paste0("apple=", matched_apple, "/", "pear=",matched_pear))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM