R 从2个列表映射变量并将其存储在单独的数据框中

R 从2个列表映射变量并将其存储在单独的数据框中,r,dataframe,mapping,nested-lists,purrr,R,Dataframe,Mapping,Nested Lists,Purrr,我有一个14次系列跳跃测试的列表。14表示时间,14表示垂直距离,如列表所示,我如何使用MAP而不是循环将每个时间和垂直试验配对为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_1

我有一个14次系列跳跃测试的列表。14表示时间,14表示垂直距离,如列表所示,我如何使用MAP而不是循环将每个时间和垂直试验配对为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)
并将所有的啤酒花储存在一个主容器中。我可以对2列表进行行绑定,并在示例中的每个like上放置一个跃点ID吗

我正在使用这个代码

首先映射提取为data.frame/tible的第一个元素以绑定列以形成新列表 它可以工作,但我得到的列id的顺序,而第二和第三列是正确的。

如何获得正确的订单,您有什么建议吗?

您可以尝试使用Map:


据我所知,您有两个列表,其中每个列表都是一个dataframe/Tible,并且都具有相同的维度。如果列的顺序相同,可能的解决方案如下:

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
我们也可以这样做

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

所以这真的很有效。它为每个跃点放置列和行,唯一奇怪的是标识符列从1跳到7-14,然后再跳回到2-6…不确定这是如何工作的。我运行了几次,每次都是这样。时间和垂直位置仍然匹配,但是对于所有14个测试,你是指.id还是G_id,或者两者都匹配?您只需省略最后一个dplyr::select line和前面的pipe操作符即可保留它们,以便进行比较您提供的代码有效,但ID列变为1,7,8,9,10,11,12,13,14,2,3,4,5,6,而时间和垂直列的顺序正确。我编辑了这篇文章,并提供了一张带有你建议的代码的图片。谢谢,我以前试过,但我需要绑定行,现在绑定列。当我尝试绑定行时,我会得到一个错误,因为每个后续的时间和垂直都有不同的试用号,所以会弹出名称错误。我可以删除名称,只使用标识符列吗?但是我需要绑定行,而不是列。把它们一个叠在另一个上面,分成三列。新数据框中的ID列、时间列和垂直列。我试过了,但是rbind有一个错误,而且试用名不一样,所以它不会将下一个试用的行绑定到上面的列,这些列的试用名在dt和dv中是不同的。@AaronSanchez Try do result
result <- Map(cbind, hop  = seq_along(dt[[1]]), t = dt[[1]], v = dv[[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
 do.call(Map, c(f = cbind, c(list(hop = seq_along(dt)), do.call(c, list(dt, dv)))))