Find the first values in vector

9 messages Options
Embed this post
Permalink
Grześ

Find the first values in vector

Reply Threaded More More options
Print post
Permalink
Hi !
I have a vector:
vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE
and I'm looking for a method which let me get only the first values equal TRUE from this vector. It means that I want to get a vector:
vec_out =  TRUE  TRUE  TRUE  TRUE

or posictions values = TRUE: vec_out = 1,2,3,4
David Winsemius

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink

On Nov 9, 2009, at 1:44 PM, Grzes wrote:

>
> Hi !
> I have a vector:
> vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE
> and I'm looking for a method which let me get only the first values  
> equal
> TRUE from this vector. It means that I want to get a vector:
> vec_out =  TRUE  TRUE  TRUE  TRUE
>
> or posictions values = TRUE: vec_out = 1,2,3,4

?rle

 > vec[ 1:rle(vec)$lengths[1] ]
[1] "TRUE" "TRUE" "TRUE" "TRUE"


Although that approach would faid if the first element were "FALSE".

> --
> View this message in context: http://old.nabble.com/Find-the-first-values-in-vector-tp26271555p26271555.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
[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.
baptiste auguie-5

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink
In reply to this post by Grześ
Hi,

One way would be,

vec[ cumsum(!vec)==0 ]

HTH,

baptiste

2009/11/9 Grzes <[hidden email]>:

>
> Hi !
> I have a vector:
> vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE
> and I'm looking for a method which let me get only the first values equal
> TRUE from this vector. It means that I want to get a vector:
> vec_out =  TRUE  TRUE  TRUE  TRUE
>
> or posictions values = TRUE: vec_out = 1,2,3,4
> --
> View this message in context: http://old.nabble.com/Find-the-first-values-in-vector-tp26271555p26271555.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
>

______________________________________________
[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.
Justin Montemarano

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink
In reply to this post by Grześ
Use which()

vec_out <- which(vec == T)

-
Justin Montemarano
Graduate Student
Kent State University - Biological Sciences

http://www.montegraphia.com

        [[alternative HTML version deleted]]

______________________________________________
[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.
Nikhil Kaza-2

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink
In reply to this post by David Winsemius
How about

vec[1:min(which(vec==FALSE))-1]

This will return a character(0) vector if vec[1] is FALSE

Nikhil

On 9 Nov 2009, at 2:38PM, David Winsemius wrote:

>> vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE

______________________________________________
[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.
Henrique Dallazuanna

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink
In reply to this post by Grześ
Try this:

head(vec, sum(cumprod(vec)))

The positions:

which(head(vec, sum(cumprod(vec))))

On Mon, Nov 9, 2009 at 4:44 PM, Grzes <[hidden email]> wrote:

>
> Hi !
> I have a vector:
> vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE
> and I'm looking for a method which let me get only the first values equal
> TRUE from this vector. It means that I want to get a vector:
> vec_out =  TRUE  TRUE  TRUE  TRUE
>
> or posictions values = TRUE: vec_out = 1,2,3,4
> --
> View this message in context: http://old.nabble.com/Find-the-first-values-in-vector-tp26271555p26271555.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [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.
>



--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
[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.
Dimitris Rizopoulos-4

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink
In reply to this post by Grześ
yet another solution is:

vec <- c(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
FALSE)

seq_len(rle(vec)$lengths[1])


I hope it helps.

Best,
Dimitris


Grzes wrote:
> Hi !
> I have a vector:
> vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE
> and I'm looking for a method which let me get only the first values equal
> TRUE from this vector. It means that I want to get a vector:
> vec_out =  TRUE  TRUE  TRUE  TRUE
>
> or posictions values = TRUE: vec_out = 1,2,3,4

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
[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.
Grześ

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink
In reply to this post by baptiste auguie-5

Thank You All Very Much   :jumping:



baptiste auguie-5 wrote:

>
> Hi,
>
> One way would be,
>
> vec[ cumsum(!vec)==0 ]
>
> HTH,
>
> baptiste
>
> 2009/11/9 Grzes <[hidden email]>:
>>
>> Hi !
>> I have a vector:
>> vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE
>> and I'm looking for a method which let me get only the first values equal
>> TRUE from this vector. It means that I want to get a vector:
>> vec_out =  TRUE  TRUE  TRUE  TRUE
>>
>> or posictions values = TRUE: vec_out = 1,2,3,4
>> --
>> View this message in context:
>> http://old.nabble.com/Find-the-first-values-in-vector-tp26271555p26271555.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> [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.
>>
>
> ______________________________________________
> [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.
>
>

--
View this message in context: http://old.nabble.com/Find-the-first-values-in-vector-tp26271555p26273390.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[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.
Grześ

Re: Find the first values in vector

Reply Threaded More More options
Print post
Permalink
In reply to this post by Dimitris Rizopoulos-4
Thank You All Very Much  
Dimitris Rizopoulos-4 wrote:
yet another solution is:

vec <- c(TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, TRUE,
FALSE)

seq_len(rle(vec)$lengths[1])


I hope it helps.

Best,
Dimitris


Grzes wrote:
> Hi !
> I have a vector:
> vec= TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE TRUE  TRUE  FALSE
> and I'm looking for a method which let me get only the first values equal
> TRUE from this vector. It means that I want to get a vector:
> vec_out =  TRUE  TRUE  TRUE  TRUE
>
> or posictions values = TRUE: vec_out = 1,2,3,4

--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014

______________________________________________
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.