2

Using this data.frame:

df <- read.table(text = c("
ID  cat1    cat2    cat3
site1   High    High    High
site1   High    High    Medium
site1   High    High    Low
site1   High    Medium  High
site1   High    Medium  Medium
site1   High    Medium  Low
site1   High    Low High
site1   High    Low Medium
site1   High    Low Low
site1   Medium  High    High
site1   Medium  High    Medium
site1   Medium  High    Low
site1   Medium  Medium  High
site1   Medium  Medium  Medium
site1   Medium  Medium  Low
site1   Medium  Low High
site1   Medium  Low Medium
site1   Medium  Low Low
site1   Low High    High
site1   Low High    Medium
site1   Low High    Low
site1   Low Medium  High
site1   Low Medium  Medium
site1   Low Medium  Low
site1   Low Low High
site1   Low Low Medium
site1   Low Low Low
"), header =T)

I want to create a new column called new_category based on cat1, cat2 and cat3.

I want each row in new_category to be have the common class "or word" in cat1, cat2 and cat3. If all values are different (High, Medium and Low), new_category will take the highest class (High in this case).

For example

If cat1 = High, cat2 = High, cat3= Medium, then new_category = High

If cat1 = High, cat2 = Medium, cat3= Low, then new_category = High

If cat1 = Medium, cat2 = Medium, cat3= Low, then new_category = Medium

I can do this using ifelse. However, there are many combinations of cat1 and cat2 and cat3.

Any suggestion for a faster or easier way to do that?

1 Answer 1

5

for rows with recurrent levels, choose most frequent one if there are such level; else choose the one with lowest rank;

# convert each row to ordered vector and find the entry with min rank;
df$new_category <- apply(
    df[2:4], 1, function(x){
        f <- table(x)
        if(max(f) > 1){
            names(f)[which.max(f)]
        }else{
            y <- factor(x, levels = c('High', 'Medium', 'Low'), ordered = T)
            as.character(min(y))
        }
    }
)
# > df
#       ID   cat1   cat2   cat3 new_category
# 1  site1   High   High   High         High
# 2  site1   High   High Medium         High
# 3  site1   High   High    Low         High
# 4  site1   High Medium   High         High
# 5  site1   High Medium Medium       Medium
# 6  site1   High Medium    Low         High
# 7  site1   High    Low   High         High
# 8  site1   High    Low Medium         High
# 9  site1   High    Low    Low          Low
# 10 site1 Medium   High   High         High
# 11 site1 Medium   High Medium       Medium
# 12 site1 Medium   High    Low         High
# 13 site1 Medium Medium   High       Medium
# 14 site1 Medium Medium Medium       Medium
# 15 site1 Medium Medium    Low       Medium
# 16 site1 Medium    Low   High         High
# 17 site1 Medium    Low Medium       Medium
# 18 site1 Medium    Low    Low          Low
# 19 site1    Low   High   High         High
# 20 site1    Low   High Medium         High
# 21 site1    Low   High    Low          Low
# 22 site1    Low Medium   High         High
# 23 site1    Low Medium Medium       Medium
# 24 site1    Low Medium    Low          Low
# 25 site1    Low    Low   High          Low
# 26 site1    Low    Low Medium          Low
# 27 site1    Low    Low    Low          Low
3
  • 1
    you are welcome. I also learned that min and max cannot be applied to ordinary factors but can be applied to ordered factors. :)
    – mt1022
    Nov 26, 2016 at 9:21
  • 4
    I'm adding it here, since its exactly same idea - different functions. To avoid tabulations and which.max by row, an alternative is to tabulate at front and use max.col. And use ifelse since we're now working on length > 1 vectors. I.e. tab = table(row(df[-1]), as.matrix(df[-1])); ifelse(rowSums(tab != 0) == 3, "High", colnames(tab)[max.col(tab, "first")])
    – alexis_laz
    Nov 26, 2016 at 9:30
  • @alexis_laz. nice row. I never used this before.
    – mt1022
    Nov 26, 2016 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.