【问题标题】:Convert hourly time series data frames to multiple data frames of single hours将每小时时间序列数据帧转换为多个单小时数据帧
【发布时间】:2021-05-13 22:37:11
【问题描述】:

我有一个数据框列表,其中包含一年内每小时的气象站数据。我想将它们拆分为数据帧,其中每个数据帧都包含来自所有气象站的同一小时时间点的数据。所以一年,这将是 24(小时)* 365(天)= 8760 个数据帧。通过这种方式,我可以使用来自所有气象站的数据创建特定小时的绘图。

这是我的方法,需要很长时间来计算。我有大约 300 个气象站一年的每小时数据,所以数据量很大。

list_of_station_df:时间序列站数据帧列表(每小时)

list_of_all_hourly_station_df:数据框列表,其中每个 df 包含该小时所有气象站的数据(在每次迭代中不断添加),其中包含最终结果

# Start by adding the hourly df's for the first station
list_of_all_hourly_station_df = split(list_of_station_df[[1]], list_of_station_df[[1]]$time)

for(station_df in list_of_station_df[-1]) {

  # Get a list of hourly df's for this station (each df will have one row)
  list_of_hourly_station_df <- split(station_df, station_df$time)

  # Merge our list with all the previous hourly data with the above
  list_of_all_hourly_station_df <- mapply(rbind, list_of_all_hourly_station_df, 
  list_of_hourly_station_df, SIMPLIFY=FALSE)

 }

这在我只迭代几个站点时有效,即for(station_df in list_of_station_df[2:5])

但是当我尝试为所有站点运行它时,它需要很长时间。希望有人能够缩短上述计算时间。谢谢

【问题讨论】:

  • 我相信将您的所有数据帧转换为单个数据帧并过滤/子集时间将使整个过程变得更加容易。

标签: r dataframe merge time-series


【解决方案1】:

通常最好将所有数据保存在一个数据帧中。

combine_df <- do.call(rbind, list_of_station_df)

您应该能够使用 combine_df 本身完成大部分事情,甚至可以获得每小时数据帧。

list_of_all_hourly_station_df <- split(combine_df, format(combine_df$time, '%Y-%m-%d %H'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    相关资源
    最近更新 更多