Hello , I'm new to R and don't really understand how to use the function "apply" instead of a "for loop", particularly for a function with multiple entries. I have a big data file and would like to apply a function in multi thread to accelerate the processus. I have a data frame containing values of* CO2 in ppm (resp[i,6])* that I want to convert in umol of CO2 emitted by stem volume biomass (CO2v) and stem area (CO2s). (tree respiration) The loop that I have is calculating the CO2 fluxes for each row. *Script :* for (i in 1:nrow(resp)) { k=resp[i,5] CO2umol[i]<-resp[i,6]*((Press*infoch[k,11]*1e-6)/(R*(resp[i,7]+273.15))) CO2v[i]<-CO2umol[i]/(infoch[k,10]) CO2s[i]<-CO2umol[i]/(infoch[k,9]) } For that, I have two data frames : - *infoch :* variables used to calculate CO2 fluxes (16 rows with the characteristics of 16 analysis chambers) <http://r.789695.n4.nabble.com/file/n4649326/infoch.jpg> - *resp: *time series containing CO2 values and temperatures by chamber analysed (more than 500.000 rows) <http://r.789695.n4.nabble.com/file/n4649326/resp.jpg> other variables : Press=93000 R=8.31 The loop is working good, but taking quite a long time to process, if someone could explain me how to use the apply function in this case, it would be really helpful. Thanks Charles -- View this message in context: http://r.789695.n4.nabble.com/Using-apply-instead-of-for-loop-multithreading-tp4649326.html Sent from the R help mailing list archive at Nabble.com.
jim holtman
2012-Nov-12 18:56 UTC
[R] Using "apply" instead of "for" loop / multithreading
Since you did not provide sample data (use dput instead of posting links to 'jpg' files), you can probably do it without loops: k=resp[,5] CO2umol <- resp[,6]*((Press*infoch[k,11]*1e-6)/(R*(resp[,7]+273.15))) CO2v <- CO2umol/(infoch[k,10]) CO2s <- CO2umol/(infoch[k,9] On Mon, Nov 12, 2012 at 12:39 PM, Charles D. <charlybegood at live.fr> wrote:> Hello , > > I'm new to R and don't really understand how to use the function "apply" > instead of a "for loop", particularly for a function with multiple entries. > I have a big data file and would like to apply a function in multi thread to > accelerate the processus. > > I have a data frame containing values of* CO2 in ppm (resp[i,6])* that I > want to convert in umol of CO2 emitted by stem volume biomass (CO2v) and > stem area (CO2s). (tree respiration) > > The loop that I have is calculating the CO2 fluxes for each row. > > *Script :* > for (i in 1:nrow(resp)) { > k=resp[i,5] > CO2umol[i]<-resp[i,6]*((Press*infoch[k,11]*1e-6)/(R*(resp[i,7]+273.15))) > CO2v[i]<-CO2umol[i]/(infoch[k,10]) > CO2s[i]<-CO2umol[i]/(infoch[k,9]) > } > > For that, I have two data frames : > - *infoch :* variables used to calculate CO2 fluxes (16 rows with the > characteristics of 16 analysis chambers) > > <http://r.789695.n4.nabble.com/file/n4649326/infoch.jpg> > > - *resp: *time series containing CO2 values and temperatures by chamber > analysed (more than 500.000 rows) > > <http://r.789695.n4.nabble.com/file/n4649326/resp.jpg> > > other variables : > Press=93000 > R=8.31 > > The loop is working good, but taking quite a long time to process, if > someone could explain me how to use the apply function in this case, it > would be really helpful. > > Thanks > Charles > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Using-apply-instead-of-for-loop-multithreading-tp4649326.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 guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- 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.
it works really faster ! thank you -- View this message in context: http://r.789695.n4.nabble.com/Using-apply-instead-of-for-loop-multithreading-tp4649326p4649346.html Sent from the R help mailing list archive at Nabble.com.