How to convert string (in English) to date?

Asked

Viewed 961 times

5

I have a data.frame with a column (mes) that represents the month and year of each observation that is in string form. I wanted to modify it to date so I could use 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 of creating a function that would change each string "month year" to a date format (unfortunately I could not play). Do you guys have any ideas? Thanks!

  • To use as.Date should first choose a day for the date, usually the day 1. Thus: as.Date(paste("1", dados$mes), "%d %B %Y").

2 answers

8


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 separated the variable mes month and year, and the package zoo to transform these two pieces of information into a single data 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 function as.yearmon. The code is then

library(dplyr)
library(zoo)
dados %>% 
  mutate(Data = as.yearmon(mes))
  • Friend, two things I didn’t understand: 1. Why did you create a column with normal distribution in data frame? 2. If I use my column already "month" of my data frame, the result will be equivalent?

  • Danilo, I created a variable with normal distribution, just to create an example data.frame, so much so that I don’t even use it. And as for the second question, using the code dados %>% mutate(Data = as.yearmon(mes)) I believe you will get the expected result. Just don’t forget to load the required packages (dplyr and zoo)

3

Hello,

I thought of an alternative not very elegant, 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 function gsub(), it replaces a word or number you want with something else you want.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.