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.
Hi Mark, Thank you so much for your help. I run your code and it works great for 5*1 character vector but if I run it for 682*1 character vector it has some error like this: Error in model.frame.default(formula = formula, ...) : variable lengths differ (found for 'egfr_v1_ckdepi') Would you please let me know what's the reason is? Many Thanks, Lida On Fri, Jul 3, 2015 at 11:38 PM, Mark Sharp <msharp at txbiomed.org> wrote:> 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. > > > > > > > >[[alternative HTML version deleted]]
Lida, Please send the code that you ran. It is almost certain that you have a vector being formed that is the wrong length. There should be only one vector that you use to increment through the loop and that is the character vector formed when you read in your_met_file to form the met character vector. 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 6, 2015, at 9:05 AM, Lida Zeighami <lid.zigh at gmail.com> wrote: > > Hi Mark, > > Thank you so much for your help. > > I run your code and it works great for 5*1 character vector but if I run it for 682*1 character vector it has some error like this: > > Error in model.frame.default(formula = formula, ...) : > variable lengths differ (found for 'egfr_v1_ckdepi') > > Would you please let me know what's the reason is? > > Many Thanks, > Lida > > On Fri, Jul 3, 2015 at 11:38 PM, Mark Sharp <msharp at txbiomed.org> wrote: > 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. > > > > > > > >