Last active
December 15, 2017 13:13
-
-
Save viniroger/126b3f57f1161dd4d1a45a086732a2c1 to your computer and use it in GitHub Desktop.
Calcular médias mensais e semanais
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(xts) | |
lugar = 'RiodeJaneiro-RJ' | |
file_in = paste('/path/ObsVar_diario_',lugar,'.csv',sep='') | |
file_out_mensal = paste('/path/ObsVar_mensal_',lugar,'.csv',sep='') | |
file_out_semanal = paste('/path/ObsVar_semanal_',lugar,'.csv',sep='') | |
write_file = function(dados,file_out){ | |
# Arredondar para inteiro | |
coredata(dados) = round(coredata(dados)) | |
# Converter zoo para data frame | |
df = data.frame(time = strftime(time(dados), "%F"),value = as.vector(dados)) | |
# Renomear colunas | |
colnames(df) = c('data','valor') | |
# Gravar em arquivo CSV | |
write.table(df, file_out, quote = FALSE, sep = ',', col.names = TRUE, row.names = FALSE) | |
} | |
# Ler arquivo | |
dados_zoo = read.zoo(file_in, sep = ",", header = TRUE) | |
# Calcular média mensal | |
dados_mensal = apply.monthly(dados_zoo, mean) | |
write_file(dados_mensal,file_out_mensal) | |
# Calcular média semanal | |
dados_semanal = apply.weekly(dados_zoo, mean) | |
write_file(dados_semanal,file_out_semanal) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment