throwaway() function

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

throwaway() function

John Wiedenhoeft
Dear all,

I apologize if this is a FAQ (seems a bit like one, but I didn't find
anything).

I'm looking for an easy way to cut one value out of a vector and shorten
the vector accordingly. Something like:

x <- c(1, 1, 0, 6, 2)
throwaway(x[3])

which will return x = 1 1 6 2, with length(x) = 4. I know one could do
this by hand, but then one would have to create a second vector y in a
loop which has to be 1 shorter then x and then assign x <- y, as there
is no "anti-c()" function which shortens a vector (at least to my
knowledge).

Is there some kind of a throwaway() function in R?

Best regards,
John

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: throwaway() function

Kevin E. Thorpe
John Wiedenhoeft wrote:

> Dear all,
>
> I apologize if this is a FAQ (seems a bit like one, but I didn't find
> anything).
>
> I'm looking for an easy way to cut one value out of a vector and shorten
> the vector accordingly. Something like:
>
> x <- c(1, 1, 0, 6, 2)
> throwaway(x[3])
>
> which will return x = 1 1 6 2, with length(x) = 4. I know one could do
> this by hand, but then one would have to create a second vector y in a
> loop which has to be 1 shorter then x and then assign x <- y, as there
> is no "anti-c()" function which shortens a vector (at least to my
> knowledge).
>
> Is there some kind of a throwaway() function in R?
>
> Best regards,
> John

x[-3] works.


--
Kevin E. Thorpe
Biostatistician/Trialist, Knowledge Translation Program
Assistant Professor, Department of Public Health Sciences
Faculty of Medicine, University of Toronto
email: [hidden email]  Tel: 416.946.8081  Fax: 416.946.3297

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: throwaway() function

Sundar Dorai-Raj
In reply to this post by John Wiedenhoeft
John Wiedenhoeft wrote:

> Dear all,
>
> I apologize if this is a FAQ (seems a bit like one, but I didn't find
> anything).
>
> I'm looking for an easy way to cut one value out of a vector and shorten
> the vector accordingly. Something like:
>
> x <- c(1, 1, 0, 6, 2)
> throwaway(x[3])
>
> which will return x = 1 1 6 2, with length(x) = 4. I know one could do
> this by hand, but then one would have to create a second vector y in a
> loop which has to be 1 shorter then x and then assign x <- y, as there
> is no "anti-c()" function which shortens a vector (at least to my
> knowledge).
>
> Is there some kind of a throwaway() function in R?
>
> Best regards,
> John
>
> ______________________________________________
> [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
> and provide commented, minimal, self-contained, reproducible code.


Use indexing (explained in R-intro)

x <- x[-3] ## drops the third element
x <- x[-c(1, 3)] ## drops the first and third element

HTH,

--sundar

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: throwaway() function

John Wiedenhoeft
In reply to this post by John Wiedenhoeft
Easier than I thought. Thanks a lot!

Am Donnerstag, den 20.07.2006, 17:25 +0100 schrieben zig Leute ;-):
> x[-3]

______________________________________________
[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
and provide commented, minimal, self-contained, reproducible code.