[R] recode letters to numbers

Phil Spector spector at stat.berkeley.edu
Fri Dec 3 17:10:20 CET 2010

Katharina -
    I think something like this may be helpful:

> z = data.frame(matrix(sample(c(LETTERS[1:4],'NR',NA),100,replace=TRUE),20,5))
> codes = c(A=100,B=27,C=50,D=25,NR=0)
> newz = sapply(z,function(x)codes[x])

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu

On Fri, 3 Dec 2010, Katharina Noussi wrote:

> 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