dummy variables to single categorical variable (factor) in R -


i have set of variables coded binomial.

   pre value_1 value_2 value_3 value_4 value_5 value_6 value_7 value_8  1   1       0       0       0       0       0       1       0       0        2   1       0       0       0       0       1       0       0       0        3   1       0       0       0       0       1       0       0       0        4   1       0       0       0       0       1       0       0       0            

i merge variables (value_1, value_2...value_8) 1 single ordered factor, while conserving column (pre) is, duch data this:

  pre value 1  1  value_6 2  1  value_5 3  1  value_5 

or better:

  pre value 1  1  6 2  1  5 3  1  5 

i aware exists: recoding dummy variable ordered factor

but when try code used in post, receive following error:

pa2$factor = factor(apply(pa2, 1, function(x) which(x == 1)), labels = colnames(pa2))   error in sort.list(y) : 'x' must atomic 'sort.list' have called 'sort' on list? 

any appreciated

a quick solution like

res <- cbind(df[1], value = factor(max.col(df[-1]), ordered = true)) res #   pre value # 1   1     6 # 2   1     5 # 3   1     5 # 4   1     5  str(res) # 'data.frame':  4 obs. of  2 variables: # $ pre  : int  1 1 1 1 # $ value: ord.factor w/ 2 levels "5"<"6": 2 1 1 1 

or if want actual names of columns (as pointed @bondeddust), can use same methodology extract them

factor(names(df)[1 + max.col(df[-1])], ordered = true) # [1] value_6 value_5 value_5 value_5 # levels: value_5 < value_6 

or can use own which strategy in following way (btw, which vectorized no need in using apply margin of 1 on it)

cbind(df[1], value = factor(which(df[-1] == 1, arr.ind = true)[, 2], ordered = true)) 

or can matrix multiplication (contributed @akrun)

cbind(df[1], value = factor(as.matrix(df[-1]) %*% seq_along(df[-1]), ordered = true)) 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -