Dear R Users, Suppose we want to creat a new vector ( x ) from a current vector (y) of length 1000. The current vector y includes negative, zero and positive values. We want our new vector x includes the negative values in y, otherwise NA with the same length as y. For this, we have x=y[y<0] . Now x includes a subset of y with shorter length than y. With x=match(y,x) we would have a vector of indices equal to the length of y. x=y[x] gives us a vector of values with the same length as y. But when I implement such a procedure, x dose not merely include negative values. It includes negative, NA and also positive values. What could be the reason for appearing pasitive values? And how to correct this? And more generally how can this purpose be coded more convenient? Thank you so much for any reply. Amir Safari
x<-y x[y < 0]<-y X[y >= 0]<-NA -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Amir Safari Sent: Monday, November 20, 2006 9:25 AM To: R-help at stat.math.ethz.ch Subject: [R] Creating a new vector Dear R Users, Suppose we want to creat a new vector ( x ) from a current vector (y) of length 1000. The current vector y includes negative, zero and positive values. We want our new vector x includes the negative values in y, otherwise NA with the same length as y. For this, we have x=y[y<0] . Now x includes a subset of y with shorter length than y. With x=match(y,x) we would have a vector of indices equal to the length of y. x=y[x] gives us a vector of values with the same length as y. But when I implement such a procedure, x dose not merely include negative values. It includes negative, NA and also positive values. What could be the reason for appearing pasitive values? And how to correct this? And more generally how can this purpose be coded more convenient? Thank you so much for any reply. Amir Safari ______________________________________________ R-help at stat.math.ethz.ch 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. -------------------------------------------------------- This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
Here are 3 possibilities: y <- seq(-3, 3) # test data ifelse(y < 0, y, NA) replace(y, y >= 0, NA) z <- y z[y >= 0] <- NA On 11/20/06, Amir Safari <amsa36060 at yahoo.com> wrote:> > > > Dear R Users, > > Suppose we want to creat a new vector ( x ) from a current vector (y) of length 1000. The current vector y includes negative, zero and positive values. We want our new vector x includes the negative values in y, otherwise NA with the same length as y. > > For this, we have x=y[y<0] . Now x includes a subset of y with shorter length than y. With x=match(y,x) we would have a vector of indices equal to the length of y. x=y[x] gives us a vector of values with the same length as y. But when I implement such a procedure, x dose not merely include negative values. It includes negative, NA and also positive values. > What could be the reason for appearing pasitive values? And how to correct this? And more generally how can this purpose be coded more convenient? > > Thank you so much for any reply. > > Amir Safari > > ______________________________________________ > R-help at stat.math.ethz.ch 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 11/20/2006 9:24 AM, Amir Safari wrote:> > > Dear R Users, > > Suppose we want to creat a new vector ( x ) from a current vector (y) of length 1000. The current vector y includes negative, zero and positive values. We want our new vector x includes the negative values in y, otherwise NA with the same length as y.I'd do it like this: x <- y x[y >= 0] <- NA> For this, we have x=y[y<0] . Now x includes a subset of y with shorter length than y. With x=match(y,x) we would have a vector of indices equal to the length of y. x=y[x] gives us a vector of values with the same length as y. But when I implement such a procedure, x dose not merely include negative values. It includes negative, NA and also positive values. > What could be the reason for appearing pasitive values? And how to correct this? And more generally how can this purpose be coded more convenient?I don't know exactly what went wrong with your approach, but it does seem rather roundabout. I'd suggest using more informative variable names; it's hard to figure out what you're getting when you use x for three different things! Name it according to what it contains, and then it will be obvious exactly where you do a computation that doesn't match your expectations. Duncan Murdoch> Thank you so much for any reply. > > Amir Safari > > ______________________________________________ > R-help at stat.math.ethz.ch 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.