[R] Loop for two columns and 154 rows

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Wed Sep 16 13:48:37 CEST 2020


Hi

 

It is recommended to keep your emails in Rhelp, there are others who could give you answer.

 

If I understand correctly you want to take 2 lines in your data frame and test them if they are same but just reversed.

 

One option could be to split the whole data frame in chunks. Based on your example

 

test <- structure(list(V1 = c("G1", "G2", "G3", "G1", "G20", "G21"), 

    V2 = c("G2", "G1", "G1", "G3", "G21", "G16")), class = "data.frame", row.names = c(NA, -6L))

test.l <- split(test, rep(1:3, each=2))

res <- rep(NA, length(test.l))

for (i in 1:length(test.l)) res[i] <- prod(as.numeric(factor(unlist(test.l[[i]]))))

test$column3 <- rep(res==4, each=2)*1

 

Maybe some clever matrix operations could do it in more elegant way.

 

Cheers

Petr

 

S pozdravem | Best Regards

RNDr. Petr PIKAL
Vedoucí Výzkumu a vývoje | Research Manager

PRECHEZA a.s.
nábř. Dr. Edvarda Beneše 1170/24 | 750 02 Přerov | Czech Republic
Tel: +420 581 252 256 | GSM: +420 724 008 364
 <mailto:petr.pikal using precheza.cz> petr.pikal using precheza.cz |  <https://www.precheza.cz/> www.precheza.cz

Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na:  <https://www.precheza.cz/zasady-ochrany-osobnich-udaju/> https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website:  <https://www.precheza.cz/en/personal-data-protection-principles/> https://www.precheza.cz/en/personal-data-protection-principles/

Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohlášení o vyloučení odpovědnosti:  <https://www.precheza.cz/01-dovetek/> https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer:  <https://www.precheza.cz/en/01-disclaimer/> https://www.precheza.cz/en/01-disclaimer/

 

From: Hesham A. AL-bukhaiti <heshamibb using yahoo.com> 
Sent: Tuesday, September 15, 2020 1:48 PM
To: PIKAL Petr <petr.pikal using precheza.cz>
Subject: Re: [R] Loop for two columns and 154 rows

 


thanks petr v much 
i attached my problem in word and my data 
i want take all elements them have relations not just G1 and G2 this just example test (G1 AND G2) i want make loop to take all elements:

G1 G2 1 
G2 G1 1
G3 G1 1
G1 G3 1
G20 G21 0
G21 G16 0 
(notice (G1 and G2 have relation and G3 and G1 also have but G20 ,G21,G16 do not have this relations we put 0 ,

(i have G1 to G154) them make 23400 relation i want to know wich forn theses relations are true (1) or not (0)

i hope you can help me.

On Tuesday, September 15, 2020, 02:54:27 AM PDT, PIKAL Petr <petr.pikal using precheza.cz <mailto:petr.pikal using precheza.cz> > wrote: 

 

 

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 <mailto:heshamibb using yahoo.com> >
> Subject: RE: [R] Loop for two columns and 154 rows
> 
> Hi
> 
> Your mail is unreadable, post in plain text not HTML.
> 
> 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 <mailto: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 <mailto: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 <mailto:R-help using 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