<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><span>Pessoal,</span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span><br></span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span>Estou fazendo um loop para variáveis (tair, tsoil, biomass, lai) e dentro desse loop estou fazendo outro com anos (2005, 2006, 2007) etc.</span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span><br></span></div><div style="font-family: 'times new roman',
'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span>Ao final dos dois loops, gostaria de ter um data.frame assim:</span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span><br></span></div><div style="background-color: transparent;">> d</div><div style="background-color: transparent;"> var date value</div><div style="background-color: transparent;">1 tair 2005-01-01 10</div><div style="background-color: transparent;">2 tair 2006-01-01 20</div><div style="background-color: transparent;">3 tair 2007-01-01 30</div><div style="background-color: transparent;"><span></span></div><div style="background-color: transparent;">4 tair 2008-01-01 40</div><div style="background-color:
transparent;"><span style="background-color: transparent;">5 tsoil 2005-01-01 10</span><br></div><div style="background-color: transparent;">6 tsoil 2006-01-01 20</div><div style="background-color: transparent;">7 tsoil 2007-01-01 30</div><div style="background-color: transparent;">8 tsoil 2008-01-01 40</div><div style="background-color: transparent;">etc</div><div style="background-color: transparent;">etc</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span><br></span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span>Qual é o meio mais eficiente para fazer isso?</span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0);
background-color: transparent; font-style: normal;"><span><br></span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;">Talvez até exista uma forma mais rápida de fazer isso do que usar loops, então qualquer sugestão será muito bem-vinda.</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;"><span><br></span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;">Segue o CMR (que não está eficiente, é só para fins de compreensão do problema):</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style:
normal;"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 16px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal;">-------------------------------------------------------------------------------------------------------------</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;">library(raster)<br></div><div><div><br></div><div># Create random rasterstack and set names</div><div>r1 <- r2 <- r3 <- r4 <- raster(nrow = 50, ncol = 100)</div><div>r1[] <- 10</div><div>r2[] <- 20</div><div>r3[] <- 30</div><div>r4[] <- 40</div><div>s <- stack(r1, r2, r3, r4)</div><div><br></div><div># Set years</div><div>names(s) <- c('X2005.01.01', 'X2006.01.01', 'X2007.01.01', 'X2008.01.01')</div><div><br></div><div># Create random variable
names</div><div>vars <- c('tempsoi', 'tempair', 'biomass', 'lai')</div><div><br></div><div>for( i in 1:length (vars) ) {</div><div> </div><div> # Loop over years (names), extract raster average values and put in the data.frame</div><div> d <- NULL</div><div> </div><div> for ( j in 1:length (names(s)) ) {</div><div> var <- vars[i]</div><div> date <- format (as.Date (gsub (pattern='\\.', replacement='-', substring (names(s)[j],2))))</div><div> value <- mean (getValues (s[[j]]), na.rm = TRUE)</div><div> d <- rbind (d, data.frame(var, date, value))</div><div> } # end loop over names</div><div> </div><div>} # end
loop vars</div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><span style="background-color: transparent; font-size: 12pt;"><br></span></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><span style="background-color: transparent; font-size: 12pt;">-------------------------------------------------------------------------------------------------------------</span><br></div></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><div></div></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;"><br></div><div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt;">Saudações,<br>--<br>Thiago V. dos Santos<br>PhD student<br>Land and Atmospheric Science<br>University of
Minnesota<br>http://www.laas.umn.edu/CurrentStudents/MeettheStudents/ThiagodosSantos/index.htm<br>Phone: (612) 323 9898</div></div></body></html>