[R] Odp: recode letters to numbers
Petr PIKAL
petr.pikal at precheza.cz
Fri Dec 3 16:46:24 CET 2010
Hi
If you had your variables as factors you can change levels of each factor.
levels(some.factor) <- c(0,25,50,27,100)
fac<-factor(sample(LETTERS[1:5],20, replace=TRUE) )
fac
[1] A A D A D A D A C B A A D D D B D E C B
Levels: A B C D E
levels(fac)<-1:5
fac
[1] 1 1 4 1 4 1 4 1 3 2 1 1 4 4 4 2 4 5 3 2
Levels: 1 2 3 4 5
as.numeric(fac)
[1] 1 1 4 1 4 1 4 1 3 2 1 1 4 4 4 2 4 5 3 2
If you want to do it for whole data frame, you can use cycle or
lapply/sapply
Regards
Petr
r-help-bounces at r-project.org napsal dne 03.12.2010 15:25:17:
> Hello,
>
> I have a dataframe assigning various scores on around 20 variables to a
list of
> countries. The scores are rated on a scale of (D, C, B, A) and there are
also
> some not rated ones (NR) and others are left blank (NA). I now wanted to
> transfer the scores into numeric values (such as NR=0, D=25, C=50, B=27,
A=100),
> while also leaving the NA in the dataframe. Can I transform the whole
dataframe
> at once or do I need to transform each column separately? How do I
correctly
> specify the levels and labels of the new dataframe?
>
> Thank you so much,
>
> Katharina
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list