views:

81

answers:

2

Hello all,

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

scale_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.

+2  A: 

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"
>
Dirk Eddelbuettel
Cheers, I think I need my head checked for missing that.
Cam B
+2  A: 

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
Brandon Bertelsen