r - Replacing rank value for each string in a character vector -


i have character vector in r, , want assign specific rank each vector element , use rank in computations, how can this?

for example, degree vector defined follows:

degree = c("low","med","high") 

and want assign rank 1 3 each degree , replacing degrees of defined vector ranks:

blood_pressure = c("low","low","high","med","high") blood_pressure = c(1,1,3,2,3) 

simply use as.numeric , factor, this:

degree = c("low","med","high") blood_pressure = c("low","low","high","med","high") as.numeric(factor(blood_pressure, degree)) # [1] 1 1 3 2 3 

another option, results in named vector, create named version of "degree" , basic matching. example:

setnames(seq_along(degree), degree)[blood_pressure] #  low  low high  med high  #    1    1    3    2    3  

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -