I have 682 variables in a data frame , and a function that I should feed 682 variables in this function one by one and each time save the file as a special name! for emaple: my data frame file includes 682 names : 1 aaa 2 bbb 3 dfdsfg 4 fghh . 682 fgfhg and a function like prep(Z, aaa, .....) and each time I should change the variable name in this function and read the variable from the data frame and each time I should save the file as a special name such as: prep1<- prep(z, aaa,...) prep2<- prep(z, bbb,...) prep3<- prep(z, dfdsfg,..) Prep4<- prep(z, fghh,...) How can I use loop function in R to that? Thanks [[alternative HTML version deleted]]
for (var_name in names(z)) { # assuming the prep function writes the content of z$var_name to the file var_name.csv prep(z, var_name) } On Wed, Jul 1, 2015 at 10:18 PM Lida Zeighami <lid.zigh at gmail.com> wrote:> I have 682 variables in a data frame , and a function that I should feed > 682 variables in this function one by one and each time save the file as a > special name! > for emaple: > my data frame file includes 682 names : > 1 aaa > 2 bbb > 3 dfdsfg > 4 fghh > . > > 682 fgfhg > and a function like prep(Z, aaa, .....) and each time I should change the > variable name in this function and read the variable from the data frame > and each time I should save the file as a special name such as: > > prep1<- prep(z, aaa,...) > prep2<- prep(z, bbb,...) > prep3<- prep(z, dfdsfg,..) > Prep4<- prep(z, fghh,...) > > How can I use loop function in R to that? > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
You never said how you wanted to save the data, so I will choose to use 'saveRDS' which should handle most anything. for (i in name_file$names){ saveRDS(prep(z, i), file = paste0(i, '.RDS')) } Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Wed, Jul 1, 2015 at 2:07 PM, Lida Zeighami <lid.zigh at gmail.com> wrote:> I have 682 variables in a data frame , and a function that I should feed > 682 variables in this function one by one and each time save the file as a > special name! > for emaple: > my data frame file includes 682 names : > 1 aaa > 2 bbb > 3 dfdsfg > 4 fghh > . > > 682 fgfhg > and a function like prep(Z, aaa, .....) and each time I should change the > variable name in this function and read the variable from the data frame > and each time I should save the file as a special name such as: > > prep1<- prep(z, aaa,...) > prep2<- prep(z, bbb,...) > prep3<- prep(z, dfdsfg,..) > Prep4<- prep(z, fghh,...) > > How can I use loop function in R to that? > > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >[[alternative HTML version deleted]]
I forgot that you also wanted to change the variable name; I would suggest that you don't use individual objects, but instead use a 'list'. Here is how it would change result <- lapply(name_file$names, function(.file){ result <- prep(z, .file) saveRDS(result, file = paste0(.file, '.RDS')) result # return value that goes in the list }) Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Wed, Jul 1, 2015 at 9:00 PM, jim holtman <jholtman at gmail.com> wrote:> You never said how you wanted to save the data, so I will choose to use > 'saveRDS' which should handle most anything. > > for (i in name_file$names){ > saveRDS(prep(z, i), file = paste0(i, '.RDS')) > } > > > > Jim Holtman > Data Munger Guru > > What is the problem that you are trying to solve? > Tell me what you want to do, not how you want to do it. > > On Wed, Jul 1, 2015 at 2:07 PM, Lida Zeighami <lid.zigh at gmail.com> wrote: > >> I have 682 variables in a data frame , and a function that I should feed >> 682 variables in this function one by one and each time save the file as a >> special name! >> for emaple: >> my data frame file includes 682 names : >> 1 aaa >> 2 bbb >> 3 dfdsfg >> 4 fghh >> . >> >> 682 fgfhg >> and a function like prep(Z, aaa, .....) and each time I should change the >> variable name in this function and read the variable from the data frame >> and each time I should save the file as a special name such as: >> >> prep1<- prep(z, aaa,...) >> prep2<- prep(z, bbb,...) >> prep3<- prep(z, dfdsfg,..) >> Prep4<- prep(z, fghh,...) >> >> How can I use loop function in R to that? >> >> Thanks >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at 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. >> > >[[alternative HTML version deleted]]
Thank you so much for replying me! for better understanding my problem, I explain my problem more: I have a 682*1 matrix called "met" , the first 5 rows similar below:> rownames(met)[1:5][1] "glycine_imp" [2] "Nacetylglycine_imp" [3] "sarcosine_imp" [4] "dimethylglycine_imp" [5] "betaine_imp" and I have a function in R that each time use one of the row names of "met" matrix and create a new object file and I should save the objects! my function is " Scores(Z=metalofGT,formula="met[i]~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") " that each time just I should change the met[i] and replace by row names "met" one by one and for each of them I should rename the function and after that I should save each object! for example for first row of "met" I have> prep1<- Scores(Z=metalofGT,formula="glycine_imp~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER")#creat the object file for first row and called prep1###> save(prep1, file="prep1.RData", compress="bzip2") ##save theobject file as "prep1.RData"##### I should do this process for 682 row names of "met" matrix and at the end I should have "prep1.RData" , "prep2.RData" , "prep3.RData" so, would you please help me how to do it? Many Thanks, Ati On Wed, Jul 1, 2015 at 1:07 PM, Lida Zeighami <lid.zigh at gmail.com> wrote:> I have 682 variables in a data frame , and a function that I should feed > 682 variables in this function one by one and each time save the file as a > special name! > for emaple: > my data frame file includes 682 names : > 1 aaa > 2 bbb > 3 dfdsfg > 4 fghh > . > > 682 fgfhg > and a function like prep(Z, aaa, .....) and each time I should change the > variable name in this function and read the variable from the data frame > and each time I should save the file as a special name such as: > > prep1<- prep(z, aaa,...) > prep2<- prep(z, bbb,...) > prep3<- prep(z, dfdsfg,..) > Prep4<- prep(z, fghh,...) > > How can I use loop function in R to that? > > Thanks >[[alternative HTML version deleted]]
Hi I cannot test it in absence of data. Anyway I would not use saving single objects but use list instead. Something like lll <- vector(mode="list", nrow(met)) for (i in 1:nrow(met)) { form <- as.formula(paste(met[i], "~ egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER")) lll[i] <- Scores(Z=metalofGT,formula=form) } Cheers Petr> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Lida > Zeighami > Sent: Thursday, July 02, 2015 6:48 PM > To: r-help at r-project.org > Subject: Re: [R] question > > Thank you so much for replying me! > for better understanding my problem, I explain my problem more: > > I have a 682*1 matrix called "met" , the first 5 rows similar below: > > > rownames(met)[1:5] > > [1] "glycine_imp" > [2] "Nacetylglycine_imp" > [3] "sarcosine_imp" > [4] "dimethylglycine_imp" > [5] "betaine_imp" > > and I have a function in R that each time use one of the row names of > "met" > matrix and create a new object file and I should save the objects! > > my function is " > Scores(Z=metalofGT,formula="met[i]~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+G > ENDER") > " that each time just I should change the met[i] and replace by row > names > "met" one by one and for each of them I should rename the function and > after that I should save each object! > for example for first row of "met" I have > > > prep1<- > Scores(Z=metalofGT,formula="glycine_imp~egfr_v1_ckdepi+pc1+pc2+pc3+V1AG > E01+GENDER") > #creat the object file for first row and called prep1### > > > save(prep1, file="prep1.RData", compress="bzip2") ##save the > object file as "prep1.RData"##### > > I should do this process for 682 row names of "met" matrix and at the > end I > should have "prep1.RData" , "prep2.RData" , "prep3.RData" > > so, would you please help me how to do it? > > Many Thanks, > Ati > > On Wed, Jul 1, 2015 at 1:07 PM, Lida Zeighami <lid.zigh at gmail.com> > wrote: > > > I have 682 variables in a data frame , and a function that I should > feed > > 682 variables in this function one by one and each time save the file > as a > > special name! > > for emaple: > > my data frame file includes 682 names : > > 1 aaa > > 2 bbb > > 3 dfdsfg > > 4 fghh > > . > > > > 682 fgfhg > > and a function like prep(Z, aaa, .....) and each time I should change > the > > variable name in this function and read the variable from the data > frame > > and each time I should save the file as a special name such as: > > > > prep1<- prep(z, aaa,...) > > prep2<- prep(z, bbb,...) > > prep3<- prep(z, dfdsfg,..) > > Prep4<- prep(z, fghh,...) > > > > How can I use loop function in R to that? > > > > Thanks > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.________________________________ Tento e-mail a jak?koliv k n?mu p?ipojen? dokumenty jsou d?v?rn? a jsou ur?eny pouze jeho adres?t?m. Jestli?e jste obdr?el(a) tento e-mail omylem, informujte laskav? neprodlen? jeho odes?latele. Obsah tohoto emailu i s p??lohami a jeho kopie vyma?te ze sv?ho syst?mu. Nejste-li zam??len?m adres?tem tohoto emailu, nejste opr?vn?ni tento email jakkoliv u??vat, roz?i?ovat, kop?rovat ?i zve?ej?ovat. Odes?latel e-mailu neodpov?d? za eventu?ln? ?kodu zp?sobenou modifikacemi ?i zpo?d?n?m p?enosu e-mailu. V p??pad?, ?e je tento e-mail sou??st? obchodn?ho jedn?n?: - vyhrazuje si odes?latel pr?vo ukon?it kdykoliv jedn?n? o uzav?en? smlouvy, a to z jak?hokoliv d?vodu i bez uveden? d?vodu. - a obsahuje-li nab?dku, je adres?t opr?vn?n nab?dku bezodkladn? p?ijmout; Odes?latel tohoto e-mailu (nab?dky) vylu?uje p?ijet? nab?dky ze strany p??jemce s dodatkem ?i odchylkou. - trv? odes?latel na tom, ?e p??slu?n? smlouva je uzav?ena teprve v?slovn?m dosa?en?m shody na v?ech jej?ch n?le?itostech. - odes?latel tohoto emailu informuje, ?e nen? opr?vn?n uzav?rat za spole?nost ??dn? smlouvy s v?jimkou p??pad?, kdy k tomu byl p?semn? zmocn?n nebo p?semn? pov??en a takov? pov??en? nebo pln? moc byly adres?tovi tohoto emailu p??padn? osob?, kterou adres?t zastupuje, p?edlo?eny nebo jejich existence je adres?tovi ?i osob? j?m zastoupen? zn?m?. This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients. If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system. If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner. The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email. In case that this e-mail forms part of business dealings: - the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning. - if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation. - the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects. - the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.
Hi Ati, Let's start from the top and see where we finish up. I'll use a somewhat smaller matrix: met<-matrix(runif(5),ncol=1) rownames(met)<-c("glycine_imp","Nacetylglycine_imp","sarcosine_imp", "dimethylglycine_imp","betaine_imp") met [,1] glycine_imp 0.61532855 Nacetylglycine_imp 0.04294675 sarcosine_imp 0.98840385 dimethylglycine_imp 0.00507230 betaine_imp 0.68528107 In your example, I think you are mixing up the names and the values of "met". I suspect that you want to use the values for the computation and the names for the filenames. Also, a one column matrix will act very much like a vector, but there are a few problems if you treat it like one. For instance, if you extract a value as though met is a vector: met[2] [1] 0.04294675 you just get the value, not the rowname. Using both indices extracts both the value and the name. met[2,1] Nacetylglycine_imp 0.04294675 As I don't have any idea what the other values in your formula are, I will take the liberty of giving them some: metalofGT<-100 egfr_v1_ckdepi<-1.2 pc1<-2.3 pc2<-3.4 pc3<-4.5 V1AGE01<-27 GENDER<-"F" Before embarking on the loop, I think you have confused the "name of the function", which is "Score", with the return value of the function. I don't think you want to change the function's name or it won't work unless you change the name in the function call. Therefore, I am going to make a blind guess and assume that you want the name of the return value of the function to be the rowname for the value that is used in the function. Now you can do something like this: for(i in 1:nrow(met)) { x<-Scores(Z=metalofGT,formula="met[i]~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") names(x)<-names(met)[i] filename<-paste("prep",i,".Rdata",sep="") save(x, file=filename, compress="bzip2") } This will produce five files with the names you requested, each containing whatever value the function "Score" produces. The name of that value will be the rowname of "met" that produced it. Jim On Fri, Jul 3, 2015 at 2:48 AM, Lida Zeighami <lid.zigh at gmail.com> wrote:> Thank you so much for replying me! > for better understanding my problem, I explain my problem more: > > I have a 682*1 matrix called "met" , the first 5 rows similar below: > >> rownames(met)[1:5] > > [1] "glycine_imp" > [2] "Nacetylglycine_imp" > [3] "sarcosine_imp" > [4] "dimethylglycine_imp" > [5] "betaine_imp" > > and I have a function in R that each time use one of the row names of "met" > matrix and create a new object file and I should save the objects! > > my function is " > Scores(Z=metalofGT,formula="met[i]~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") > " that each time just I should change the met[i] and replace by row names > "met" one by one and for each of them I should rename the function and > after that I should save each object! > for example for first row of "met" I have > >> prep1<- Scores(Z=metalofGT,formula="glycine_imp~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") > #creat the object file for first row and called prep1### > >> save(prep1, file="prep1.RData", compress="bzip2") ##save the > object file as "prep1.RData"##### > > I should do this process for 682 row names of "met" matrix and at the end I > should have "prep1.RData" , "prep2.RData" , "prep3.RData" > > so, would you please help me how to do it? > > Many Thanks, > Ati > > On Wed, Jul 1, 2015 at 1:07 PM, Lida Zeighami <lid.zigh at gmail.com> wrote: > >> I have 682 variables in a data frame , and a function that I should feed >> 682 variables in this function one by one and each time save the file as a >> special name! >> for emaple: >> my data frame file includes 682 names : >> 1 aaa >> 2 bbb >> 3 dfdsfg >> 4 fghh >> . >> >> 682 fgfhg >> and a function like prep(Z, aaa, .....) and each time I should change the >> variable name in this function and read the variable from the data frame >> and each time I should save the file as a special name such as: >> >> prep1<- prep(z, aaa,...) >> prep2<- prep(z, bbb,...) >> prep3<- prep(z, dfdsfg,..) >> Prep4<- prep(z, fghh,...) >> >> How can I use loop function in R to that? >> >> Thanks >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Lida, I expect that there is a better way to solve your problem than the process you propose. However, something like this may do what you want. ### ## met <- read.csv("your_met_file?) ## Since I do not have your file a made a small 5*1 character vector. met <- c("glycine_imp", "Nacetylglycine_imp", "sarcosine_imp", "dimethylglycine_imp", "betaine_imp") for (i in seq_along(met)) { my_formula <- paste0(met[i], "~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") prep <- Scores(Z=metalofGT, formula = my_formula) save(prep, file = paste0("prep", i)) } ### Mark R. Mark Sharp, Ph.D. Director of Primate Records Database Southwest National Primate Research Center Texas Biomedical Research Institute P.O. Box 760549 San Antonio, TX 78245-0549 Telephone: (210)258-9476 e-mail: msharp at TxBiomed.org> On Jul 2, 2015, at 11:48 AM, Lida Zeighami <lid.zigh at gmail.com> wrote: > > Thank you so much for replying me! > for better understanding my problem, I explain my problem more: > > I have a 682*1 matrix called "met" , the first 5 rows similar below: > >> rownames(met)[1:5] > > [1] "glycine_imp" > [2] "Nacetylglycine_imp" > [3] "sarcosine_imp" > [4] "dimethylglycine_imp" > [5] "betaine_imp" > > and I have a function in R that each time use one of the row names of "met" > matrix and create a new object file and I should save the objects! > > my function is " > Scores(Z=metalofGT,formula="met[i]~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") > " that each time just I should change the met[i] and replace by row names > "met" one by one and for each of them I should rename the function and > after that I should save each object! > for example for first row of "met" I have > >> prep1<- Scores(Z=metalofGT,formula="glycine_imp~egfr_v1_ckdepi+pc1+pc2+pc3+V1AGE01+GENDER") > #creat the object file for first row and called prep1### > >> save(prep1, file="prep1.RData", compress="bzip2") ##save the > object file as "prep1.RData"##### > > I should do this process for 682 row names of "met" matrix and at the end I > should have "prep1.RData" , "prep2.RData" , "prep3.RData" > > so, would you please help me how to do it? > > Many Thanks, > Ati > > On Wed, Jul 1, 2015 at 1:07 PM, Lida Zeighami <lid.zigh at gmail.com> wrote: > >> I have 682 variables in a data frame , and a function that I should feed >> 682 variables in this function one by one and each time save the file as a >> special name! >> for emaple: >> my data frame file includes 682 names : >> 1 aaa >> 2 bbb >> 3 dfdsfg >> 4 fghh >> . >> >> 682 fgfhg >> and a function like prep(Z, aaa, .....) and each time I should change the >> variable name in this function and read the variable from the data frame >> and each time I should save the file as a special name such as: >> >> prep1<- prep(z, aaa,...) >> prep2<- prep(z, bbb,...) >> prep3<- prep(z, dfdsfg,..) >> Prep4<- prep(z, fghh,...) >> >> How can I use loop function in R to that? >> >> Thanks >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.