Another approach using in-place replacement and thinking with matrix operations instead of vector operations:

DF <- matrix( c( 0,  0, 1, 0, 0
               , NA, 0, 1, 1, 0
               , 0,  1, 0, 0, NA )
            , byrow=TRUE
            , nrow=3 )
DF2 <- DF
DF2[ , -1 ] <- ifelse(   !is.na( DF[ , -ncol( DF ) ] )
                       & 1 == DF[ , -ncol( DF ) ]
                     , 1
                     , DF[ , -1 ]
                     )

On Thu, 22 Sep 2016, Henrik Bengtsson wrote:

which(df == 1, arr.ind=TRUE) is useful here:

df <- matrix(c(0,NA,0,0,0,1,1,1,0,0,1,0,0,0,NA), nrow=3)
df
    [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    1    0    0
[2,]   NA    0    1    1    0
[3,]    0    1    0    0   NA

## Identify (row,col) indices for 1:s
idxs <- which(df == 1, arr.ind=TRUE)
idxs
    row col
[1,]   3   2
[2,]   1   3
[3,]   2   3
[4,]   2   4

## Drop any in the last column
idxs <- idxs[idxs[,"col"] < ncol(df), , drop=FALSE]
idxs
    row col
[1,]   3   2
[2,]   1   3
[3,]   2   3
[4,]   2   4

idxs[,"col"] <- idxs[,"col"] + 1L
idxs
    row col
[1,]   3   3
[2,]   1   4
[3,]   2   4
[4,]   2   5

df[idxs] <- 1
df
    [,1] [,2] [,3] [,4] [,5]
[1,]    0    0    1    1    0
[2,]   NA    0    1    1    1
[3,]    0    1    1    0   NA

/Henrik

On Thu, Sep 22, 2016 at 8:13 PM, Jim Lemon <[email protected]> wrote:
Hi Saba,
Try this:

df<-matrix(c(0,NA,0,0,0,1,1,1,0,0,1,0,0,0,NA),nrow=3)
dimdf<-dim(df)
df1<-df==1
df[cbind(rep(FALSE,dimdf[1]),df1[,-dimdf[2]])]<-1

Jim



On Fri, Sep 23, 2016 at 12:27 PM, Saba Sehrish via R-help
<[email protected]> 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

______________________________________________
[email protected] 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.

______________________________________________
[email protected] 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.

______________________________________________
[email protected] 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.


---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[email protected]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k

______________________________________________
[email protected] 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