Dear R-users, I'm trying to recode a variable. After using cut(data.vector,breaks=my.breaks,labels=my.label) I need the data.vector to have the same values as the labels. To make it clear: x<-runif(10) y<-cut(x,breaks=c(0,0.3,0.7,0.9,1),labels=c(3,6,7,9)) as.numeric(y) returns something like a vector 1 3 3 2 2 1 4 2 3 4 . I need something like 3 7 7 6 6 3 7 9 6 7 9 for further use. I might be using the wrong function but a loop seems to be inefficient as I need to do it about 60 times with varying length of labels Is there a quick solution? Thanks for any help in advance! Nina Wawro R.1.6.0 on Windows2000
cut returns a factor. Try something like t1 <- cut(data.vector, breaks = my.breaks, labels = my.label) as.numeric(as.character(t1)) This is an issue that comes up frequently with factor. Take a look at the help page for factor for some warnings. Hope this helps, Matt Wiener -----Original Message----- From: Nina Wawro [mailto:nina at math.uni-bremen.de] Sent: Wednesday, January 29, 2003 11:39 AM To: R-help Subject: [R] help on cut? Dear R-users, I'm trying to recode a variable. After using cut(data.vector,breaks=my.breaks,labels=my.label) I need the data.vector to have the same values as the labels. To make it clear: x<-runif(10) y<-cut(x,breaks=c(0,0.3,0.7,0.9,1),labels=c(3,6,7,9)) as.numeric(y) returns something like a vector 1 3 3 2 2 1 4 2 3 4 . I need something like 3 7 7 6 6 3 7 9 6 7 9 for further use. I might be using the wrong function but a loop seems to be inefficient as I need to do it about 60 times with varying length of labels Is there a quick solution? Thanks for any help in advance! Nina Wawro R.1.6.0 on Windows2000 ______________________________________________ R-help at stat.math.ethz.ch mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help ------------------------------------------------------------------------------
> Dear R-users, > > I'm trying to recode a variable. > After using > cut(data.vector,breaks=my.breaks,labels=my.label) > I need the data.vector to have the same values as the labels. > > > To make it clear: > x<-runif(10) > y<-cut(x,breaks=c(0,0.3,0.7,0.9,1),labels=c(3,6,7,9)) > as.numeric(y) returns something like a vector 1 3 3 2 2 1 4 2 3 4 . > I need something like 3 7 7 6 6 3 7 9 6 7 9 for further use.y is a factor and you want a numeric representing the factor labels, this is in section 7.12 of the R-FAQ: R> x<-runif(10) R> y<-cut(x,breaks=c(0,0.3,0.7,0.9,1),labels=c(3,6,7,9)) R> y [1] 3 7 9 3 6 6 7 7 3 6 Levels: 3 6 7 9 R> as.numeric(as.character(y)) [1] 3 7 9 3 6 6 7 7 3 6 Torsten> > I might be using the wrong function but a loop seems to be inefficient as I > need to do it about 60 times with varying length of labels > Is there a quick solution? > Thanks for any help in advance! > > Nina Wawro > > R.1.6.0 on Windows2000 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help > >