Question:
Good morning!
I have a quarterly data frame that is divided by 1st, 2nd and 3rd month of each quarter, see:
Trimestre Variável Referência temporal
1º trimestre 2007 Animais abatidos No 1º mês
1º trimestre 2007 Animais abatidos No 1º mês
1º trimestre 2007 Animais abatidos No 1º mês
1º trimestre 2007 Animais abatidos No 1º mês
1º trimestre 2007 Animais abatidos No 1º mês
1º trimestre 2007 Animais abatidos No 1º mês
1º trimestre 2007 Animais abatidos No 2º mês
1º trimestre 2007 Animais abatidos No 2º mês
2º trimestre 2007 Animais abatidos No 1º mês
2º trimestre 2007 Animais abatidos No 1º mês
2º trimestre 2007 Animais abatidos No 1º mês
2º trimestre 2007 Animais abatidos No 1º mês
2º trimestre 2007 Animais abatidos No 2º mês
2º trimestre 2007 Animais abatidos No 2º mês
I want to rename in the Time Reference column by months of the year according to the quarter, so I want my data frame to look like this:
Trimestre Variável Referência temporal
1º trimestre 2007 Animais abatidos Janeiro
1º trimestre 2007 Animais abatidos Janeiro
1º trimestre 2007 Animais abatidos Janeiro
1º trimestre 2007 Animais abatidos Janeiro
1º trimestre 2007 Animais abatidos Janeiro
1º trimestre 2007 Animais abatidos Janeiro
1º trimestre 2007 Animais abatidos Fevereiro
1º trimestre 2007 Animais abatidos Fevereiro
2º trimestre 2007 Animais abatidos Abril
2º trimestre 2007 Animais abatidos Abril
2º trimestre 2007 Animais abatidos Abril
2º trimestre 2007 Animais abatidos Abril
2º trimestre 2007 Animais abatidos Maio
2º trimestre 2007 Animais abatidos Maio
Note that the 1st month of the 1st quarter is January, the 2nd month of the 1st quarter is February, and so on…
Is there any way to rename these strings directly? Well, my data frame is quite big and it would be impossible to rename it line by line.
dput result(head(table1092):
> dput(head(tabela1092))
structure(list(Trimestre = c("1º trimestre 2007", "1º trimestre 2007",
"1º trimestre 2007", "1º trimestre 2007", "1º trimestre 2007",
"1º trimestre 2007"), Variável = c("Animais abatidos", "Animais abatidos",
"Animais abatidos", "Animais abatidos", "Animais abatidos", "Animais abatidos"
), `Referência temporal (Código)` = c("115233", "115233", "115233",
"115233", "115233", "115233"), `Referência temporal` = c("No 1º mês",
"No 1º mês", "No 1º mês", "No 1º mês", "No 1º mês", "No 1º mês"
), `Tipo de rebanho bovino` = c("Bois", "Bois", "Bois", "Bois",
"Bois", "Bois"), `Tipo de inspeção` = c("Total", "Total", "Total",
"Total", "Total", "Total"), `Unidade da Federação` = c("Rondônia",
"Acre", "Amazonas", "Roraima", "Pará", "Amapá"), `Unidade de Medida` = c("Cabeças",
"Cabeças", "Cabeças", "Cabeças", "Cabeças", "Cabeças"), Valor = c(83979,
17709, 5982, NA, 111030, NA)), row.names = 2:7, class = "data.frame")
Answer:
One of the ways you can do this is to use the ifelse
function together with the logical operator &
and the subset of strings starting from substr
(so that it doesn't depend on the year):
dados$referencia.temporal <- ifelse(substr(dados$Trimestre,1,1)=="1" & substr(dados$`Referência temporal`,4,4)=="1", "Janeiro",
ifelse(substr(dados$Trimestre,1,1)=="1" & substr(dados$`Referência temporal`,4,4)=="2", "Fevereiro",
ifelse(substr(dados$Trimestre,1,1)=="1" & substr(dados$`Referência temporal`,4,4)=='3', "Março",
ifelse(substr(dados$Trimestre,1,1)=="2" & substr(dados$`Referência temporal`,4,4)=="1", "Abril",
ifelse(substr(dados$Trimestre,1,1)=="2" & substr(dados$`Referência temporal`,4,4)=="2", "Maio",
ifelse(substr(dados$Trimestre,1,1)=="2" & substr(dados$`Referência temporal`,4,4)=='3', "Junho",
ifelse(substr(dados$Trimestre,1,1)=="3" & substr(dados$`Referência temporal`,4,4)=="1", "Julho",
ifelse(substr(dados$Trimestre,1,1)=="3" & substr(dados$`Referência temporal`,4,4)=="2", "Agosto",
ifelse(substr(dados$Trimestre,1,1)=="3" & substr(dados$`Referência temporal`,4,4)=='3', "Setembro",
ifelse(substr(dados$Trimestre,1,1)=="4" & substr(dados$`Referência temporal`,4,4)=="1", "Outubro",
ifelse(substr(dados$Trimestre,1,1)=="4" & substr(dados$`Referência temporal`,4,4)=="2", "Novembro",
ifelse(substr(dados$Trimestre,1,1)=="4" & substr(dados$`Referência temporal`,4,4)=='3', "Dezembro","Erro"))))))))))))
With that, I can combine the value of the two columns. Another way to do this would be through a left join
from the merge
function for example.