Redeem time period in r

Asked

Viewed 191 times

2

Hello, I have a large series of temporal data. I have over 100 years to work. I need to rescue (abstract) only the summers of each of the years, and do not know how to do in R. For example, I need the time period 20/06/2015 to 20/09/2015, and the previous years as 2014,2013, which are all contained together in the same series. Can someone help me with a formula or command?

  • what you need in this period ? days, weeks, hours, ... ?

  • I am working with climate data, I need to rescue the date and the average temperature of this period.

  • maybe these two links can help you: https://biologyforfun.wordpress.com/2014/05/importing-100-years-of-climate-change-into-r/ and https://ropensci.org/blog/2013/08/18/sciordata/

  • Thanks, I’ll take a look.

1 answer

1

I didn’t see how your data was stored so I invented a database. This sequence of operations will take only the period from 20/06 to 20/09 of all the years that are at the base.

library(lubridate)
library(dplyr)

dados <- data.frame(
  datas = seq(as.Date('1900-01-01'),as.Date('2000-12-31'),by = 1),
  valor = 1:36890
)

dados %>% tbl_df %>%
  # pegar apenas os meses de junho a setembro
  filter(month(datas) <= 9, month(datas) >= 6) %>%
  # se for junho, só pegar os dias maiores que 20
  filter(!(month(datas) == 6 & day(datas) < 20)) %>%
  # se for setembro pegar os dias menores do que 20
  filter(!(month(datas) == 9 & day(datas) > 20))

Source: local data frame [9,393 x 2]

        datas valor
       (date) (int)
1  1900-06-20   171
2  1900-06-21   172
3  1900-06-22   173
4  1900-06-23   174
5  1900-06-24   175
6  1900-06-25   176
7  1900-06-26   177
8  1900-06-27   178
9  1900-06-28   179
10 1900-06-29   180
..        ...   ...
  • Hello, the data is very similar. I will test and let you know if it worked. Thank you.

  • I would like to understand this command: data %>% tbl_df %>%

  • You’re making this mistake in my programming. Error in data.frame(dates = seq(as.Date("01-01-1979"), as.Date("28-10-2014"), : Arguments imply differing number of Rows: 10137, 13080

  • Would that be columns and rows respectively? (value = 1:36890)

  • This step creates a database just p/ you test the following code. So I create a simple base with two columns: datas and valor. In this code you posted, there are two problems: the dates are written in Brazilian format, but this command only understands the American format aaaa-mm-dd. But the error is happening because your value vector is larger than the date vector you create. In my example when I put 1:36890 was pq from 1900 to 2000 passed 36890 days .

Browser other questions tagged

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