Gundala Viswanath
2010-Jul-06 06:39 UTC
[R] Conditional Splitting a Vectors into Two Vectors
Suppose I have two vectors of same dimensions: x <-c(0.49534,0.80796,0.93970,0.99998) count <-c(0,33,0,4) How can I group the vectors 'x' into two vectors: 1. Vector `grzero` that contain value in x with `count` value greater than 0 and 2. Vector `eqzero` with value in x with `count` value equal to zero. Yielding > print(grzero) > [1] 0.80796 0.99998 > print(eqzero) > [1] 0.49534 0.93970 Regards, G.V.
On 2010-07-06 0:39, Gundala Viswanath wrote:> Suppose I have two vectors of same dimensions: > > x<-c(0.49534,0.80796,0.93970,0.99998) > count<-c(0,33,0,4) > > How can I group the vectors 'x' into two vectors: > > 1. Vector `grzero` that contain value in x with `count` value greater > than 0 and > 2. Vector `eqzero` with value in x with `count` value equal to zero. > > Yielding > > > print(grzero) > > [1] 0.80796 0.99998 > > print(eqzero) > > [1] 0.49534 0.93970 > > Regards, > G.V. >It might be time to work your way through 'An Introduction to R'. Anyway, here you want conditional extraction: x[count > 0] x[!(count > 0)] ##or equivalent -Peter Ehlers
Christos Argyropoulos
2010-Jul-06 11:52 UTC
[R] Conditional Splitting a Vectors into Two Vectors
One possible way is the following: x <-c(0.49534,0.80796,0.93970,0.99998) count <-c(0,33,0,4) x[count==0] [1] 0.49534 0.93970> x[count>0][1] 0.80796 0.99998 Christos> Date: Tue, 6 Jul 2010 15:39:08 +0900 > From: gundalav@gmail.com > To: r-help@stat.math.ethz.ch > Subject: [R] Conditional Splitting a Vectors into Two Vectors > > Suppose I have two vectors of same dimensions: > > x <-c(0.49534,0.80796,0.93970,0.99998) > count <-c(0,33,0,4) > > How can I group the vectors 'x' into two vectors: > > 1. Vector `grzero` that contain value in x with `count` value greater > than 0 and > 2. Vector `eqzero` with value in x with `count` value equal to zero. > > Yielding > > > print(grzero) > > [1] 0.80796 0.99998 > > print(eqzero) > > [1] 0.49534 0.93970 > > Regards, > G.V. > > ______________________________________________ > 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._________________________________________________________________ Hotmail: Trusted email with powerful SPAM protection. [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Jul-06 13:08 UTC
[R] Conditional Splitting a Vectors into Two Vectors
You can't try this also: split(x, count > 0) On Tue, Jul 6, 2010 at 3:39 AM, Gundala Viswanath <gundalav@gmail.com>wrote:> Suppose I have two vectors of same dimensions: > > x <-c(0.49534,0.80796,0.93970,0.99998) > count <-c(0,33,0,4) > > How can I group the vectors 'x' into two vectors: > > 1. Vector `grzero` that contain value in x with `count` value greater > than 0 and > 2. Vector `eqzero` with value in x with `count` value equal to zero. > > Yielding > > > print(grzero) > > [1] 0.80796 0.99998 > > print(eqzero) > > [1] 0.49534 0.93970 > > Regards, > G.V. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]