Skip to content

Loop for two columns and 154 rows

1 message · PIKAL Petr

#
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 at precheza.cz> petr.pikal at 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 at yahoo.com> 
Sent: Tuesday, September 15, 2020 1:48 PM
To: PIKAL Petr <petr.pikal at 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 at precheza.cz <mailto:petr.pikal at precheza.cz> > wrote:
Sorry, forgot to copy to r help.

Petr