tmpf <- function(x) {
  n <- length(x)
  indices <- which(match(x,1) == 1)
  x[indices+1] <- 1
  x[1:n]  ## needed for the case when the last item in a row has value 1
}

tmp <- matrix(c(0,0,1,0,0,
                NA,0,1,1,0,
                0,1,0,0,NA,
                1,0,1,0,1), ## last item in row has value 1
              byrow=TRUE, 4, 5)

tmp

t(apply(tmp, 1, tmpf))

On Thu, Sep 22, 2016 at 10:27 PM, Saba Sehrish via R-help
<r-help@r-project.org> wrote:
> Hi
>
> I have a matrix that contains 1565 rows and 132 columns. All the observations 
> are either "0" or "1". Now I want to keep all the observations same but just 
> one change, i.e. whenever there is "1", the very next value in the same row 
> should become "1". Please see below as a sample:
>
>>df
>
>      0    0    1    0    0
>     NA    0    1    1    0
>      0    1    0    0    NA
>
> What I want is:
>
>
>     0    0    1    1    0
>    NA    0    1    1    1
>     0    1    1    0    NA
>
>
>
> I shall be thankful for the reply.
>
>
> Saba
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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 via email to