<html><body><div style="color:#000; background-color:#fff; font-family:arial, helvetica, sans-serif;font-size:10pt"><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; ">Caros listeiros,</div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; "><br></div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; ">estou com uma base de dados para a qual preciso calcular médias móveis para dois atributos com relação a certos níveis de fatores. Para isso, pensei em utilizar uma combinação de split+lapply, mas mesmo com apenas pouco mais de 126.000 observações o split do banco de dados leva uma eternidade (um dos fatores possui 1242 níveis). Posteriormente tentei utilizar a função ddply (pacote plyr), mas retorna um erro.</div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; "><br></div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; ">Alguma
sugestão?</div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; "><br></div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; ">Segue um exemplo abaixo (base com menos observações que a original):</div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; "><br></div><div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">P<-2000:2010</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">Rep<-letters[1:5]</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">TF<-1:2</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">CC<-paste("H-",101:299,sep="")</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier,
monaco, monospace, sans-serif">a<-expand.grid(P=P,R=Rep,TF=TF,CC=CC)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">a$y<-rnorm(nrow(a))</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">a$z<-runif(nrow(a))</font></div><div><font class="Apple-style-span" face="'Courier New', courier, monaco, monospace, sans-serif" size="2"><br></font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">library(zoo)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">ma.fun<-function(x,k){</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"> mm.y<-rollmean(x$y,k)</font></div><div><font class="Apple-style-span" size="2"
face="'Courier New', courier, monaco, monospace, sans-serif"> mm.z<-rollmean(x$z,k)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"> cbind(x[-c(1:2),-c(5:6)],ma.y=c(mm.y),ma.z=c(mm.z))</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">}</font></div><div><font class="Apple-style-span" face="'Courier New', courier, monaco, monospace, sans-serif" size="2"><br></font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># Abordagem 1: Nem chega a completar o split</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">a.1<-split(a,list(a$R,a$TF,a$CC),drop=T)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace,
sans-serif">a.2<-lapply(a.1,ma.fun,k=3)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"><br></font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># Abodagem 2: Mensagem de erro: </font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># Erro em do.ply(i) : </font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># task 1 failed - "adicionando classe "factor" a um objeto inválido"</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># Além disso: Mensagens de aviso perdidas:</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># 1:
<anonymous>: ... may be used in an incorrect context: ‘.fun(piece, ...)’</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># </font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"># 2: <anonymous>: ... may be used in an incorrect context: ‘.fun(piece, ...)’</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"><br></font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">library(plyr)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">library(doSNOW)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">library(foreach)</font></div><div><font
class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif"><br></font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">ncpu<-4</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">cl<-makeSOCKcluster(rep("localhost",ncpu))</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">registerDoSNOW(cl)</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">clusterEvalQ(cl,c(library(plyr),library(zoo)))</font></div><div><font class="Apple-style-span" size="2" face="'Courier New', courier, monaco, monospace, sans-serif">a.2<-ddply(a,c('R','CC','TF'),ma.fun,k=3,.parallel=T)</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif"
size="2"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif" size="2"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif" size="2">Grato,</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif" size="2">Rubem</font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif" size="2"><br></font></div><div><font class="Apple-style-span" face="arial, helvetica, sans-serif" size="2"><br></font></div></div></div></body></html>