Quantcast

Replace values in a vector

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

Replace values in a vector

Farida Mostajabi
Hi all,

I have a vector like this:

x<- c(0.7, 0.1, 0, 0.2, 0.2, 0, 0, 0 , 0, 0.4, 0, 0.8, 1.8)

I would like  to replace the zero values with the first previous non zero value.

my returning vector should look like this:

y<-c( 0.7, 0.1, 0.1,0.2,0.2,0.2,0.2,0.2, 0.4, 0.4, 0.8, 1.8)

How can I do this in R without using for loop?

Thank you

______________________________________________
[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
|  
Report Content as Inappropriate

Re: Replace values in a vector

Gabor Grothendieck
na.locf in the zoo package takes the last occurrence and carries it forward
into NAs so replace your zeros with NAs and then apply na.locf like this:

   library(zoo)
   na.locf(replace(x, x==0, NA))


On Thu, Dec 3, 2009 at 11:41 AM, Farida Mostajabi
<[hidden email]>wrote:

> Hi all,
>
> I have a vector like this:
>
> x<- c(0.7, 0.1, 0, 0.2, 0.2, 0, 0, 0 , 0, 0.4, 0, 0.8, 1.8)
>
> I would like  to replace the zero values with the first previous non zero
> value.
>
> my returning vector should look like this:
>
> y<-c( 0.7, 0.1, 0.1,0.2,0.2,0.2,0.2,0.2, 0.4, 0.4, 0.8, 1.8)
>
> How can I do this in R without using for loop?
>
> Thank you
>
> ______________________________________________
> [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.
>

        [[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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Replace values in a vector

Farida Mostajabi
In reply to this post by Farida Mostajabi
WOW! It worked. Thank you!

>>> Gabor Grothendieck <[hidden email]> 12/03/09 11:46 AM >>>
na.locf in the zoo package takes the last occurrence and carries it forward
into NAs so replace your zeros with NAs and then apply na.locf like this:

   library(zoo)
   na.locf(replace(x, x==0, NA))


On Thu, Dec 3, 2009 at 11:41 AM, Farida Mostajabi
<[hidden email]>wrote:

> Hi all,
>
> I have a vector like this:
>
> x<- c(0.7, 0.1, 0, 0.2, 0.2, 0, 0, 0 , 0, 0.4, 0, 0.8, 1.8)
>
> I would like  to replace the zero values with the first previous non zero
> value.
>
> my returning vector should look like this:
>
> y<-c( 0.7, 0.1, 0.1,0.2,0.2,0.2,0.2,0.2, 0.4, 0.4, 0.8, 1.8)
>
> How can I do this in R without using for loop?
>
> Thank you
>
> ______________________________________________
> [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
|  
Report Content as Inappropriate

Re: Replace values in a vector

William Dunlap
In reply to this post by Farida Mostajabi
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Farida Mostajabi
> Sent: Thursday, December 03, 2009 8:41 AM
> To: [hidden email]
> Subject: [R] Replace values in a vector
>
> Hi all,
>
> I have a vector like this:
>
> x<- c(0.7, 0.1, 0, 0.2, 0.2, 0, 0, 0 , 0, 0.4, 0, 0.8, 1.8)
>
> I would like  to replace the zero values with the first
> previous non zero value.
>
> my returning vector should look like this:
>
> y<-c( 0.7, 0.1, 0.1,0.2,0.2,0.2,0.2,0.2, 0.4, 0.4, 0.8, 1.8)

y is shorter than x.  Shouldn't there be a run of 6 0.2s,
not 5?
 
> How can I do this in R without using for loop?

One way is
   > isNotZero <- function(x) !is.na(x) & x!=0
   > f<-function(x)x[cummax(seq_along(x) * isNotZero(x))]
   > f(x)
    [1] 0.7 0.1 0.1 0.2 0.2 0.2 0.2 0.2 0.2 0.4 0.4 0.8 1.8
This one drops an initial run of 0's but could be adjusted
to do something with them.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
 
> Thank you
>
> ______________________________________________
> [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
|  
Report Content as Inappropriate

Re: Replace values in a vector

Farida Mostajabi
In reply to this post by Farida Mostajabi
Yes, should be 6  0.2.  The code worked. Thank you!

>>> William Dunlap <[hidden email]> 12/03/09 12:07 PM >>>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Farida Mostajabi
> Sent: Thursday, December 03, 2009 8:41 AM
> To: [hidden email]
> Subject: [R] Replace values in a vector
>
> Hi all,
>
> I have a vector like this:
>
> x<- c(0.7, 0.1, 0, 0.2, 0.2, 0, 0, 0 , 0, 0.4, 0, 0.8, 1.8)
>
> I would like  to replace the zero values with the first
> previous non zero value.
>
> my returning vector should look like this:
>
> y<-c( 0.7, 0.1, 0.1,0.2,0.2,0.2,0.2,0.2, 0.4, 0.4, 0.8, 1.8)

y is shorter than x.  Shouldn't there be a run of 6 0.2s,
not 5?
 
> How can I do this in R without using for loop?

One way is
   > isNotZero <- function(x) !is.na(x) & x!=0
   > f<-function(x)x[cummax(seq_along(x) * isNotZero(x))]
   > f(x)
    [1] 0.7 0.1 0.1 0.2 0.2 0.2 0.2 0.2 0.2 0.4 0.4 0.8 1.8
This one drops an initial run of 0's but could be adjusted
to do something with them.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
 
> Thank you
>
> ______________________________________________
> [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.
Loading...