how to get the first priority by applying rules into a data frame in r -


i have data frame including different levels of choices:

    df = read.table(text="index v1  v2  v3  v4  v5     1         b       2   b   b   b   b   b     3   b   c   c   b   b     4   b   b   c   d   e     5   b   b   c   c   d     6     b   b   b   b     7   c   c   b   d   d     8     b   c   d   e", header=t, stringsasfactors=f) 

i create column hold accepted choice each row. if there more 1 choices, take maximum numbers of occurrences. if maximum number more 1, take first choice maximum number of occurrences. result expected:

    index   v1  v2  v3  v4  v5  final     1         b         2   b   b   b   b   b   b     3   b   c   c   b   b   b     4   b   b   c   d   e   b     5   b   b   c   c   d   b     6     b   b   b   b   b     7   c   c   b   d   d   c     8     b   c   d   e   

thanks helps.

we can finding frequency of values in each row using table. loop through rows of dataset except first column (apply margin = 1), frequency table, find index of maximum frequency (which.max) , names corresponds max frequency

df$final <- apply(df[-1], 1, fun = function(x) {           tbl <- table(factor(x, levels = unique(x)))            names(tbl)[which.max(tbl)]}) df$final #[1] "a" "b" "b" "b" "b" "b" "c" "a" 

Comments