开发者

Create Lists from Character String

Perhaps my brain is not working today but i cant figure out how to create a list from 2 character strings.

I've currently got

scale_lab
[1] "Very Poor"  "Poor"       "Average"    "Good"       "Very Good" 
[6] "Don't Know"

and

sc开发者_JAVA百科ale_rep
[1] "1" "2" "3" "4" "5" "9"

So what I want to do is combine the two into a list so that 1 = very poor, 2 = poor and so on.


Just use names() to assign it:

> scale_lab <- c("Very Poor", "Poor", "Average", "Good", 
+                "Very Good", "Don't Know")
> scale_rep <- c("1","2","3","4","5","9")
> names(scale_lab) <- scale_rep
> scale_lab
           1            2            3            4            5            9
 "Very Poor"       "Poor"    "Average"       "Good"  "Very Good" "Don't Know"
> scale_lab["9"]
           9
"Don't Know"
>


Alternatively, you could save it as factor (R's equivalent of a categorical variable)

scale_rep <- factor(scale_rep, label=scale_lab)

If you need to use the numbers for some ordinal data stats you could always go back to the numbers:

as.numeric(scale_rep) 

Although, I would recode DK as NA

scale_rep[scale_rep == 9] <- NA
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜