How to convert string (in Portuguese) into date?

4

I have data.frame with a column (month) that represents the month and year of each remark that is in the form of a string. I would like to modify it to date so I can use the proc in excel with date. How can I do it?

          mes                   Sub_item Brasil Brasilia.DF Goiania.GO Campo_Grande.MS Salvador.BA Fortaleza.CE
1  junho 2017    3103.Cama, mesa e banho  -0.73       -0.50      -0.88           -3.43       -3.37        -1.54
2   maio 2017    3103.Cama, mesa e banho  -0.42       -1.75       0.90           -3.51        1.88        -1.71
3  junho 2017       4101.Roupa masculina   0.39        0.73      -1.74            0.54        1.87        -2.00
4   maio 2017       4101.Roupa masculina   0.98       -0.57      -0.61            1.02        1.72         1.06
5  junho 2017        4102.Roupa feminina   0.21       -0.15      -0.25            1.42       -0.12         1.01
6   maio 2017        4102.Roupa feminina   1.10        0.69       0.43           -1.23        0.56         0.59
7  junho 2017        4103.Roupa infantil   0.51        1.01       0.49           -1.14        0.72        -0.28
8   maio 2017        4103.Roupa infantil   1.33        0.08       0.33            2.72        0.59        -0.41
9  junho 2017 4201.Calcados e acessorios   0.05        0.48      -0.69            0.12        1.14         0.19
10  maio 2017 4201.Calcados e acessorios   0.85        0.16       0.13            0.53        1.25         1.07
11 junho 2017    4301.Joias e bijuterias  -0.28       -1.42       0.27            0.96        0.40        -0.82
12  maio 2017    4301.Joias e bijuterias   0.49        1.67      -0.66           -0.74       -1.78        -2.26
13 junho 2017   4401.Tecidos e armarinho   0.36       -0.46       2.50           -0.68       -0.76         2.61
14  maio 2017   4401.Tecidos e armarinho   0.30       -0.93       1.06            0.29        1.11         0.39

I tried to use some functions like str_replace , as.Date . I thought about creating a function that would change each string "month year" to a date format (unfortunately I could not play). Do you have any ideas? Thanks!

    
asked by anonymous 26.07.2017 / 16:31

2 answers

7

I created the following database to use as an example

dados <- data.frame(
  mes = c("junho 2017", "maio 2017", "junho 2017",
          "maio 2017", "junho 2017", "maio 2017",
          "junho 2017", "maio 2017", "junho 2017",
          "maio 2017", "junho 2017", "maio 2017"),
  x = rnorm(12)
)
dados$mes <- as.character(dados$mes)

Then with the package stringr I have separated the mes variable into month and year, and the zoo package to transform these two information into a single one in date format

library(dplyr)
library(stringr)
library(zoo)
dados %>% 
  mutate(Data = as.yearmon(paste(word(mes), word(mes, -4))))

In fact, it is not even necessary to separate the variable mes to use the as.yearmon function. The code is then

library(dplyr)
library(zoo)
dados %>% 
  mutate(Data = as.yearmon(mes))
    
26.07.2017 / 18:10
2

Hello,

I thought of a not very elegant alternative, but it works.

vari <- c("junho 2009", "maio 2090", "outubro 1096", "junho 2509", "maio 2340", "abril 1342")
vari <- gsub("abril ", "04/", vari)
vari <- gsub("maio ", "05/", vari)
vari <- gsub("junho ", "06/", vari)
vari <- gsub("outubro ", "10/", vari)

use the gsub () function, it replaces a word or number you want with something else you want.

If I understood, you wanted to turn it into a numerical date.

    
28.07.2017 / 19:22