【发布时间】:2020-12-22 03:16:05
【问题描述】:
我有一个包含 14 个时间序列跳跃试验的列表。 14 与时间和 14 与垂直距离,如列表中所示,我将如何使用 MAP 而不是 LOOP 将每个相应的时间和垂直试验配对为 hop1、hop2、hop3 等......并将它们放在主数据框?
列出 dt 和 dv
示例如下所示:
hop1 <-data.frame(hop =1,t = dt$time_100L_1, v = dv$vertical_100L_1)
hop2 <-data.frame(hop =2,t = dt$time_100L_2, v = dv$vertical_100L_2)
并将所有的啤酒花存储在一个 master 中。我可以对 2 个列表进行 rowbind 并在示例中的每个 like 上放置一个 hop ID 吗?
我正在使用此代码,
首先将提取为 data.frame/tibble 的第一个元素映射到绑定列以形成新列表
combined_hop <- purrr::map2(d100Lt, d100Lv, ~data.frame(.x, .y)) %>%
# use ldply() as it will use the list names as a column we can use as ID
plyr::ldply(tibble) %>%
# group by this newly generated ID
dplyr::group_by(.id) %>%
# build new group ID (you could build it from your column names
dplyr::mutate(G_ID = dplyr::cur_group_id()) %>%
# ungroup the tibble
dplyr::ungroup() %>%
# select renaming the columns of interest (dropping the intermediate ID)
dplyr::select(ID = G_ID, TIME = .x, Vertical = .y)
它有效,但我的列 id 乱序,而第 2 列和第 3 列是正确的。 new data frame
对如何获得正确的顺序有什么建议吗?
【问题讨论】:
标签: r dataframe mapping nested-lists purrr