【问题标题】:How transform an irregular table with false days for a regular time series?如何为规则时间序列转换具有假天数的不规则表?
【发布时间】:2020-04-23 11:10:34
【问题描述】:

我有很多表格,每天都有气候观测。如您所见,月份的所有列都有 31 天。我想正确地组织这些数据,排除所有错误的日子并按常规时间序列进行转换。

数据视图:

输入(头(数据,31)):

structure(list(NA. = 1:31, JAN = c(NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), FEV = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), MAR = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 12.5, 0), ABR = c(0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 1.9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, NA), MAI = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 2.5, 0, 0, 0, 0, 0, 0, 1.8, 0, 0, 0), JUN = c(0, 
0, 0, 0, 0, 3.4, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 
0, 6.8, 2.4, 2.1, 0, 0, 0, 0, 0, NA), JUL = c(0, 0, 4.4, 0, 0, 
15.4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.2, 0, 1.3, 0, 0, 1.7, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), AGO = c(0, 0, 0, 0, 1.9, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.1, 4, 2.6, 0, 0, 
0, 0, 0), SET = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NA), OUT = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0), NOV = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17.7, 0, 0, 0, 0, 0, 0, 
0, NA), DEZ = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 
31L), class = "data.frame")

我想喜欢这个例子:

原始数据:https://www.dropbox.com/s/0xizr6fhbriieds/data.csv?dl=0

请任何人帮助我。是为了我的科学启蒙。

【问题讨论】:

  • 是从2000年开始的吗?
  • 我尝试按照您的要求放置数据。但我不知道 dput() 是如何工作的。
  • 不完全是。今年我举个例子。我的数据集有很多不同的年份。但是,如果我可以在示例中使用此集合,我实际上将能够与其他集合一起工作。
  • 完成!我认为现在是正确的。

标签: r datatable time-series


【解决方案1】:

从您的问题中并不清楚您要如何过滤数据,但要转换为长格式并删除 NAs,一种方法是使用 dplyrtidyrlubridate

我正在使用data.table 加载您的数据,因为由于某种原因,即使文件扩展名是.csv,它也是用分号分隔的。

您可能不重命名月份,因为您当地可能接受西班牙语月份的缩写。

libary(data.table)
data <- fread("data.csv")
names(data) <- c("Day",month.abb)

library(dplyr)
library(tidyr)
library(lubridate)
Year <- 2000
data %>% 
  pivot_longer(-Day,names_to = "Month") %>%
  mutate(Date = dmy(paste(Day,Month,Year))) %>%
  filter(!is.na(value)) %>%
  select(Date,value)
#   Date       value
#   <date>     <dbl>
# 1 2000-07-03   4.4
# 2 2000-08-05   1.9
# 3 2000-06-06   3.4
# 4 2000-07-06  15.4
# 5 2000-04-12   1.9
# 6 2000-06-15   7  
# 7 2000-07-16   2.2
# 8 2000-07-18   1.3
# 9 2000-05-21   2.5
#10 2000-07-21   1.7

【讨论】:

  • 嘿,兄弟。你再帮我吗?我怎样才能使年份变化?在此代码中,年份是恒定的。我可以有一个时间序列,年份从 2000 年开始,明年有所不同吗?例如:2000、2001、2002、2003...
  • 你怎么知道数据来自哪一年?您问题中的起始数据实际上没有年份。我只是选择了 2000,因为这就是您想要的输出。
猜你喜欢
  • 2011-04-23
  • 2014-09-02
  • 2020-09-21
  • 1970-01-01
  • 2011-11-17
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 2011-06-29
相关资源
最近更新 更多