removal of an element from a vector

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

removal of an element from a vector

gynmeerut

Dear All,
  I have some problem in R which I'm explaining using an example:
x<-(120,235,172,95,175,200,233,142)
i want to remove the elements which are lesser than 100 and as a result i want two vectors

y<-(containing elements <100)
z<-(remaining elements)


Moreover if I wish to use two different programs for vectors y and z.
which command shall I use(will IF-ELSE  work ?)



Thanks and regards,

GS

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: removal of an element from a vector

Dimitris Rizopoulos
try the following:

x <- c(120, 235, 172, 95, 175, 200, 233, 142)
y <- x[x < 100]
z <- x[x >= 100]

x
y
z

I hope it helps.

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://www.med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


----- Original Message -----
From: "gynmeerut" <[hidden email]>
To: "R-Stat Help" <[hidden email]>
Sent: Wednesday, January 04, 2006 10:22 AM
Subject: [R] removal of an element from a vector


>
> Dear All,
>  I have some problem in R which I'm explaining using an example:
> x<-(120,235,172,95,175,200,233,142)
> i want to remove the elements which are lesser than 100 and as a
> result i want two vectors
>
> y<-(containing elements <100)
> z<-(remaining elements)
>
>
> Moreover if I wish to use two different programs for vectors y and
> z.
> which command shall I use(will IF-ELSE  work ?)
>
>
>
> Thanks and regards,
>
> GS
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: removal of an element from a vector

Kort, Eric
In reply to this post by gynmeerut
Gynmeerut asks...
> Dear All,
>   I have some problem in R which I'm explaining using an example:
> x<-(120,235,172,95,175,200,233,142)
> i want to remove the elements which are lesser than 100 and as a
result i
> want two vectors
>
> y<-(containing elements <100)
> z<-(remaining elements)

x<-c(120,235,172,95,175,200,233,142)
y <- x[which(x < 100)]
z <- x[which(x >= 100)]

> Moreover if I wish to use two different programs for vectors y and z.
> which command shall I use(will IF-ELSE  work ?)

This part of the question I do not quite understand.  Now that you have
y and z, you can do whatever you like with them.  But R certainly does
have conditional control statements (if/else) as described in An
Introduction to R.

-Eric

>
>
>
> Thanks and regards,
>
> GS
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-
> guide.html
This email message, including any attachments, is for the so...{{dropped}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: removal of an element from a vector

Gabor Grothendieck
In reply to this post by gynmeerut
idx <- x < 100
x[idx]
x[!idx]

On 1/4/06, gynmeerut <[hidden email]> wrote:

>
> Dear All,
>  I have some problem in R which I'm explaining using an example:
> x<-(120,235,172,95,175,200,233,142)
> i want to remove the elements which are lesser than 100 and as a result i want two vectors
>
> y<-(containing elements <100)
> z<-(remaining elements)
>
>
> Moreover if I wish to use two different programs for vectors y and z.
> which command shall I use(will IF-ELSE  work ?)
>
>
>
> Thanks and regards,
>
> GS
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: removal of an element from a vector

Vitor Chagas
In reply to this post by gynmeerut
Hi GS,

You could simply do the following

y <- x[<100]
z <- x[x>=100]

That's one of the wonderful things about R.

Vitor


--- gynmeerut <[hidden email]> wrote:

>
> Dear All,
>   I have some problem in R which I'm explaining
> using an example:
> x<-(120,235,172,95,175,200,233,142)
> i want to remove the elements which are lesser than
> 100 and as a result i want two vectors
>
> y<-(containing elements <100)
> z<-(remaining elements)
>
>
> Moreover if I wish to use two different programs for
> vectors y and z.
> which command shall I use(will IF-ELSE  work ?)
>
>
>
> Thanks and regards,
>
> GS
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>


Vitor Chagas
Actuary
Portugal

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html