Re: [R] condense repetitive code for read.csv and rename.vars
Thanks very much A.K. I can see from answers to my previous posts, too, that I will be using lapply() a lot in my R future. BNC From: arun kirshna [via R] [mailto:ml-node+s789695n4673817...@n4.nabble.com] Sent: Wednesday, August 14, 2013 11:06 PM To: Crombie, Burnette N Subject: Re: condense repetitive code for read.csv and rename.vars HI, You could try: #If all the files are in the working directory: vec1-list.files() #Created 3 dummy files in my WD vec1 #[1] mergedStatstA.csv mergedStatstB.csv mergedStatstC.csv library(gdata) lapply(seq_along(vec1),function(i) {x1-read.csv(vec1[i],header=TRUE,sep=\t);x2-rename.vars(x1,from=X,to=Statistics.Calculated,info=FALSE);write.csv(x2,paste0(Modified_,vec1[i]),row.names=FALSE)}) ###If you want #to save it as a different file name. list.files() #[1] mergedStatstA.csv mergedStatstB.csv #[3] mergedStatstC.csv Modified_mergedStatstA.csv #[5] Modified_mergedStatstB.csv Modified_mergedStatstC.csv vec2- list.files()[grep(Modified,list.files())] sapply(seq_along(vec2),function(i){x1-read.csv(vec2[i],header=TRUE); colnames(x1)}) # [,1][,2][,3] #[1,] Statistics.Calculated Statistics.Calculated Statistics.Calculated #[2,] col2 col3 col4 A.K. - Original Message - From: bcrombie [hidden email]/user/SendEmail.jtp?type=nodenode=4673817i=0 To: [hidden email]/user/SendEmail.jtp?type=nodenode=4673817i=1 Cc: Sent: Wednesday, August 14, 2013 3:43 PM Subject: [R] condense repetitive code for read.csv and rename.vars Is there a more concise way to write the following code? library(gdata) mydataOUTPUTrtfA - read.csv(mergedStatstA.csv) save(mydataOUTPUTrtfA, file=mydataOUTPUTrtfA.RData) mydataOUTPUTrtfA - rename.vars(mydataOUTPUTrtfA, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfB - read.csv(mergedStatstB.csv) save(mydataOUTPUTrtfB, file=mydataOUTPUTrtfB.RData) mydataOUTPUTrtfB - rename.vars(mydataOUTPUTrtfB, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfC - read.csv(mergedStatstC.csv) save(mydataOUTPUTrtfC, file=mydataOUTPUTrtfC.RData) mydataOUTPUTrtfC - rename.vars(mydataOUTPUTrtfC, from=X, to=Statistics.Calculated, info=FALSE) I will have a series of mydataOUTPUTrtf files spanning a large portion of the alphabet, so to speak: e.g. mydataOUTPUTrtfA to mydataOUTPUTrtfG --- thanks for your help -- View this message in context: http://r.789695.n4.nabble.com/condense-repetitive-code-for-read-csv-and-rename-vars-tp4673783.html Sent from the R help mailing list archive at Nabble.com. __ [hidden email]/user/SendEmail.jtp?type=nodenode=4673817i=2 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. __ [hidden email]/user/SendEmail.jtp?type=nodenode=4673817i=3 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. If you reply to this email, your message will be added to the discussion below: http://r.789695.n4.nabble.com/condense-repetitive-code-for-read-csv-and-rename-vars-tp4673783p4673817.html To unsubscribe from condense repetitive code for read.csv and rename.vars, click herehttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_codenode=4673783code=YmNyb21iaWVAdXRrLmVkdXw0NjczNzgzfC0xMzI5MzM0NzI3. NAMLhttp://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewerid=instant_html%21nabble%3Aemail.namlbase=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespacebreadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml -- View this message in context: http://r.789695.n4.nabble.com/condense-repetitive-code-for-read-csv-and-rename-vars-tp4673783p4673856.html Sent from the R help mailing list archive at Nabble.com. [[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.
Re: [R] condense repetitive code for read.csv and rename.vars
Thanks very much for your contribution, Siraaj. I appreciate you taking the time to help me learn loops, etc. BNC -Original Message- From: Siraaj Khandkar [mailto:sir...@khandkar.net] Sent: Wednesday, August 14, 2013 9:08 PM To: Crombie, Burnette N Cc: r-help@r-project.org Subject: Re: [R] condense repetitive code for read.csv and rename.vars On 08/14/2013 03:43 PM, bcrombie wrote: Is there a more concise way to write the following code? library(gdata) mydataOUTPUTrtfA - read.csv(mergedStatstA.csv) save(mydataOUTPUTrtfA, file=mydataOUTPUTrtfA.RData) mydataOUTPUTrtfA - rename.vars(mydataOUTPUTrtfA, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfB - read.csv(mergedStatstB.csv) save(mydataOUTPUTrtfB, file=mydataOUTPUTrtfB.RData) mydataOUTPUTrtfB - rename.vars(mydataOUTPUTrtfB, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfC - read.csv(mergedStatstC.csv) save(mydataOUTPUTrtfC, file=mydataOUTPUTrtfC.RData) mydataOUTPUTrtfC - rename.vars(mydataOUTPUTrtfC, from=X, to=Statistics.Calculated, info=FALSE) I will have a series of mydataOUTPUTrtf files spanning a large portion of the alphabet, so to speak: e.g. mydataOUTPUTrtfA to mydataOUTPUTrtfG --- thanks for your help alphabet - c(FOO, BAR, BAZ) for (a in alphabet) { filename - paste(c(basename, a, .csv), collapse=) data - read.csv(filename) date - rename.vars( data , from=X , to=Statistics.Calculated , info=FALSE ) # do some other stuff with data } You should be able to pick it up from here. In case you need an actual alphabet, it is already predefined: LETTERS [1] A B C D E F G H I J K L M N O P Q [18] R S T U V W X Y Z letters [1] a b c d e f g h i j k l m n o p q [18] r s t u v w x y z __ 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.
[R] condense repetitive code for read.csv and rename.vars
Is there a more concise way to write the following code? library(gdata) mydataOUTPUTrtfA - read.csv(mergedStatstA.csv) save(mydataOUTPUTrtfA, file=mydataOUTPUTrtfA.RData) mydataOUTPUTrtfA - rename.vars(mydataOUTPUTrtfA, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfB - read.csv(mergedStatstB.csv) save(mydataOUTPUTrtfB, file=mydataOUTPUTrtfB.RData) mydataOUTPUTrtfB - rename.vars(mydataOUTPUTrtfB, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfC - read.csv(mergedStatstC.csv) save(mydataOUTPUTrtfC, file=mydataOUTPUTrtfC.RData) mydataOUTPUTrtfC - rename.vars(mydataOUTPUTrtfC, from=X, to=Statistics.Calculated, info=FALSE) I will have a series of mydataOUTPUTrtf files spanning a large portion of the alphabet, so to speak: e.g. mydataOUTPUTrtfA to mydataOUTPUTrtfG --- thanks for your help -- View this message in context: http://r.789695.n4.nabble.com/condense-repetitive-code-for-read-csv-and-rename-vars-tp4673783.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] condense repetitive code for read.csv and rename.vars
On 08/14/2013 03:43 PM, bcrombie wrote: Is there a more concise way to write the following code? library(gdata) mydataOUTPUTrtfA - read.csv(mergedStatstA.csv) save(mydataOUTPUTrtfA, file=mydataOUTPUTrtfA.RData) mydataOUTPUTrtfA - rename.vars(mydataOUTPUTrtfA, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfB - read.csv(mergedStatstB.csv) save(mydataOUTPUTrtfB, file=mydataOUTPUTrtfB.RData) mydataOUTPUTrtfB - rename.vars(mydataOUTPUTrtfB, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfC - read.csv(mergedStatstC.csv) save(mydataOUTPUTrtfC, file=mydataOUTPUTrtfC.RData) mydataOUTPUTrtfC - rename.vars(mydataOUTPUTrtfC, from=X, to=Statistics.Calculated, info=FALSE) I will have a series of mydataOUTPUTrtf files spanning a large portion of the alphabet, so to speak: e.g. mydataOUTPUTrtfA to mydataOUTPUTrtfG --- thanks for your help alphabet - c(FOO, BAR, BAZ) for (a in alphabet) { filename - paste(c(basename, a, .csv), collapse=) data - read.csv(filename) date - rename.vars( data , from=X , to=Statistics.Calculated , info=FALSE ) # do some other stuff with data } You should be able to pick it up from here. In case you need an actual alphabet, it is already predefined: LETTERS [1] A B C D E F G H I J K L M N O P Q [18] R S T U V W X Y Z letters [1] a b c d e f g h i j k l m n o p q [18] r s t u v w x y z __ 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.
Re: [R] condense repetitive code for read.csv and rename.vars
HI, You could try: #If all the files are in the working directory: vec1-list.files() #Created 3 dummy files in my WD vec1 #[1] mergedStatstA.csv mergedStatstB.csv mergedStatstC.csv library(gdata) lapply(seq_along(vec1),function(i) {x1-read.csv(vec1[i],header=TRUE,sep=\t);x2-rename.vars(x1,from=X,to=Statistics.Calculated,info=FALSE);write.csv(x2,paste0(Modified_,vec1[i]),row.names=FALSE)}) ###If you want #to save it as a different file name. list.files() #[1] mergedStatstA.csv mergedStatstB.csv #[3] mergedStatstC.csv Modified_mergedStatstA.csv #[5] Modified_mergedStatstB.csv Modified_mergedStatstC.csv vec2- list.files()[grep(Modified,list.files())] sapply(seq_along(vec2),function(i){x1-read.csv(vec2[i],header=TRUE); colnames(x1)}) # [,1] [,2] [,3] #[1,] Statistics.Calculated Statistics.Calculated Statistics.Calculated #[2,] col2 col3 col4 A.K. - Original Message - From: bcrombie bcrom...@utk.edu To: r-help@r-project.org Cc: Sent: Wednesday, August 14, 2013 3:43 PM Subject: [R] condense repetitive code for read.csv and rename.vars Is there a more concise way to write the following code? library(gdata) mydataOUTPUTrtfA - read.csv(mergedStatstA.csv) save(mydataOUTPUTrtfA, file=mydataOUTPUTrtfA.RData) mydataOUTPUTrtfA - rename.vars(mydataOUTPUTrtfA, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfB - read.csv(mergedStatstB.csv) save(mydataOUTPUTrtfB, file=mydataOUTPUTrtfB.RData) mydataOUTPUTrtfB - rename.vars(mydataOUTPUTrtfB, from=X, to=Statistics.Calculated, info=FALSE) mydataOUTPUTrtfC - read.csv(mergedStatstC.csv) save(mydataOUTPUTrtfC, file=mydataOUTPUTrtfC.RData) mydataOUTPUTrtfC - rename.vars(mydataOUTPUTrtfC, from=X, to=Statistics.Calculated, info=FALSE) I will have a series of mydataOUTPUTrtf files spanning a large portion of the alphabet, so to speak: e.g. mydataOUTPUTrtfA to mydataOUTPUTrtfG --- thanks for your help -- View this message in context: http://r.789695.n4.nabble.com/condense-repetitive-code-for-read-csv-and-rename-vars-tp4673783.html Sent from the R help mailing list archive at Nabble.com. __ 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. __ 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.