【问题标题】:Mapping variables from 2 lists and storing them in separate data frames映射来自 2 个列表的变量并将它们存储在单独的数据框中
【发布时间】: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


    【解决方案1】:

    据我了解,您有两个列表,每个列表都是一个数据框/小标题,并且都具有相同的尺寸。如果列的顺序相同,则可能的解决方案是:

    library(dplyr)
    library(purrr)
    library(plyr)
    
    # demo lists
    dt <- list(data.frame(a1 = c(1, 1),
                          a2 = c(2, 2),
                          a3 = c(3, 3)))
    
    dv <- list(data.frame(b1 = c(111, 111),
                          b2 = c(222, 222),
                          b3 = c(222, 222)))
    
    # first map the first element extracted as data.frame/tibble to bind columns to form a new list
    purrr::map2(dt[[1]], dv[[1]], ~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, VELO = .x, TIME = .y)
    
    # A tibble: 6 x 3
         ID  VELO  TIME
      <int> <dbl> <dbl>
    1     1     1   111
    2     1     1   111
    3     2     2   222
    4     2     2   222
    5     3     3   222
    6     3     3   222
    

    【讨论】:

    • 所以这非常有效。它为每个跃点放置了列和行,唯一奇怪的是标识符列从 1 跳转到 7-14 然后又回到 2-6 ......不确定它是如何工作的。我跑了几次,每次都是这样。尽管所有 14 次试验的时间和垂直位置仍然匹配
    • 您的意思是“.id”还是“G_ID”或两者兼而有之? (您可以通过省略最后的“dplyr::select()”行和之前的管道运算符来保留它们以进行比较)
    • 您提供的代码有效,但 ID 列变为 1,7,8,9,10,11,12,13,14,2,3,4,5,6 而时间和垂直列的顺序正确。我编辑了帖子并提供了一张带有您建议的代码的图片。顺便说一句,很棒。谢谢
    【解决方案2】:

    你可以试试Map

    result <- Map(cbind, hop  = seq_along(dt), t = dt, v = dv)
    

    如果 dtdv 在列表中,您可以这样做:

    result <- Map(cbind, hop  = seq_along(dt[[1]]), t = dt[[1]], v = dv[[1]])
    

    【讨论】:

    • 但我需要绑定行,而不是列。将它们一叠放在三列中。新数据框中的 ID 列、时间列和垂直列。我已经尝试过了,但我在 rbind 上遇到了错误,并且试用名称不一样,因此它不会将下一次试用的行绑定到上面的列中,使用 dt 和 dv 中的不同试用名称。
    • @AaronSanchez 试试result &lt;- do.call(rbind, result)
    【解决方案3】:

    我们也可以

     do.call(Map, c(f = cbind, c(list(hop = seq_along(dt)), do.call(c, list(dt, dv)))))
    

    【讨论】:

    • 是的,我以前试过这个,但我需要绑定行,现在是列。当我尝试绑定行时出现错误,因为每个后续时间和垂直都有不同的试用号,因此会弹出名称错误。我可以删除名称而只使用标识符列吗?
    猜你喜欢
    • 2020-03-29
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 2012-09-29
    相关资源
    最近更新 更多