The following links may help navigating this page
No Feed Found

R-Project: apply versus for-loop · 2016-05-23

We run a piece of R-code that turns all variables of a dataframe into factors. It does it like this:

a<-apply(a, 2, function(x){factor(x)})

It applies (apply) a function (function(x)) column-wise (2) to dataframe a and runs just fine on a 64bit-Windows-machine with 32 GB RAM but fails miserably on a 32bit-Sparc-Box with 128 GB RAM:

Error: cannot allocate vector of size XYZ Kb

We did not find a proper solution for this issue. It is propably caused by address space limits on 32bit-Systems ... but who knows. The solution which works for us looks like this:

for(i in colnames(a)){a[,i]<-factor(a[,i])}

Any comments on this one? We are not perfectly satisfied with our solution. When should one stick with apply and when are for-loops favourable?

Commenting is closed for this article.