如何一起减少和融合列表中的数据框

How to reduce & melt dataframes inside a list together

更新:

我在列表中有数据框,如下所示:

v1 <- data.frame(time = c(1, 3, 5, 9, 33),
                 X = c(4, 3, 2, 3, 12),
                 SE = c(1, 2, 3, 2, 1))
v2 <- data.frame(time = c(1, 3, 5, 9, 33),
                    Y = c(12, 3, NA, 2, 4),
                 SE = c(1, 2, 1, 12, 3))
list <- list(v1, v2)

我想melt/reduce它看起来像这样:

   time variable value SE
1     1        X     4  1
2     3        X     3  2
3     5        X     2  3
4     9        X     3  2
5    33        X    12  1
6     1        Y    12  1
7     3        Y     3  2
8     5        Y    NA  1
9     9        Y     2 12
10   33        Y     4  3

到目前为止,我已尝试此代码无济于事。

data <- list %>% reduce(full_join, by = "time") 
data2 <- melt(data, id = c("time"))

谢谢!

我们可以使用 meltpatterns

library(data.table)
melt(setDT(data), measure = patterns('X|Y', 'SE'), 
   value.name = c('value', 'SE'))[, variable := c("X", "Y")[variable]][]
#     time variable value SE
# 1:    1        X     4  1
# 2:    3        X     3  2
# 3:    5        X     2  3
# 4:    9        X     3  2
# 5:   33        X    12  1
# 6:    1        Y    12  1
# 7:    3        Y     3  2
# 8:    5        Y    NA  1
# 9:    9        Y     2 12
#10:   33        Y     4  3

或使用pivot_longer

library(tidyr)
library(stringr)
library(dplyr)
data %>%
    rename_at(vars(X, Y), ~ str_c('value.', tolower(.))) %>% 
    pivot_longer(cols = -time, names_to = c('.value', 'variable'), 
        names_sep="\.", values_drop_na = TRUE) %>% 
    arrange(variable)
# A tibble: 10 x 4
#    time variable value    SE
#   <dbl> <chr>    <dbl> <dbl>
# 1     1 x            4     1
# 2     3 x            3     2
# 3     5 x            2     3
# 4     9 x            3     2
# 5    33 x           12     1
# 6     1 y           12     1
# 7     3 y            3     2
# 8     5 y           NA     1
# 9     9 y            2    12
#10    33 y            4     3