[R] Replacing value with "1"

Jim Lemon drjimlemon at gmail.com
Fri Sep 23 05:13:35 CEST 2016


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
<r-help at 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 at 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.



More information about the R-help mailing list