Dear All, this is probably an easy one but I can not get a handle on it: x <-c(1,2,3,4,5) y <-c(6,7,8,9,10) z <-15 w <-ifelse(z>14,x,y) this will give me a value of 1 for w. What I would like to get is the whole string of x, so that w would become a numeric object of 5 characters exactly the same as x. Apreciate the help, Sincerely, Andras [[alternative HTML version deleted]]
Hello, w2 <- if(z > 14) x else y w2 The difference is that ifelse is vectorized and returns an object of the same length as the condition. Since length(z > 14) == 1, it only returns x[1] (or y[1], were the condition FALSE). Hope this helps, Rui Barradas Em 31-08-2012 12:55, Andras Farkas escreveu:> Dear All, > > this is probably an easy one but I can not get a handle on it: > > x <-c(1,2,3,4,5) > y <-c(6,7,8,9,10) > z <-15 > w <-ifelse(z>14,x,y) > > this will give me a value of 1 for w. What I would like to get is the whole string of x, so that w would become a numeric object of 5 characters exactly the same as x. > > Apreciate the help, > > Sincerely, > > Andras > [[alternative HTML version deleted]] > > > > ______________________________________________ > 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.[[alternative HTML version deleted]]
Hi, Try this: z1<-c(z,z,z,z,z) ?ifelse(z1>14,x,y) #[1] 1 2 3 4 5 A.K. ----- Original Message ----- From: Andras Farkas <motyocska at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Friday, August 31, 2012 7:55 AM Subject: [R] Help on numerical object and ifelse function Dear All, ? this is probably an easy one but I can not get a handle on it: ? x <-c(1,2,3,4,5) y <-c(6,7,8,9,10) z <-15 w <-ifelse(z>14,x,y) ? this will give me a value of 1 for w. What I would like to get is the whole string of x, so that w would become a numeric object of 5 characters exactly the same as x. ? Apreciate the help, ? Sincerely, ? Andras ??? [[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.
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of arun > Sent: Friday, August 31, 2012 8:47 AM > To: Andras Farkas > Cc: R help > Subject: Re: [R] Help on numerical object and ifelse function > > Hi, > Try this: > z1<-c(z,z,z,z,z) > ifelse(z1>14,x,y) > #[1] 1 2 3 4 5 > A.K. > > > > ----- Original Message ----- > From: Andras Farkas <motyocska at yahoo.com> > To: "r-help at r-project.org" <r-help at r-project.org> > Cc: > Sent: Friday, August 31, 2012 7:55 AM > Subject: [R] Help on numerical object and ifelse function > > Dear All, > > this is probably an easy one but I can not get a handle on it: > > x <-c(1,2,3,4,5) > y <-c(6,7,8,9,10) > z <-15 > w <-ifelse(z>14,x,y) > > this will give me a value of 1 for w. What I would like to get is the > whole string of x, so that w would become a numeric object of 5 characters > exactly the same as x. > > Apreciate the help, > > Sincerely, > > AndrasAndras, if you have a single test value, and you want to return either x or y, then use if() instead. w <- if(z > 14) x else y Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA