Turning a logical vector into its indices without losing its length

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

Turning a logical vector into its indices without losing its length

Leeds, Mark (IED)
I have the code below which gives me what I want for temp based on
logvec but I was wondering if there was a shorter way ( i.e :
a one liner ) without having to initialize temp to zeros.  This is
purely for learning purposes. Thanks.

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

temp<-numeric(length(invec))
temp[invec]<-which(invec)
temp

[1] 1 0 0 4 0 0 7 0

obviously, the code below doesn't work.

temp <- which(invec)
> temp
[1] 1 4 7
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/se...{{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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Turning a logical vector into its indices without losing itslength

Leeds, Mark (IED)
There's a typo in my previous message. logvec should be invec. sorry.


-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Leeds, Mark (IED)
Sent: Friday, August 24, 2007 5:12 PM
To: [hidden email]
Subject: [R] Turning a logical vector into its indices without losing
itslength

I have the code below which gives me what I want for temp based on
logvec but I was wondering if there was a shorter way ( i.e :
a one liner ) without having to initialize temp to zeros.  This is
purely for learning purposes. Thanks.

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

temp<-numeric(length(invec))
temp[invec]<-which(invec)
temp

[1] 1 0 0 4 0 0 7 0

obviously, the code below doesn't work.

temp <- which(invec)
> temp
[1] 1 4 7
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to
buy/se...{{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
and provide commented, minimal, self-contained, reproducible code.
--------------------------------------------------------

This is not an offer (or solicitation of an offer) to buy/se...{{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
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Turning a logical vector into its indices without losing its length

Gabor Grothendieck
In reply to this post by Leeds, Mark (IED)
Here are two solutions:

> logvec <- c(TRUE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE)

> ifelse(logvec, seq_along(logvec), 0)
[1] 1 0 0 4 0 0 7 0

> replace(logvec * 0, logvec, which(logvec))
[1] 1 0 0 4 0 0 7 0


On 8/24/07, Leeds, Mark (IED) <[hidden email]> wrote:

> I have the code below which gives me what I want for temp based on
> logvec but I was wondering if there was a shorter way ( i.e :
> a one liner ) without having to initialize temp to zeros.  This is
> purely for learning purposes. Thanks.
>
> logvec <- c(TRUE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE)
>
> temp<-numeric(length(invec))
> temp[invec]<-which(invec)
> temp
>
> [1] 1 0 0 4 0 0 7 0
>
> obviously, the code below doesn't work.
>
> temp <- which(invec)
> > temp
> [1] 1 4 7
> --------------------------------------------------------
>
> This is not an offer (or solicitation of an offer) to buy/se...{{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
> 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.
Reply | Threaded
Open this post in threaded view
|

Re: Turning a logical vector into its indices without losing its length

Achim Zeileis
In reply to this post by Leeds, Mark (IED)
On Fri, 24 Aug 2007, Leeds, Mark (IED) wrote:

> I have the code below which gives me what I want for temp based on
> logvec but I was wondering if there was a shorter way ( i.e :
> a one liner ) without having to initialize temp to zeros.  This is
> purely for learning purposes. Thanks.
>
> logvec <- c(TRUE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE)

R> logvec[logvec] <- which(logvec)
R> logvec
[1] 1 0 0 4 0 0 7 0

hth,
Z

> temp<-numeric(length(invec))
> temp[invec]<-which(invec)
> temp
>
> [1] 1 0 0 4 0 0 7 0
>
> obviously, the code below doesn't work.
>
> temp <- which(invec)
> > temp
> [1] 1 4 7
> --------------------------------------------------------
>
> This is not an offer (or solicitation of an offer) to buy/se...{{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
> 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.
Reply | Threaded
Open this post in threaded view
|

Re: Turning a logical vector into its indices without losing its length

Gabor Grothendieck
In reply to this post by Gabor Grothendieck
On 8/24/07, Gabor Grothendieck <[hidden email]> wrote:
> Here are two solutions:
>
> > logvec <- c(TRUE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE)
>
> > ifelse(logvec, seq_along(logvec), 0)
> [1] 1 0 0 4 0 0 7 0
>
> > replace(logvec * 0, logvec, which(logvec))
> [1] 1 0 0 4 0 0 7 0

Actually the * 0 is not needed.  The last one could simply be:

replace(logvec, logvec, which(logvec))


>
>
> On 8/24/07, Leeds, Mark (IED) <[hidden email]> wrote:
> > I have the code below which gives me what I want for temp based on
> > logvec but I was wondering if there was a shorter way ( i.e :
> > a one liner ) without having to initialize temp to zeros.  This is
> > purely for learning purposes. Thanks.
> >
> > logvec <- c(TRUE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE)
> >
> > temp<-numeric(length(invec))
> > temp[invec]<-which(invec)
> > temp
> >
> > [1] 1 0 0 4 0 0 7 0
> >
> > obviously, the code below doesn't work.
> >
> > temp <- which(invec)
> > > temp
> > [1] 1 4 7
> > --------------------------------------------------------
> >
> > This is not an offer (or solicitation of an offer) to buy/se...{{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
> > 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.
Reply | Threaded
Open this post in threaded view
|

Re: Turning a logical vector into its indices without losing its length

François Pinard
In reply to this post by Leeds, Mark (IED)
[Leeds, Mark (IED)]

>I have the code below which gives me what I want for temp based on
>invec but I was wondering if there was a shorter way ( i.e :
>a one liner ) without having to initialize temp to zeros.  This is
>ppurely for learning purposes. Thanks.

>invec <- c(TRUE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE)

>temp<-numeric(length(invec))
>temp[invec]<-which(invec)
>temp

>[1] 1 0 0 4 0 0 7 0

A mere:

   invec * seq_along(invec)

would do it.  To be honest, I dislike the multiplication trickery, and
so prefer Gabor's solution, even if a bit longer:

   ifelse(invec, seq_along(invec), 0)

--
François Pinard   http://pinard.progiciels-bpi.ca

______________________________________________
[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: Turning a logical vector into its indices without losing its length

Gabor Grothendieck
In reply to this post by Gabor Grothendieck
On 8/24/07, Gabor Grothendieck <[hidden email]> wrote:

> On 8/24/07, Gabor Grothendieck <[hidden email]> wrote:
> > Here are two solutions:
> >
> > > logvec <- c(TRUE,FALSE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE)
> >
> > > ifelse(logvec, seq_along(logvec), 0)
> > [1] 1 0 0 4 0 0 7 0
> >
> > > replace(logvec * 0, logvec, which(logvec))
> > [1] 1 0 0 4 0 0 7 0
>
> Actually the * 0 is not needed.  The last one could simply be:
>
> replace(logvec, logvec, which(logvec))

If logvec can have NAs then this solution would not work but could
be modified to be done like this:

replace(logvec, which(logvec), which(logvec))

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