Hello, I would like to replace a for loop with lapply in order to speed up the treatment of my data (I've read lapply can help to do it). At the end of the message, you will find a simple example (function is more complex and data frames is more than 1 million of rows) of what I would like to do, that is applying the same calculation to all the rows of the data frame (knowing that I cannot change the structure of the function). I hope you understand my french explainations ... Thanks in advance for your help, Have a nice week-end, Ptit Bleu. df1<-data.frame(c(1,2,3,4), c(9,6,5,8), c(5,4,8,6)) names(df1)<-c("C1", "C2", "C3") fcttest<-function(a1,a2,a3) { v<-a1*a2 w<-a2+a3 return(c(v,w)) } # The following for loop works result<-data.frame() for (i in 1:length(df1[,1])) { result<-rbind(result,fcttest(df1[i,1],df1[i,2],df1[i,3])) } # But the command lapply(df1, fcttest) # gives (in french, sorry) # Erreur dans FUN(X[[1L]], ...) : l'?l?ment 2 est vide ; # la partie de la liste d'arguments de '*' en cours d'?valuation ?tait : # (a1, a2) -- View this message in context: http://r.789695.n4.nabble.com/Still-confused-with-lapply-tp3050414p3050414.html Sent from the R help mailing list archive at Nabble.com.
PtitBleu <ptit_bleu <at> yahoo.fr> writes:> > > Hello, > > I would like to replace a for loop with lapply in order to speed up the > treatment of my data (I've read lapply can help to do it). > > At the end of the message, you will find a simple example (function is more > complex and data frames is more than 1 million of rows) of what I would like > to do, that is applying the same calculation to all the rows of the data > frame (knowing that I cannot change the structure of the function). > > I hope you understand my french explainations ... > Thanks in advance for your help, > Have a nice week-end, > Ptit Bleu. > > df1<-data.frame(c(1,2,3,4), c(9,6,5,8), c(5,4,8,6)) > names(df1)<-c("C1", "C2", "C3") > > fcttest<-function(a1,a2,a3) { > v<-a1*a2 > w<-a2+a3 > return(c(v,w)) > } > > # The following for loop works > result<-data.frame() > for (i in 1:length(df1[,1])) { > result<-rbind(result,fcttest(df1[i,1],df1[i,2],df1[i,3]))why bother with lapply when you can just do this with(df1, cbind(df1[[1]] * df1[[2]], df1[[2]] + df1[[3]])) HTH Ken -- Ken Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen L?pine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html
Thanks for your answer but do you have a solution using the function ? I need to you use the data frame with a function (not the one given). Thanks in advance again. Ptit Bleu. -- View this message in context: http://r.789695.n4.nabble.com/Still-confused-with-lapply-tp3050414p3050455.html Sent from the R help mailing list archive at Nabble.com.
Hi r-help-bounces at r-project.org napsal dne 19.11.2010 15:41:08:> > Hello, > > I would like to replace a for loop with lapply in order to speed up the > treatment of my data (I've read lapply can help to do it).Not exactly. For loop if correctly written can be as quick as lapply. See P.Burns' R inferno.> > At the end of the message, you will find a simple example (function ismore> complex and data frames is more than 1 million of rows) of what I wouldlike> to do, that is applying the same calculation to all the rows of the data > frame (knowing that I cannot change the structure of the function).Why? You do not have access to the code of a function?> > I hope you understand my french explainations ... > Thanks in advance for your help, > Have a nice week-end, > Ptit Bleu. > > > df1<-data.frame(c(1,2,3,4), c(9,6,5,8), c(5,4,8,6)) > names(df1)<-c("C1", "C2", "C3") > > fcttest<-function(a1,a2,a3) { > v<-a1*a2 > w<-a2+a3 > return(c(v,w)) > } > > # The following for loop works > result<-data.frame() > for (i in 1:length(df1[,1])) { > result<-rbind(result,fcttest(df1[i,1],df1[i,2],df1[i,3])) > }Let assume you want to use some function for first two columns and some for the columns 2 and 3. Than why not just do it?> matrix(fcttest(df1[,1],df1[,2],df1[,3]),ncol=2)[,1] [,2] [1,] 9 14 [2,] 12 10 [3,] 15 13 [4,] 32 14 Regards Petr> > # But the command > lapply(df1, fcttest) > # gives (in french, sorry) > # Erreur dans FUN(X[[1L]], ...) : l'?l?ment 2 est vide ; > # la partie de la liste d'arguments de '*' en cours d'?valuation ?tait : > # (a1, a2) > > > > -- > View this message in context:http://r.789695.n4.nabble.com/Still-confused-> with-lapply-tp3050414p3050414.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.