I am having difficulty storing the output of a for loop I have generated. All I want to do is find all the files that I have, create a string with all of the names in quotes and separated by commas. This is proving more difficult than I initially anticipated. I am sure it is either very simple or the construction of the for loop is not quite right The result gets automatically printed after the loop but I can't seem to save it. I have tried to create the element in advance but the result is the same: NULL individual.proj = Sys.glob("Arabica/proj_current/individual_projections/*.img", dirmark = FALSE) individual.proj [1] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GBM.img" [2] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GLM.img" [3] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_MARS.img" [4] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_RF.img" [5] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_RUN10_GBM.img" ##generate loop to create string out of the table of projected files. L.ip = length(individual.proj) for (i in 1:L.ip){ individual.proj.i <- individual.proj[i] individual.proj.quote = cat(paste('"', individual.proj.i, '"', ',',sep="")) } "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GBM.img","Arabica/proj_current/individual_projections/proj_current.... ##print output string individual.proj.quote NULL #command to be applied to individual.proj.quote to removed the final comma from the string substr(individual.proj.quote, 1, nchar(individual.proj.quote)-1) Any help or pointers would be greatly appreciated, no amount of extensive google searches have been fruitful so far. ****************** Jenny Williams Spatial Information Scientist, GIS Unit Herbarium, Library, Art & Archives Directorate Royal Botanic Gardens, Kew Richmond, TW9 3AB, UK Tel: +44 (0)208 332 5277 email: jenny.williams@kew.org<mailto:jenny.williams@kew.org> ****************** Film: The Forgotten Home of Coffee - Beyond the Gardens<http://www.youtube.com/watch?v=-uDtytKMKpA&sns=tw> Stories: Coffee Expedition - Ethiopia<http://storify.com/KewGIS/coffee-expedition-ethiopia> Kew in Harapan Rainforest Sumatra<http://storify.com/KewGIS/kew-in-harapan-rainforest> Articles: Seeing the wood for the trees<http://www.kew.org/ucm/groups/public/documents/document/kppcont_060602.pdf> How Kew's GIS team and South East Asia botanists are working to help conserve and restore a rainforest in Sumatra. Download a pdf of this article here.<http://www.kew.org/ucm/groups/public/documents/document/kppcont_060602.pdf> ________________________________ The Royal Botanic Gardens, Kew is a non-departmental public body with exempt charitable status, whose principal place of business is at Royal Botanic Gardens, Kew, Richmond, Surrey TW9 3AB, United Kingdom. The information contained in this email and any attachments is intended solely for the addressee(s) and may contain confidential or legally privileged information. If you have received this message in error, please return it immediately and permanently delete it. Do not use, copy or disclose the information contained in this email or in any attachment. Any views expressed in this email do not necessarily reflect the opinions of RBG Kew. Any files attached to this email have been inspected with virus detection software by RBG Kew before transmission, however you should carry out your own virus checks before opening any attachments. RBG Kew accepts no liability for any loss or damage which may be caused by software viruses. [[alternative HTML version deleted]]
Hi Jenny, Firstly, to my knowledge you cannot assign the output of cat to an object (i.e. it only prints it). Second, you can just add the 'collapse' option of the paste function. individual.proj.quote <- paste(individual.proj, collapse = ",") if you really want the quotes individual.proj.quote <- paste(individual.proj, collapse='","') but you will be stuck with some backslashes I can't recall the syntax to remove. Hope this serves your purposes Cheers, Charles On Thu, Aug 8, 2013 at 10:05 AM, Jenny Williams <jenny.williams@kew.org>wrote:> I am having difficulty storing the output of a for loop I have generated. > All I want to do is find all the files that I have, create a string with > all of the names in quotes and separated by commas. This is proving more > difficult than I initially anticipated. > I am sure it is either very simple or the construction of the for loop is > not quite right > The result gets automatically printed after the loop but I can't seem to > save it. > I have tried to create the element in advance but the result is the same: > NULL > > individual.proj > Sys.glob("Arabica/proj_current/individual_projections/*.img", dirmark > FALSE) > individual.proj > [1] > "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GBM.img" > [2] > "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GLM.img" > [3] > "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_MARS.img" > [4] > "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_RF.img" > [5] > "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_RUN10_GBM.img" > > > ##generate loop to create string out of the table of projected files. > L.ip = length(individual.proj) > for (i in 1:L.ip){ > individual.proj.i <- individual.proj[i] > individual.proj.quote = cat(paste('"', individual.proj.i, '"', > ',',sep="")) > } > > "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GBM.img","Arabica/proj_current/individual_projections/proj_current.... > > ##print output string > individual.proj.quote > NULL > > #command to be applied to individual.proj.quote to removed the final comma > from the string > substr(individual.proj.quote, 1, nchar(individual.proj.quote)-1) > > Any help or pointers would be greatly appreciated, no amount of extensive > google searches have been fruitful so far. > > > ****************** > Jenny Williams > Spatial Information Scientist, GIS Unit > Herbarium, Library, Art & Archives Directorate > Royal Botanic Gardens, Kew > Richmond, TW9 3AB, UK > > Tel: +44 (0)208 332 5277 > email: jenny.williams@kew.org<mailto:jenny.williams@kew.org> > ****************** > > Film: The Forgotten Home of Coffee - Beyond the Gardens< > http://www.youtube.com/watch?v=-uDtytKMKpA&sns=tw> > Stories: Coffee Expedition - Ethiopia< > http://storify.com/KewGIS/coffee-expedition-ethiopia> > Kew in Harapan Rainforest Sumatra< > http://storify.com/KewGIS/kew-in-harapan-rainforest> > Articles: Seeing the wood for the trees< > http://www.kew.org/ucm/groups/public/documents/document/kppcont_060602.pdf > > > How Kew's GIS team and South East Asia botanists are working to help > conserve and restore a rainforest in Sumatra. Download a pdf of this > article here.< > http://www.kew.org/ucm/groups/public/documents/document/kppcont_060602.pdf > > > > > ________________________________ > The Royal Botanic Gardens, Kew is a non-departmental public body with > exempt charitable status, whose principal place of business is at Royal > Botanic Gardens, Kew, Richmond, Surrey TW9 3AB, United Kingdom. > > The information contained in this email and any attachments is intended > solely for the addressee(s) and may contain confidential or legally > privileged information. If you have received this message in error, please > return it immediately and permanently delete it. Do not use, copy or > disclose the information contained in this email or in any attachment. > > Any views expressed in this email do not necessarily reflect the opinions > of RBG Kew. > > Any files attached to this email have been inspected with virus detection > software by RBG Kew before transmission, however you should carry out your > own virus checks before opening any attachments. RBG Kew accepts no > liability for any loss or damage which may be caused by software viruses. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Charles Determan Integrated Biosciences PhD Candidate University of Minnesota [[alternative HTML version deleted]]
It's not clear how you are planning to use this within R, but you don't need a loop. individual.proj.quote <- capture.output(write.table(matrix(individual.proj, 1), quote=TRUE, sep=",", row.names=FALSE, col.names=FALSE)) This produces a single character string which consists of the quoted file names separated by commas. ------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jenny Williams Sent: Thursday, August 8, 2013 10:06 AM To: 'r-help at r-project.org' Subject: [R] For loop output I am having difficulty storing the output of a for loop I have generated. All I want to do is find all the files that I have, create a string with all of the names in quotes and separated by commas. This is proving more difficult than I initially anticipated. I am sure it is either very simple or the construction of the for loop is not quite right The result gets automatically printed after the loop but I can't seem to save it. I have tried to create the element in advance but the result is the same: NULL individual.proj Sys.glob("Arabica/proj_current/individual_projections/*.img", dirmark = FALSE) individual.proj [1] "Arabica/proj_current/individual_projections/proj_current_arabic a_pa.data.tmp$pa.tab_Full_GBM.img" [2] "Arabica/proj_current/individual_projections/proj_current_arabic a_pa.data.tmp$pa.tab_Full_GLM.img" [3] "Arabica/proj_current/individual_projections/proj_current_arabic a_pa.data.tmp$pa.tab_Full_MARS.img" [4] "Arabica/proj_current/individual_projections/proj_current_arabic a_pa.data.tmp$pa.tab_Full_RF.img" [5] "Arabica/proj_current/individual_projections/proj_current_arabic a_pa.data.tmp$pa.tab_RUN10_GBM.img" ##generate loop to create string out of the table of projected files. L.ip = length(individual.proj) for (i in 1:L.ip){ individual.proj.i <- individual.proj[i] individual.proj.quote = cat(paste('"', individual.proj.i, '"', ',',sep="")) } "Arabica/proj_current/individual_projections/proj_current_arabic a_pa.data.tmp$pa.tab_Full_GBM.img","Arabica/proj_current/individ ual_projections/proj_current.... ##print output string individual.proj.quote NULL #command to be applied to individual.proj.quote to removed the final comma from the string substr(individual.proj.quote, 1, nchar(individual.proj.quote)-1) Any help or pointers would be greatly appreciated, no amount of extensive google searches have been fruitful so far. ****************** Jenny Williams Spatial Information Scientist, GIS Unit Herbarium, Library, Art & Archives Directorate Royal Botanic Gardens, Kew Richmond, TW9 3AB, UK Tel: +44 (0)208 332 5277 email: jenny.williams at kew.org<mailto:jenny.williams at kew.org> ****************** Film: The Forgotten Home of Coffee - Beyond the Gardens<http://www.youtube.com/watch?v=-uDtytKMKpA&sns=tw> Stories: Coffee Expedition - Ethiopia<http://storify.com/KewGIS/coffee-expedition-ethiopia> Kew in Harapan Rainforest Sumatra<http://storify.com/KewGIS/kew-in-harapan-rainforest> Articles: Seeing the wood for the trees<http://www.kew.org/ucm/groups/public/documents/document/kp pcont_060602.pdf> How Kew's GIS team and South East Asia botanists are working to help conserve and restore a rainforest in Sumatra. Download a pdf of this article here.<http://www.kew.org/ucm/groups/public/documents/document/kp pcont_060602.pdf> ________________________________ The Royal Botanic Gardens, Kew is a non-departmental public body with exempt charitable status, whose principal place of business is at Royal Botanic Gardens, Kew, Richmond, Surrey TW9 3AB, United Kingdom. The information contained in this email and any attachments is intended solely for the addressee(s) and may contain confidential or legally privileged information. If you have received this message in error, please return it immediately and permanently delete it. Do not use, copy or disclose the information contained in this email or in any attachment. Any views expressed in this email do not necessarily reflect the opinions of RBG Kew. Any files attached to this email have been inspected with virus detection software by RBG Kew before transmission, however you should carry out your own virus checks before opening any attachments. RBG Kew accepts no liability for any loss or damage which may be caused by software viruses. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list 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.
On Thu, Aug 08, 2013 at 04:05:57PM +0100, Jenny Williams wrote:> I am having difficulty storing the output of a for loop I have generated. All I want to do is find all the files that I have, create a string with all of the names in quotes and separated by commas. This is proving more difficult than I initially anticipated. > I am sure it is either very simple or the construction of the for loop is not quite right > The result gets automatically printed after the loop but I can't seem to save it. > I have tried to create the element in advance but the result is the same: NULLThis is a somewhat frequent confusion of the (very different!) concepts of assignment and printing. The fact that something becomes visible to the human user via printing has nothing to do really with the fact that an object is generated and assigned to a variable, thereby becoming accessible (and, in that allegorical sense, "visible") to the subsequent code. The cat function prints values but it does not return them. It returns NULL, which is what you get.> individual.proj = Sys.glob("Arabica/proj_current/individual_projections/*.img", dirmark = FALSE) > individual.proj > [1] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GBM.img" > [2] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GLM.img" > [3] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_MARS.img" > [4] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_RF.img" > [5] "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_RUN10_GBM.img" > > > ##generate loop to create string out of the table of projected files. > L.ip = length(individual.proj) > for (i in 1:L.ip){ > individual.proj.i <- individual.proj[i] > individual.proj.quote = cat(paste('"', individual.proj.i, '"', ',',sep="")) > } > "Arabica/proj_current/individual_projections/proj_current_arabica_pa.data.tmp$pa.tab_Full_GBM.img","Arabica/proj_current/individual_projections/proj_current.... > > ##print output string > individual.proj.quote > NULL > > #command to be applied to individual.proj.quote to removed the final comma from the string > substr(individual.proj.quote, 1, nchar(individual.proj.quote)-1) > > Any help or pointers would be greatly appreciated, no amount of extensive google searches have been fruitful so far.If you don't mind me suggesting this, reading the documentation page of the function(s) you use is an approach that is more targeted and therefore often quicker. (Googling "cat" is probably especially bad, as there are various cat applications and functions in several languages, not to mention several species of mammals... ;-) ) As a further remark, you don't need a loop, one line composed of sprintf and paste (check the collapse parameter) should do the trick you're after. Best regards, Jan> > ****************** > Jenny Williams > Spatial Information Scientist, GIS Unit > Herbarium, Library, Art & Archives Directorate > Royal Botanic Gardens, Kew > Richmond, TW9 3AB, UK > > Tel: +44 (0)208 332 5277 > email: jenny.williams at kew.org<mailto:jenny.williams at kew.org> > ****************** > > Film: The Forgotten Home of Coffee - Beyond the Gardens<http://www.youtube.com/watch?v=-uDtytKMKpA&sns=tw> > Stories: Coffee Expedition - Ethiopia<http://storify.com/KewGIS/coffee-expedition-ethiopia> > Kew in Harapan Rainforest Sumatra<http://storify.com/KewGIS/kew-in-harapan-rainforest> > Articles: Seeing the wood for the trees<http://www.kew.org/ucm/groups/public/documents/document/kppcont_060602.pdf> > How Kew's GIS team and South East Asia botanists are working to help conserve and restore a rainforest in Sumatra. Download a pdf of this article here.<http://www.kew.org/ucm/groups/public/documents/document/kppcont_060602.pdf> > > > ________________________________ > The Royal Botanic Gardens, Kew is a non-departmental public body with exempt charitable status, whose principal place of business is at Royal Botanic Gardens, Kew, Richmond, Surrey TW9 3AB, United Kingdom. > > The information contained in this email and any attachments is intended solely for the addressee(s) and may contain confidential or legally privileged information. If you have received this message in error, please return it immediately and permanently delete it. Do not use, copy or disclose the information contained in this email or in any attachment. > > Any views expressed in this email do not necessarily reflect the opinions of RBG Kew. > > Any files attached to this email have been inspected with virus detection software by RBG Kew before transmission, however you should carry out your own virus checks before opening any attachments. RBG Kew accepts no liability for any loss or damage which may be caused by software viruses. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- +- Jan T. Kim -------------------------------------------------------+ | email: jttkim at gmail.com | | WWW: http://www.jtkim.dreamhosters.com/ | *-----=< hierarchical systems are for files, not for humans >=-----*
If I understand the request correctly, here is an easy to follow example: (I'm using the first four letters as surrogates for the file names) (and assuming we want quotes at both the beginning and the end)> tmp <- letters[1:4] > tmp[1] "a" "b" "c" "d"> foo <- paste( "'", paste(tmp,collapse="','"), "'", sep='') > cat(foo,'\n')'a','b','c','d' In this case, cat() does a better job than print() of telling you exactly what you have. I use this sort of thing to construct arguments for the SQL "in" clause) -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 8/8/13 8:05 AM, "Jenny Williams" <jenny.williams at kew.org> wrote:>I am having difficulty storing the output of a for loop I have generated. >All I want to do is find all the files that I have, create a string with >all of the names in quotes and separated by commas. This is proving more >difficult than I initially anticipated. >I am sure it is either very simple or the construction of the for loop is >not quite right >The result gets automatically printed after the loop but I can't seem to >save it. >I have tried to create the element in advance but the result is the same: >NULL > >individual.proj = >Sys.glob("Arabica/proj_current/individual_projections/*.img", dirmark >FALSE) >individual.proj >[1] >"Arabica/proj_current/individual_projections/proj_current_arabica_pa.data. >tmp$pa.tab_Full_GBM.img" > [2] >"Arabica/proj_current/individual_projections/proj_current_arabica_pa.data. >tmp$pa.tab_Full_GLM.img" > [3] >"Arabica/proj_current/individual_projections/proj_current_arabica_pa.data. >tmp$pa.tab_Full_MARS.img" > [4] >"Arabica/proj_current/individual_projections/proj_current_arabica_pa.data. >tmp$pa.tab_Full_RF.img" > [5] >"Arabica/proj_current/individual_projections/proj_current_arabica_pa.data. >tmp$pa.tab_RUN10_GBM.img" > > >##generate loop to create string out of the table of projected files. >L.ip = length(individual.proj) > for (i in 1:L.ip){ > individual.proj.i <- individual.proj[i] > individual.proj.quote = cat(paste('"', individual.proj.i, '"', >',',sep="")) > } >"Arabica/proj_current/individual_projections/proj_current_arabica_pa.data. >tmp$pa.tab_Full_GBM.img","Arabica/proj_current/individual_projections/proj >_current.... > >##print output string >individual.proj.quote >NULL > >#command to be applied to individual.proj.quote to removed the final >comma from the string >substr(individual.proj.quote, 1, nchar(individual.proj.quote)-1) > >Any help or pointers would be greatly appreciated, no amount of extensive >google searches have been fruitful so far. > > >****************** >Jenny Williams >Spatial Information Scientist, GIS Unit >Herbarium, Library, Art & Archives Directorate >Royal Botanic Gardens, Kew >Richmond, TW9 3AB, UK > >Tel: +44 (0)208 332 5277 >email: jenny.williams at kew.org<mailto:jenny.williams at kew.org> >****************** > >Film: The Forgotten Home of Coffee - Beyond the >Gardens<http://www.youtube.com/watch?v=-uDtytKMKpA&sns=tw> >Stories: Coffee Expedition - >Ethiopia<http://storify.com/KewGIS/coffee-expedition-ethiopia> > Kew in Harapan Rainforest >Sumatra<http://storify.com/KewGIS/kew-in-harapan-rainforest> >Articles: Seeing the wood for the >trees<http://www.kew.org/ucm/groups/public/documents/document/kppcont_0606 >02.pdf> >How Kew's GIS team and South East Asia botanists are working to help >conserve and restore a rainforest in Sumatra. Download a pdf of this >article >here.<http://www.kew.org/ucm/groups/public/documents/document/kppcont_0606 >02.pdf> > > >________________________________ >The Royal Botanic Gardens, Kew is a non-departmental public body with >exempt charitable status, whose principal place of business is at Royal >Botanic Gardens, Kew, Richmond, Surrey TW9 3AB, United Kingdom. > >The information contained in this email and any attachments is intended >solely for the addressee(s) and may contain confidential or legally >privileged information. If you have received this message in error, >please return it immediately and permanently delete it. Do not use, copy >or disclose the information contained in this email or in any attachment. > >Any views expressed in this email do not necessarily reflect the opinions >of RBG Kew. > >Any files attached to this email have been inspected with virus detection >software by RBG Kew before transmission, however you should carry out >your own virus checks before opening any attachments. RBG Kew accepts no >liability for any loss or damage which may be caused by software viruses. > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list >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.