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]]
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 alist of> countries. The scores are rated on a scale of (D, C, B, A) and there arealso> 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 wholedataframe> at once or do I need to transform each column separately? How do Icorrectly> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
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. >
On 12/04/2010 01:25 AM, 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? >Hi Katharina, Just to muddy the waters a bit further, say your original variable is named "score" in a dataframe named "scoredata", and is a factor with the usual default alphabetic ordering. Try this: transform<-c(100,27,50,25,0) scoredata$newscore<-NA scoredata$newscore[!is.na(scoredata$score)]<- transform[as.numeric(scoredata$score[!is.na(scoredata$score))] Jim