Dear R users, I have a question how to use 2 "ifelse" to sort my data. Such as from 11 to 20 assign to A; 6 to 10 assign to B, and the rest of them assign to C a<-1:20 tt<-ifelse(a>10, "A",no=ifelse( 5< a <=10, "B", "C")) Many Thanks Chunhao -- View this message in context: http://n4.nabble.com/Use-2-ifelse-to-sort-data-tp1565422p1565422.html Sent from the R help mailing list archive at Nabble.com.
a <- 1:20 tt <- ifelse(a > 10, "A", ifelse(a > 5, "B", "C")) The problem you have is with '5 < a <= 10'. Such double sided inequalities are used in mathematics but not in R. Here you only need the first part, but if you did need both you would need to write 5 < a & a <= 10 Look carefully and spot the difference. Bill Venables CSIRO/CMIS Cleveland Laboratories -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Chunhao Sent: Tuesday, 23 February 2010 12:13 PM To: r-help at r-project.org Subject: [R] Use 2 "ifelse" to sort data Dear R users, I have a question how to use 2 "ifelse" to sort my data. Such as from 11 to 20 assign to A; 6 to 10 assign to B, and the rest of them assign to C a<-1:20 tt<-ifelse(a>10, "A",no=ifelse( 5< a <=10, "B", "C")) Many Thanks Chunhao -- View this message in context: http://n4.nabble.com/Use-2-ifelse-to-sort-data-tp1565422p1565422.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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 Feb 22, 2010, at 8:13 PM, Chunhao wrote:> > Dear R users, > I have a question how to use 2 "ifelse" to sort my data. Such as > from 11 to 20 assign to A; 6 to 10 assign to B, and the rest of them > assign > to C > > a<-1:20 > > tt<-ifelse(a>10, "A",no=ifelse( 5< a <=10, "B", "C"))Two way comparisons are not supported in R (so use "&") and the negative consequent is not named: > tt<-ifelse(a>10, "A", ifelse(5< a & a <=10, "B", "C")) > tt [1] "C" "C" "C" "C" "C" "B" "B" "B" "B" "B" "A" "A" "A" "A" "A" "A" "A" "A" "A" "A" > -- David.> > > Many Thanks > Chunhao > > -- > View this message in context: http://n4.nabble.com/Use-2-ifelse-to-sort-data-tp1565422p1565422.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Or do it using a function that is designed for that task as in: a <- 1:20 cut(a, breaks = c(0, 5, 10, 20), labels = c("C", "B", "A")) Best, Uwe Ligges On 23.02.2010 05:12, David Winsemius wrote:> > On Feb 22, 2010, at 8:13 PM, Chunhao wrote: > >> >> Dear R users, >> I have a question how to use 2 "ifelse" to sort my data. Such as >> from 11 to 20 assign to A; 6 to 10 assign to B, and the rest of them >> assign >> to C >> >> a<-1:20 >> >> tt<-ifelse(a>10, "A",no=ifelse( 5< a <=10, "B", "C")) > > Two way comparisons are not supported in R (so use "&") and the negative > consequent is not named: > > > tt<-ifelse(a>10, "A", ifelse(5< a & a <=10, "B", "C")) > > tt > [1] "C" "C" "C" "C" "C" "B" "B" "B" "B" "B" "A" "A" "A" "A" "A" "A" "A" > "A" "A" "A" > >