[R] Using "apply" instead of "for" loop / multithreading

Charles D. charlybegood at live.fr
Mon Nov 12 18:39:55 CET 2012


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.



More information about the R-help mailing list