【问题标题】:Convert list in data frame collapsing one column and keeping others unaletered in R转换数据框中的列表折叠一列并在 R 中保持其他列不变
【发布时间】:2020-04-06 09:42:04
【问题描述】:

我有一个由 12 个元素组成的列表,每个元素都是一个数据框。每个 df 包含三列,两列跨所有元素,另一列不同。

两个常见的列是:

  • coche_OEM
  • dia_hora_OEM

在将列表转换为数据框时,可以将每个元素中不同的另一列折叠在唯一的列中。例如,其中一个元素中的列U0073 包含一个同名的值,而列B1182 包含另一个与变量名同名的元素。

问题是我想将此列表转换为具有三列(变量)的数据框:

  • coche_OEM
  • dia_hora_OEM
  • DTC:此列包含每列中存在的所有值及其代码。

列表是这样的:

listdf <- list(structure(list(B1182 = structure(1L, .Label = c("B1182", 
"NULL"), class = "factor"), coche_OEM = structure(3L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1577774413, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(B1182 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("B1182", 
"NULL"), class = "factor"), coche_OEM = structure(c(1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), 1L, integer(0), integer(0), integer(0), integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("B1182", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("B1182", "coche_OEM", 
"dia_hora_OEM")), structure(list(B124D = structure(1L, .Label = c("B124D", 
"NULL"), class = "factor"), coche_OEM = structure(3L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1577774413, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(B124D = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("B124D", 
"NULL"), class = "factor"), coche_OEM = structure(c(1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), 1L, integer(0), integer(0), integer(0), integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("B124D", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("B124D", "coche_OEM", 
"dia_hora_OEM")), structure(list(P2000 = structure(1L, .Label = c("c(\"P2000\", \"P2000\", \"P2000\")", 
"NULL"), class = "factor"), coche_OEM = structure(5L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1577793330, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(P2000 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c(\"P2000\", \"P2000\", \"P2000\")", 
"NULL"), class = "factor"), coche_OEM = structure(c(1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), integer(0), integer(0), 1L, integer(0), integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("P2000", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("P2000", "coche_OEM", 
"dia_hora_OEM")), structure(list(U3003 = structure(c(2L, 2L), .Label = c("NULL", 
"U3003"), class = "factor"), coche_OEM = structure(c(5L, 1L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(c(1577793330, 
1582648789), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
-2L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), groups = structure(list(
    U3003 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L), .Label = c("NULL", "U3003"), class = "factor"), 
    coche_OEM = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 
    4L, 5L, 6L), .Label = c("356232050832996", "356232050836666", 
    "356232050880755", "356232050882736", "356232050899078", 
    "356232050905933"), class = "factor"), .rows = list(integer(0), 
        integer(0), integer(0), integer(0), integer(0), integer(0), 
        2L, integer(0), integer(0), integer(0), 1L, integer(0))), .Names = c("U3003", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("U3003", "coche_OEM", 
"dia_hora_OEM")), structure(list(B1D01 = structure(c(1L, 1L, 
2L), .Label = c("B1D01", "c(\"B1D01\", \"B1D01\")", "NULL"), class = "factor"), 
    coche_OEM = structure(c(2L, 1L, 1L), .Label = c("356232050832996", 
    "356232050836666", "356232050880755", "356232050882736", 
    "356232050899078", "356232050905933"), class = "factor"), 
    dia_hora_OEM = structure(c(1581690876, 1582648789, 1582651926
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
-3L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), groups = structure(list(
    B1D01 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("B1D01", "c(\"B1D01\", \"B1D01\")", 
    "NULL"), class = "factor"), coche_OEM = structure(c(1L, 2L, 
    3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 
    6L), .Label = c("356232050832996", "356232050836666", "356232050880755", 
    "356232050882736", "356232050899078", "356232050905933"), class = "factor"), 
    .rows = list(2L, 1L, integer(0), integer(0), integer(0), 
        integer(0), 3L, integer(0), integer(0), integer(0), integer(0), 
        integer(0), integer(0), integer(0), integer(0), integer(0), 
        integer(0), integer(0))), .Names = c("B1D01", "coche_OEM", 
".rows"), row.names = c(NA, -18L), class = c("tbl_df", "tbl", 
"data.frame"), .drop = FALSE), .Names = c("B1D01", "coche_OEM", 
"dia_hora_OEM")), structure(list(U0155 = structure(2L, .Label = c("NULL", 
"U0155"), class = "factor"), coche_OEM = structure(1L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1582648789, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(U0155 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("NULL", 
"U0155"), class = "factor"), coche_OEM = structure(c(1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0), 
    1L, integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("U0155", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("U0155", "coche_OEM", 
"dia_hora_OEM")), structure(list(C1B00 = structure(1L, .Label = c("C1B00", 
"NULL"), class = "factor"), coche_OEM = structure(1L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1582648789, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(C1B00 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("C1B00", 
"NULL"), class = "factor"), coche_OEM = structure(c(1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(1L, integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("C1B00", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("C1B00", "coche_OEM", 
"dia_hora_OEM")), structure(list(P037D = structure(2L, .Label = c("NULL", 
"P037D"), class = "factor"), coche_OEM = structure(1L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1582648789, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(P037D = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("NULL", 
"P037D"), class = "factor"), coche_OEM = structure(c(1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0), 
    1L, integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("P037D", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("P037D", "coche_OEM", 
"dia_hora_OEM")), structure(list(P0616 = structure(2L, .Label = c("NULL", 
"P0616"), class = "factor"), coche_OEM = structure(1L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1582648789, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(P0616 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("NULL", 
"P0616"), class = "factor"), coche_OEM = structure(c(1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0), 
    1L, integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("P0616", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("P0616", "coche_OEM", 
"dia_hora_OEM")), structure(list(P0562 = structure(2L, .Label = c("NULL", 
"P0562"), class = "factor"), coche_OEM = structure(1L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1582648789, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(P0562 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("NULL", 
"P0562"), class = "factor"), coche_OEM = structure(c(1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0), 
    1L, integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("P0562", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("P0562", "coche_OEM", 
"dia_hora_OEM")), structure(list(U0073 = structure(2L, .Label = c("NULL", 
"U0073"), class = "factor"), coche_OEM = structure(1L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1582648789, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(U0073 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("NULL", 
"U0073"), class = "factor"), coche_OEM = structure(c(1L, 2L, 
3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0), 
    1L, integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("U0073", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("U0073", "coche_OEM", 
"dia_hora_OEM")), structure(list(P0138 = structure(1L, .Label = c("c(\"P0138\", \"P0138\", \"P0138\")", 
"NULL"), class = "factor"), coche_OEM = structure(5L, .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), dia_hora_OEM = structure(1583391111, class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -1L), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"), groups = structure(list(P0138 = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("c(\"P0138\", \"P0138\", \"P0138\")", 
"NULL"), class = "factor"), coche_OEM = structure(c(1L, 2L, 3L, 
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L), .Label = c("356232050832996", 
"356232050836666", "356232050880755", "356232050882736", "356232050899078", 
"356232050905933"), class = "factor"), .rows = list(integer(0), 
    integer(0), integer(0), integer(0), 1L, integer(0), integer(0), 
    integer(0), integer(0), integer(0), integer(0), integer(0))), .Names = c("P0138", 
"coche_OEM", ".rows"), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = FALSE), .Names = c("P0138", "coche_OEM", 
"dia_hora_OEM")))

那么,如何根据我的要求将此列表转换为数据框?

【问题讨论】:

  • 列表的dput 似乎不完整。不加载数据。
  • 这很奇怪,我从上面的问题中复制/粘贴只是为了确定它可以正常工作,并且对我有用。你能重试吗?
  • 不,它不在我的系统上。尝试了多次,也重新启动了 R。不知道出了什么问题。
  • 您是否知道列表中的某些 df 具有包含 c(c(\"P2000\", \"P2000\", \"P2000\") 的行,这意味着向量的字符串表示形式?
  • 我知道这些行存在,我稍后会尝试过滤它。 @GGamba 加载数据成功了吗?

标签: r list dataframe dplyr


【解决方案1】:

我们可以将所有不是“coche_OEM”或“dia_hora_OEM”的列重命名为预定义的字符串(此处为“id”):

map_df(listdf, ~rename_at(.x, vars(-c('coche_OEM', 'dia_hora_OEM')), ~'id'))
# A tibble: 15 x 3
# Groups:   id, coche_OEM [78]
   id                                   coche_OEM       dia_hora_OEM       
   <chr>                                <fct>           <dttm>             
 1 "B1182"                              356232050880755 2019-12-31 06:40:13
 2 "B124D"                              356232050880755 2019-12-31 06:40:13
 3 "c(\"P2000\", \"P2000\", \"P2000\")" 356232050899078 2019-12-31 11:55:30
 4 "U3003"                              356232050899078 2019-12-31 11:55:30
 5 "U3003"                              356232050832996 2020-02-25 16:39:49
 6 "B1D01"                              356232050836666 2020-02-14 14:34:36
 7 "B1D01"                              356232050832996 2020-02-25 16:39:49
 8 "c(\"B1D01\", \"B1D01\")"            356232050832996 2020-02-25 17:32:06
 9 "U0155"                              356232050832996 2020-02-25 16:39:49
10 "C1B00"                              356232050832996 2020-02-25 16:39:49
11 "P037D"                              356232050832996 2020-02-25 16:39:49
12 "P0616"                              356232050832996 2020-02-25 16:39:49
13 "P0562"                              356232050832996 2020-02-25 16:39:49
14 "U0073"                              356232050832996 2020-02-25 16:39:49
15 "c(\"P0138\", \"P0138\", \"P0138\")" 356232050899078 2020-03-05 06:51:51

【讨论】:

    猜你喜欢
    • 2019-12-14
    • 2013-02-28
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多