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.z...@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.z...@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@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. ______________________________________________ R-help@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.