Hi Nikhil, The problem is that your initial "as.factor(0)" causes x to have values of "1" : "20" instead of 1 : 20. There are two possible solutions:
1) > library(car) > x <- 1:20 > y <- as.factor(recode(x, " 1:5='A'; 6:10='B'; 11:15='C'; 16:20='D' ")) > y [1] A A A A A B B B B B C C C C C D D D D D Levels: A B C D 2) > x <- as.factor(1:20) > y <- recode(x, " c('1','2','3','4','5')='A'; c('6','7','8','9','10')='B'; > c('11','12','13','14','15')='C'; c('16','17','18','19','20')='D' ") > y [1] A A A A A B B B B B C C C C C D D D D D Levels: A B C D Solution 1 requires a lot less typing... -Ista On Thu, Oct 22, 2009 at 6:43 PM, Nikhil Kaza <nk...@unc.edu> wrote: > I am having trouble with the recode function that is provided in the CAR > package. I trying to create a new factors based on existing factors. > > E.g. > >>x <- as.factor(1:20) >>y <- recode(x, " 1:5='A'; 6:10='B'; 11:15='C'; 16:20='D' ") >>y > > [1] A A A A A 6 7 8 9 A A A A A A A A A A A > Levels: 6 7 8 9 A > > Could someone point to me, my error? Thanks > > ______________________________________________ > R-help@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. > -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org ______________________________________________ R-help@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.