Help with loop logic
2
0
Entering edit mode
@elliot-harrison-2391
Last seen 10.2 years ago
Hi BioC, I'm working on a code to create a contrast matrix from the values listed against array file in the targets file. I can parse unique values from the input file ok but then I want to iterate over them to make the comparisons and matrix Group1-group2,group1-group3,group2-group3 I have included my code that I think should get me close (perhaps need to strip trailing commas). However using this code the output string is empty. Can anyone spot what I'm missing? Thanks Elliott > sessionInfo() R version 2.9.0 (2009-04-17) i386-pc-mingw32 locale: LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] limma_2.18.0 loaded via a namespace (and not attached): [1] tools_2.9.0 A=as.matrix(c("Upper", "Lower", "Middle")) string = "" nItems <- dim(A)[1] for ( i in 1:nItems ){ for (j in 1:nItems){ tolower(as.character(A[[i]])) tolower(as.character(A[[j]])) if (tolower(as.character(A[[i]])) != tolower(as.character(A[[j]]))) { if(length(i <- grep(paste(as.character(A[[i]]),as.character(A[[j]]), sep = "-") ,string)) == 0) { } else { paste(as.character(A[[i]]),as.character(A[[j]]), sep = "-") string } if(length(i <- grep(paste(as.character(A[[j]]),as.character(A[[i]]), sep = "-") ,string)) == 0) { paste(string ,paste(as.character(A[[j]]),as.character(A[[i]]), sep = "-"), sep = ",") } else { paste(as.character(A[[j]]),as.character(A[[i]]), sep = "-") string } } } } string Mr Elliott Harrison Senior Research Associate Epistem Ltd 48 Grafton Street Manchester, M13 9XX. UK Office Tel: +44 (0) 161 606 7398 Fax: +44 (0) 161 606 7348 Incorporated and registered in England and Wales under registration number: 03901952. Registered office: The Incubator Building, Grafton Street, Manchester M13 9XX. VAT registration number: 903 0350 77. This email and any attachments and files are confidential and are intended only for the addressee(s). Any review, onward transmission, disclosure or other use of this email or any attachments by any other person is prohibited. If you receive this email and are not an intended recipient please inform the sender immediately and delete this message, removing any copies from your system. Unless expressly stated otherwise, any opinions expressed in this email or any attachments transmitted with it are personal and do not constitute the opinions of Epistem Holdings Plc or any subsidiary company (together "Epistem"). Although emails are routinely screened for viruses, Epistem does not accept any liability or responsibility for: (i) changes made to this email or any attachments transmitted with it after they were sent; or (ii) any viruses transmitted through this email or attachments transmitted with it. It is the responsibility of the recipient to ensure that the onward transmission, opening or use of this email or any files or attachments transmitted with it will not adversely affect its systems or data and the recipient should carry out such virus checks and other checks that it considers appropriate. For more information about Epistem please visit: www.epistem.co.uk <http: www.epistem.co.uk=""/> This message has been scanned for viruses by BlackSpider...{{dropped:6}}
• 948 views
ADD COMMENT
0
Entering edit mode
@james-w-macdonald-5106
Last seen 1 day ago
United States
Hi Elliott, elliott harrison wrote: > Hi BioC, > > > > I'm working on a code to create a contrast matrix from the values listed > against array file in the targets file. > > > > I can parse unique values from the input file ok but then I want to > iterate over them to make the comparisons and matrix > > > > Group1-group2,group1-group3,group2-group3 > > > > > > I have included my code that I think should get me close (perhaps need > to strip trailing commas). However using this code the output string is > empty. > > > > Can anyone spot what I'm missing? Yes. Vectorization and the combn() function. > grp <- paste("Group", 1:4, sep = "") > ind <- t(combn(1:4, 2)) > paste(grp[ind[,1]], grp[ind[,2]], sep = "-") [1] "Group1-Group2" "Group1-Group3" "Group1-Group4" "Group2-Group3" [5] "Group2-Group4" "Group3-Group4" Best, Jim > > > > Thanks > > > > Elliott > > > >> sessionInfo() > > R version 2.9.0 (2009-04-17) > > i386-pc-mingw32 > > > > locale: > > LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United > Kingdom.1252;LC_MONETARY=English_United > Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252 > > > > attached base packages: > > [1] stats graphics grDevices utils datasets methods base > > > > > other attached packages: > > [1] limma_2.18.0 > > > > loaded via a namespace (and not attached): > > [1] tools_2.9.0 > > > > > > A=as.matrix(c("Upper", "Lower", "Middle")) > > string = "" > > nItems <- dim(A)[1] > > for ( i in 1:nItems ){ > > for (j in 1:nItems){ > > tolower(as.character(A[[i]])) > > tolower(as.character(A[[j]])) > > if (tolower(as.character(A[[i]])) != > tolower(as.character(A[[j]]))) { > > if(length(i <- > grep(paste(as.character(A[[i]]),as.character(A[[j]]), sep = "-") > ,string)) == 0) { > > > > } > > > else { > > > paste(as.character(A[[i]]),as.character(A[[j]]), sep = "-") > > string > > } > > if(length(i <- > grep(paste(as.character(A[[j]]),as.character(A[[i]]), sep = "-") > ,string)) == 0) { > > paste(string > ,paste(as.character(A[[j]]),as.character(A[[i]]), sep = "-"), sep = > ",") > > } > > else { > > > paste(as.character(A[[j]]),as.character(A[[i]]), sep = "-") > > string > > } > > } > > } > > } > > string > > > > Mr Elliott Harrison > > Senior Research Associate > > Epistem Ltd > 48 Grafton Street > Manchester, M13 9XX. UK > > Office Tel: +44 (0) 161 606 7398 > Fax: +44 (0) 161 606 7348 > > Incorporated and registered in England and Wales under registration > number: 03901952. Registered office: The Incubator Building, Grafton > Street, Manchester M13 9XX. VAT registration number: 903 0350 77. > > This email and any attachments and files are confidential and are > intended only for the addressee(s). Any review, onward transmission, > disclosure or other use of this email or any attachments by any other > person is prohibited. If you receive this email and are not an intended > recipient please inform the sender immediately and delete this message, > removing any copies from your system. Unless expressly stated otherwise, > any opinions expressed in this email or any attachments transmitted with > it are personal and do not constitute the opinions of Epistem Holdings > Plc or any subsidiary company (together "Epistem"). Although emails are > routinely screened for viruses, Epistem does not accept any liability or > responsibility for: (i) changes made to this email or any attachments > transmitted with it after they were sent; or (ii) any viruses > transmitted through this email or attachments transmitted with it. It is > the responsibility of the recipient to ensure that the onward > transmission, opening or use of this email or any files or attachments > transmitted with it will not adversely affect its systems or data and > the recipient should carry out such virus checks and other checks that > it considers appropriate. > > For more information about Epistem please visit: www.epistem.co.uk > <http: www.epistem.co.uk=""/> > > > > This message has been scanned for viruses by BlackSpider...{{dropped:6}} > > _______________________________________________ > Bioconductor mailing list > Bioconductor at stat.math.ethz.ch > https://stat.ethz.ch/mailman/listinfo/bioconductor > Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor -- James W. MacDonald, M.S. Biostatistician Douglas Lab University of Michigan Department of Human Genetics 5912 Buhl 1241 E. Catherine St. Ann Arbor MI 48109-5618 734-615-7826
ADD COMMENT
0
Entering edit mode
@elliot-harrison-2391
Last seen 10.2 years ago
Hi Jim, Thanks that is great stopped me going round the houses to achieve this. Elliott Message: 12 Date: Fri, 24 Jul 2009 11:32:55 -0400 From: "James W. MacDonald" <jmacdon@med.umich.edu> Subject: Re: [BioC] Help with loop logic To: elliott harrison <e.harrison@epistem.co.uk> Cc: bioconductor@stat.math.ethz.ch Message-ID: <4A69D427.4060003@med.umich.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Hi Elliott, elliott harrison wrote: > Hi BioC, > > > > I'm working on a code to create a contrast matrix from the values listed > against array file in the targets file. > > > > I can parse unique values from the input file ok but then I want to > iterate over them to make the comparisons and matrix > > > > Group1-group2,group1-group3,group2-group3 > > > > > > I have included my code that I think should get me close (perhaps need > to strip trailing commas). However using this code the output string is > empty. > > > > Can anyone spot what I'm missing? Yes. Vectorization and the combn() function. > grp <- paste("Group", 1:4, sep = "") > ind <- t(combn(1:4, 2)) > paste(grp[ind[,1]], grp[ind[,2]], sep = "-") [1] "Group1-Group2" "Group1-Group3" "Group1-Group4" "Group2-Group3" [5] "Group2-Group4" "Group3-Group4" Best, Jim This message has been scanned for viruses by BlackSpider...{{dropped:6}}
ADD COMMENT

Login before adding your answer.

Traffic: 572 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6