在新列中使用列表标识的数据帧的大列表

发布于 2025-01-30 03:10:43 字数 1040 浏览 1 评论 0原文

使用IETD软件包中的示例数据“ hourly_time_series”, 我使用IETD软件包创建了一个大列表(每个子列表都是不同尺寸的数据范围)。每个列表代表一个降雨事件。

library(IETD)
HU <- drawre(Time_series=hourly_time_series,IETD=5,Thres=0.5)
RE <- HU$Rainfall_Events

我需要将RE转换为带有额外列的数据框架,其中包含降雨事件的数量(作为ID),以轻松将它们分开。 我尝试使用bind_rows()和rbindlist()函数尝试,但这些函数不保留降雨ID。 我该怎么做?所需的输出为:

事件日期深度
12000-01-06 04:00:001.0
12000-01-06 05:00:000.0 0.0
12000-01-06-06 06:00:001.5
1 1.5 12000-- 01-06 07:00:001.5
22000-01-06 20:00:000.5
22000-01-06 21:00:000.5 0.5
。 。
。 。
。 。

希望你能帮忙

Using the example data "hourly_time_series" from IETD package,
I created a large list (each sublist are dataframes of different sizes) with IETD package. Each list represents a rainfall event.

library(IETD)
HU <- drawre(Time_series=hourly_time_series,IETD=5,Thres=0.5)
RE <- HU$Rainfall_Events

I need to convert RE to a dataframe with an additional column with the number of the rainfall event (as an ID) to separate them easily.
I tried with bind_rows() and rbindlist() functions but these don´t preserve the rainfall ID.
How Could I do this? The desired output as an example is:

eventDatedepth
12000-01-06 04:00:001.0
12000-01-06 05:00:000.0
12000-01-06 06:00:001.5
12000-01-06 07:00:001.5
22000-01-06 20:00:000.5
22000-01-06 21:00:000.5
.. ..
.. ..
.. ..

Hope you could help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久隐师 2025-02-06 03:10:43

我将定义一个用于演示的示例数据集,一个名为ll的列表,其中五个元素是一个带有两个字段的数据框,date and depth 。每个列表元素都有一个唯一的名称,例如event1event2等。

# Example data
ll <- list()
for(i in 1:5){
  df <- data.frame(
    date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 100),
    depth = runif(100, 1, 5))
  ll[[paste0("event", i)]] <- df
}

您可以在每个子列表中添加一个ID字段,然后将列表折叠到单个数据框中:

# Define df ID for each list element
ll <- Map(cbind, ll, group = names(ll))

# Collapse list into single dataframe
res <- do.call(rbind, ll)

head(res)
               date    depth
event1.1 1999-04-09 2.174580
event1.2 1999-07-19 2.052316
event1.3 1999-10-23 4.890014
event1.4 1999-04-24 1.208217
event1.5 1999-10-20 1.946533
event1.6 1999-03-10 1.946779

I'll define an example dataset for demonstration, a list called ll with five elements, each of which is a dataframe with two fields, date and depth. Each list element has a unique name, eg event1, event2, etc.

# Example data
ll <- list()
for(i in 1:5){
  df <- data.frame(
    date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 100),
    depth = runif(100, 1, 5))
  ll[[paste0("event", i)]] <- df
}

You could add an ID field to each sublist and then collapse the list into a single dataframe:

# Define df ID for each list element
ll <- Map(cbind, ll, group = names(ll))

# Collapse list into single dataframe
res <- do.call(rbind, ll)

head(res)
               date    depth
event1.1 1999-04-09 2.174580
event1.2 1999-07-19 2.052316
event1.3 1999-10-23 4.890014
event1.4 1999-04-24 1.208217
event1.5 1999-10-20 1.946533
event1.6 1999-03-10 1.946779
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文