How to filter data from R lines?

Asked

Viewed 304 times

0

I want to use Selic’s data for an R simulation, but not the initial data, only after a certain date. The data are organized as below:

selic <- structure(list(var_1 = c("jun", "jul", "ago", "set", "out", "nov", 
"dez", "jan", "fev", "mar", "abr", "mai", "jun"), var_2 = c(1986, 
1986, 1986, 1986, 1986, 1986, 1986, 1987, 1987, 1987, 1987, 1987, 
1987), var_3 = c(NA, NA, NA, NA, NA, NA, NA, "11.00", "19.61", 
"11.95", "15.30", "24.63", "18.02"), var_4 = c("1.27", "1.95", 
"2.57", "2.94", "1.96", "2.37", "5.47", NA, NA, NA, NA, NA, NA
)), row.names = c(NA, -13L), class = c("tbl_df", "tbl", "data.frame"
))

I want you to show up from July 94 until December 2019.

How can I proceed?

  • Filter from a given year or from a given year and month? Provide more details on what you need, Diego.

  • Good evening. I want you to come from July 94 until December 2019.

2 answers

1

This answer uses the package dplyr to filter lines by date. But dates must be class objects "Date" and for this purpose a temporary column is created first, temp_date.

library(dplyr)

inicio <- as.Date("1994-07-01")
fim <- as.Date("2019-12-31")

selic %>%
  mutate(temp_date = paste(1, var_1, var_2),
         temp_date = as.Date(temp_date, "%d %b %Y")) %>%
  filter(temp_date >= inicio, temp_date <= fim) %>%
  select(-temp_date)
  • Thanks, buddy! I’ll test it this way!

0

Solution tidyverse + lubridate:

library(tidyverse)
library(lubridate)

selic %>% 
  mutate(var_0 = '01') %>% 
  unite(data = ., var_0, var_1, var_2, col = 'new', sep = '-', remove = FALSE) %>% 
  mutate(new_date = dmy(new)) %>% 
  filter(new_date >= '1994-07-01' & new_date <= '2019-12-31')

Browser other questions tagged

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