# [R] FW: Loop for two columns and 154 rows

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Tue Sep 15 11:54:20 CEST 2020

```Sorry, forgot to copy to r help.

Petr
> -----Original Message-----
> From: PIKAL Petr
> Sent: Tuesday, September 15, 2020 11:53 AM
> To: 'Hesham A. AL-bukhaiti' <heshamibb using yahoo.com>
> Subject: RE: [R] Loop for two columns and 154 rows
>
> Hi
>
>
> If I deciphered it correcttly you want all values which have G1 in column 1 and
> G2 in column 2 or G2 in column 1 and G1 in column to produce 1 all other
> produce 0
>
> So if your data frame is named truth
>
> truth\$column3 <- ((truth[,1] =="G1" & truth[,2] =="G2") | (truth[,2] =="G1" &
> truth[,1] =="G2")) * 1
>
> Cheers
> Petr
>
> > -----Original Message-----
> > From: R-help <r-help-bounces using r-project.org> On Behalf Of Hesham A. AL-
> > bukhaiti via R-help
> > Sent: Tuesday, September 15, 2020 11:01 AM
> > To: r-help using r-project.org
> > Subject: [R] Loop for two columns and 154 rows
> >
> >  Dears in R :i have this code in R:
> > # this for do not work true (i tried )out<-read.csv("outbr.csv") truth<-
> > out[,seq(1,2)]truth<-
> > cbind(as.character(truth[,1]),as.character(truth[,2])
> ,as.data.frame(rep(
> > 0,,dim(out)[1])));for (j in 1:2) {  for (i in 1:20) {    truth[(truth[,1]== truth[j,i] &
> > truth[,2]== truth[j,i+1]) |         (truth[,1]== truth[j+1,i] & truth[,2]==
> > truth[j+1,i+1]),3]<-1   } }
> > #truth<-out[,seq(1,2)]#truth<-
> > cbind(as.character(truth[,1]),as.character(truth[,2])  #
> ,as.data.frame(rep
> > (0,,dim(out)[1])));#truth[(truth[,1]=="G2" & truth[,2]=="G1") |
> (truth[,1]=="G1"
> > & truth[,2]=="G2"),3]<-1
> >
> #################################################################
> > #########3
> >
> > I have file have two columns  . data in this file is text just (G1,G2,G3… to
> > G154). one element  can repeat, no problem ,so  we have 23562 rows in two
> > columns (for 154 elements) like :
> > Column1   column2     column3
> > G1                 G4            0
> > G4                 G6            0
> > G100           G7             1G7              G100    .     1.                      ..                       .
> I
> > want to make third column (1 or 0) based on this condition:
> > IF  truth[,1]==”G1” & truth[,2]==”G2” | truth[,1]==”G2” & truth[,2]==”G1” <-
> > 1.then In the third column write 1 otherwise write 0.G1 and G2  just
> > exampl  (indeed  i want test If two each elements   has a reciprocal
> > relationship(G1 to G2 and G2 to G1or not) Best regHesham
> >
> >
> >
> >
> > 	[[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help