Zlo
Zlo

Reputation: 1170

Creating a binary factor form ordinal data in R

I have an ordinal variable that ranges from 1 through 67

summary(var)
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
1.0     4.0     8.0    10.2    15.0    67.0

I want to recode it into a binary variable with first level being a score of 1 on the original variable and the second level – all other scores combined.

I assume I have to use factor() function, but can't figure out how to aggregate all of the values (excluding 1) for the second level.

Thanks.

Upvotes: 1

Views: 382

Answers (1)

DGKarlsson
DGKarlsson

Reputation: 1101

Yes, the factor() function works. You could just use:

factor(var == 1, labels=c("other", "one"))

The condition will split the data for you, and then labels will assign relevant names (otherwise the names will be "FALSE" and "TRUE").

Upvotes: 4

Related Questions