如何(迭代地)将列表元素保存到数据框中?

时间:2018-06-28 12:15:13

标签: r list extract

我有一个包含多个列表的数据框。

我要特别关注的列表(df$list)包含要提取并保存到单独数据框中的产品代码。

要直接查看产品代码,我会写

> df$list[[1]][8]
product.sku_code
1           116932

由于我的数据框包含528行,因此手动进行此操作似乎不可行。

我将以下内容作为起点,在每个列表位置打印出该值:

for(i in 1:nrow(df)){
  print(df$list[[i]][8])
}

但是,这里的问题是,并非所有行都遵循相同的格式。有些包含列表,有些则不包含列表,而有些并不总是包含产品代码。

这样,我得到了一些结果,然后遇到了undefined columns selected错误,并且循环退出了

如果存在代码,如何提取代码并将其保存到数据框中?

如果有帮助,我正在查看的列表中的字段始终称为SKU,因为我知道这些字段通常可以用名称来引用。

编辑: 我现在发现了如何解决问题的第一部分:

for(i in 1:nrow(df)){
      print(df$list[[i]]["SKU"])
}

但是,我想知道现在如何将这些值写入数据帧,每一行都对应于它来自的列表元素。 (行1指向第一个列表元素。)

编辑: 这是我数据框的前五行中的dput

包含列表的字段称为_source.eventData.transaction.line_itemsdf$'_source.eventData.transaction.line_items'

structure(list(`_index` = list("site-events", "site-events", 
    "site-events", "site-events", "site-events", "site-events", 
    "site-events", "site-events", "site-events", "site-events", 
    "site-events", "site-events", "site-events", "site-events", 
    "site-events", "site-events", "site-events", "site-events", 
    "site-events", "site-events", "site-events", "site-events", 
    "site-events", "site-events", "site-events"), `_type` = list(
    "user-event", "user-event", "user-event", "user-event", "user-event", 
    "user-event", "user-event", "user-event", "user-event", "user-event", 
    "user-event", "user-event", "user-event", "user-event", "user-event", 
    "user-event", "user-event", "user-event", "user-event", "user-event", 
    "user-event", "user-event", "user-event", "user-event", "user-event"), 
    `_id` = list("AWHn7b4DCEE3nh9to6Y9", "AWH9cV87PoCHXbqu_yS-", 
        "AWIDbUsmCEE3nh9tsWUL", "AWHodrcwGIs7chfgH3ki", "AWHt9FhVPoCHXbqu93ZI", 
        "AWHnvqyVCYVeX8vPMmO2", "AWHoTRjOPoCHXbqu9DA-", "AWILdC_sCEE3nh9ttKdH", 
        "AWISw4-xCEE3nh9tuDT_", "AWIMAd7gGIs7chfgMETm", "AWJGf8HlPNR2ZIQbF094", 
        "AWJLph8TPNR2ZIQbGnAw", "AWJUTXJ4nItPliZWdS4U", "AWJQzHvBnItPliZWchCn", 
        "AWJ_JbXFPNR2ZIQbPCBQ", "AWJ_JbXIqZOjxfTF2L8E", "AWJ5_1wlPNR2ZIQbOU4-", 
        "AWJ5_1wlW7mRqY-aUf7h", "AWJ5_1xDnItPliZWi0mj", "AWKJcm79W7mRqY-aWd2N", 
        "AWJk3f6vqZOjxfTFzKDO", "AWJbGTQvW7mRqY-aQbet", "AWJvsqKkPNR2ZIQbNRjr", 
        "AWJt8G9yPNR2ZIQbNFtk", "AWKTvyeRPNR2ZIQbReNi"), `_score` = list(
        6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 
        6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 
        6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 6.8013, 
        6.8013, 6.8013, 6.8013, 6.8013), `_source.type` = list(
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout", "user-basket-checkout", "user-basket-checkout", 
        "user-basket-checkout"), `_source.funnelType` = list(
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `_source.attributed` = list(
        "Vq3VblKpZn", "Vq3VblKpZn", "PXU7CqGO1S", "Vq3VblKpZn", 
        "MYbsg1E2EQ", "Vq3VblKpZn", "Vq3VblKpZn", "q9SsmoL8Ig", 
        "ISdTwUISIG", "MYbsg1E2EQ", "PXU7CqGO1S", "ISdTwUISIG", 
        "ISdTwUISIG", "wncQ5pXq88", "MYbsg1E2EQ", "MYbsg1E2EQ", 
        "ISdTwUISIG", "wncQ5pXq88", "wncQ5pXq88", "q9SsmoL8Ig", 
        "q9SsmoL8Ig", "Oft5GPL1Kz", "kEgnBQeUVH", "MYbsg1E2EQ", 
        "MYbsg1E2EQ"), `_source.attributedMerchant` = list("dxrb8CqFzw", 
        "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", 
        "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", 
        "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", 
        "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", 
        "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", 
        "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw", "dxrb8CqFzw"), 
    `_source.user` = list("oCLBBW7VhH", "yXlIO35CxU", "l1gWPplbr5", 
        "oCLBBW7VhH", "Pb0lF2mzc7", "yXlIO35CxU", "yXlIO35CxU", 
        "S97H7IjaJP", "nqgb9Cv6ZI", "DNayKtdGpX", "osQiHOe5zC", 
        "bNS0OelJos", "b5CeKOGPhU", "BYEiBYXD8V", "bkhGf7Xlim", 
        "biD8QcBhJy", "IqxLN22aNc", "cbFPIKEokD", "BYEiBYXD8V", 
        "MidADe5pM8", "LR21jZ0uI5", "yVJmCs8p66", "12RqBiPvpV", 
        "FT9NZcKCVf", "B5gAS2Fcuf"), `_source.retailer` = list(
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe"), `_source.applicationId` = list("ca3a503b-cee1-4d3f-ae22-5850c3805dd2", 
        "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", 
        "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", 
        "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", 
        "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", NULL, "ca3a503b-cee1-4d3f-ae22-5850c3805dd2", 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL), `_source.short` = list(
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
        NULL, FALSE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), `_source.partial` = list(
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
        NULL, FALSE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), `_source.recordedAt` = list(
        "2018-03-02T18:16:12.908Z", "2018-03-06T22:32:00.827Z", 
        "2018-03-08T02:25:16.942Z", "2018-03-02T20:45:49.646Z", 
        "2018-03-03T22:21:08.918Z", "2018-03-02T17:24:47.888Z", 
        "2018-03-02T20:00:22.090Z", "2018-03-09T15:49:46.434Z", 
        "2018-03-11T01:53:48.919Z", "2018-03-09T18:24:31.817Z", 
        "2018-03-21T03:00:00.516Z", "2018-03-22T03:00:00.669Z", 
        "2018-03-23T19:19:44.441Z", "2018-03-23T03:00:00.958Z", 
        "2018-04-01T03:00:00.485Z", "2018-04-01T03:00:00.501Z", 
        "2018-03-31T03:00:01.066Z", "2018-03-31T03:00:01.088Z", 
        "2018-03-31T03:00:01.139Z", "2018-04-03T03:00:00.836Z", 
        "2018-03-27T00:31:32.980Z", "2018-03-25T03:00:01.057Z", 
        "2018-03-29T03:00:00.551Z", "2018-03-28T18:48:16.477Z", 
        "2018-04-05T03:00:00.969Z"), `_source.time` = list("2018-03-02T18:16:11.161Z", 
        "2018-03-06T22:31:59.103Z", "2018-03-08T02:25:14.054Z", 
        "2018-03-02T20:45:48.121Z", "2018-03-03T22:21:06.434Z", 
        "2018-02-27T21:08:41.271Z", "2018-03-02T20:00:20.548Z", 
        "2018-03-09T15:49:37.673Z", "2018-03-11T01:53:47.194Z", 
        "2018-03-09T18:24:30.214Z", "2018-03-20T21:35:56.129Z", 
        "2018-03-21T17:50:25.114Z", "2018-03-23T19:19:42.096Z", 
        "2018-03-22T18:34:57.151Z", "2018-03-31T16:06:51.457Z", 
        "2018-03-31T16:17:02.119Z", "2018-03-30T04:54:26.887Z", 
        "2018-03-30T20:49:55.520Z", "2018-03-30T21:52:41.461Z", 
        "2018-04-02T18:52:18.667Z", "2018-03-27T00:31:31.830Z", 
        "2018-03-24T22:01:48.170Z", "2018-03-28T17:41:58.721Z", 
        "2018-03-28T18:48:15.689Z", "2018-04-04T17:39:10.346Z"), 
    `_source.order_id` = list(NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, "8059165", NULL, "8059322", "8059330", "8059429", 
        "8059422", "8059501", "8059502", "8059601", "8059499", 
        "8059604", "8059723", "8059569", "8059438", "8059588", 
        "8059590", "8059825"), `_source.funnel` = list(NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL), `_source.added` = list(
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL), `_source.eventData.time` = list(
        1520014571161, 1520375519103, 1520475914054, 1520023548121, 
        1520115666434, 1519765721271, 1520020820548, 1520610577673, 
        1520733227194, 1520619870214, "2018-03-20T21:35:56.129Z", 
        "2018-03-21T17:50:25.114Z", 1521832782096, "2018-03-22T18:34:57.151Z", 
        "2018-03-31T16:06:51.457Z", "2018-03-31T16:17:02.119Z", 
        "2018-03-30T04:54:26.887Z", "2018-03-30T20:49:55.520Z", 
        "2018-03-30T21:52:41.461Z", "2018-04-02T18:52:18.667Z", 
        1522110691830, "2018-03-24T22:01:48.170Z", "2018-03-28T17:41:58.721Z", 
        1522262895689, "2018-04-04T17:39:10.346Z"), `_source.eventData.attributions` = list(
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL), `_source.eventData.transaction.order_id` = list(
        "8058983", "8059024", "8059032", "8058986", "8059096", 
        "8059071", "8058985", "8059141", "8059165", "8059142", 
        "8059322", "8059330", "8059429", "8059422", "8059501", 
        "8059502", "8059601", "8059499", "8059604", "8059723", 
        "8059569", "8059438", "8059588", "8059590", "8059825"), 
    `_source.eventData.transaction.returning` = list(FALSE, FALSE, 
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), `_source.eventData.transaction.currency` = list(
        "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
        "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
        "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
        "USD"), `_source.eventData.transaction.subtotal` = list(
        1150L, 1195L, 990L, 1995L, 1490L, 595L, 1595L, 385L, 
        750L, 395L, 420L, 195L, 1190L, 295L, 395L, 595L, 695L, 
        395L, 295L, 820L, 1190L, 295L, 495L, 595L, 750L), `_source.eventData.transaction.subtotal_include_tax` = list(
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
        FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
        FALSE), `_source.eventData.transaction.tax` = list(80.5, 
        74.69, 61.88, 139.65, 98.71, 35.7, 123.61, 24.07, 0L, 
        23.7, 24.69, 17.31, 83.3, 22.86, 35.06, 37.78, 62.55, 
        23.7, 22.86, 54.46, 98.18, 25.44, 39.6, 29.75, 46.88), 
    `_source.eventData.transaction.shipping_cost` = list(0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 35L, 0L, 0L, 25L, 0L, 25L, 0L, 
        0L, 0L, 25L, 0L, 0L, 25L, 0L, 0L, 0L, 0L, 0L), `_source.eventData.transaction.total` = list(
        1230.5, 1269.69, 1051.88, 2134.65, 1588.71, 630.7, 1718.61, 
        409.07, 750L, 418.7, 444.69, 212.31, 1273.3, 317.86, 
        430.06, 632.78, 757.55, 418.7, 317.86, 874.46, 1288.18, 
        320.44, 534.6, 624.75, 796.88), `_source.eventData.transaction.line_items` = list(
        structure(list(quantity = list(1L), subtotal = list(1150L), 
            product.title = list("Women's c Chain 5MM Necklace in s s with Diamonds"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(1150L), product.unit_price = list(
                1150L), product.url = list("http://site"), 
            product.sku_code = list("116932"), product.currency = list(
                "USD")), class = "data.frame", row.names = 1L), 
        structure(list(quantity = list(1L), subtotal = list(1195L), 
            product.title = list("Men's c Chain 12mm Link Necklace in s s"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(1195L), product.unit_price = list(
                1195L), product.url = list("http://site/c-chain-12mm-link-necklace-in-s/235454"), 
            product.sku_code = list("235454"), product.currency = list(
                "USD")), class = "data.frame", row.names = 1L), 
        structure(list(quantity = list(1L, 1L), subtotal = list(
            595L, 395L), product.title = list("Women's Bamboo Ring in s s with Black Sapphire", 
            "Men's c Chain Triple Wrap 3.5mm b in s s, Leather"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L)), structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(595L, 395L), product.unit_price = list(
                595L, 395L), product.url = list("http://site/bamboo-ring-in-s-with-gemstone/216658", 
                "http://site/c-chain-triple-wrap-3.5mm-b-in-s%252C-leather/228922"), 
            product.sku_code = list("216658", "228922"), product.currency = list(
                "USD", "USD")), class = "data.frame", row.names = 1:2), 
        structure(list(quantity = list(1L), subtotal = list(1995L), 
            product.title = list("Women's c Chain 9MM Graduated Necklace in s s and Wood"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(1995L), product.unit_price = list(
                1995L), product.url = list("http://site"), 
            product.sku_code = list("229108"), product.currency = list(
                "USD")), class = "data.frame", row.names = 1L), 
        structure(list(quantity = list(1L, 1L), subtotal = list(
            495L, 995L), product.title = list("r", 
            "Women's Legends 5MM Necklace in s s and g g"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L)), structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(495L, 995L), product.unit_price = list(
                495L, 995L), product.url = list("http://site", 
                "http://site/legends-5mm-necklace-in-s-and-g-g/204668"), 
            product.sku_code = list("147982", "204668"), product.currency = list(
                "USD", "USD")), class = "data.frame", row.names = 1:2), 
        structure(list(quantity = list(1L), subtotal = list(595L), 
            product.title = list("Women's Legends Naga 5MM Station b in s s and g g"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(595L), product.unit_price = list(
                595L), product.url = list("http://site"), 
            product.sku_code = list("216486"), product.currency = list(
                "USD")), class = "data.frame", row.names = 1L), 
        structure(list(quantity = list(1L), subtotal = list(1595L), 
            product.title = list("Women's c Chain 10.5MM b in s s with Blue Sapphire"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(1595L), product.unit_price = list(
                1595L), product.url = list("http://site/c-chain-10.5mm-b-in-s-with-gemstone/139792"), 
            product.sku_code = list("139792"), product.currency = list(
                "USD")), class = "data.frame", row.names = 1L), 
        structure(list(quantity = list(1L), subtotal = list(350L), 
            product.title = list("Women's Modern Chain Small J Hoop Earring in s s"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(350L), product.unit_price = list(
                350L), product.url = list("http://site"), 
            product.sku_code = list("229182"), product.currency = list(
                "USD")), class = "data.frame", row.names = 1L), 
        structure(list(quantity = list(1L), subtotal = list(750L), 
            product.time = list(1520658013980), product.sku_code = list(
                "230751"), product.categories = list("g"), 
            product.title = list("Women's c Chain 5MM Hammered Clasp b, s s, g g"), 
            product.images = list(structure(c("http://site", 
            "http://site"
            ), .Dim = 2:1)), product.url = list("http://site"), 
            product.stockQuantity = list(1L), product.price = list(
                "750"), product.currency = list("USD"), product.retailer = list(
                "site.com"), product.id = list("site.com230751"), 
            product.metadata.gender = list("female"), product.metadata.age_group = list(
                "adult"), product.metadata.size = list("XS"), 
            product.metadata.style_number = list("BZ96184"), 
            product.metadata.material = list("s s/g Bonded Yellow g"), 
            product.merchant.objectId = list("R9xuoK1HBe"), product.merchant.className = list(
                "ParentMerchant")), class = "data.frame", row.names = 1L), 
        structure(list(quantity = list(1L), subtotal = list(395L), 
            product.title = list("Men's Chain Jawan 4MM Station b in s s, Leather"), 
            product.images = list(structure("http://site", .Dim = c(1L, 
            1L))), product.price = list(395L), product.unit_price = list(
                395L), product.url = list("http://site/chain-jawan-4mm-station-b-in-s%252C-leather/230343"), 
            product.sku_code = list("230343"), product.currency = list(
                "USD")), class = "data.frame", row.names = 1L), 
        list(), list(), structure(list(quantity = list(1L, 1L, 
            1L), subtotal = list(595L, 595L, 0L), product.time = list(
            1521781210042, 1521781210788, NULL), product.sku_code = list(
            "226406", "227077", "235862"), product.categories = list(
            "site/Women's/Earrings/Legends Collection/s s | g Yellow g", 
            "site/Women's/Rings/Legends Collection/s s | g Yellow g", 
            NULL), product.title = list("Women's Legends Cobra Drop Earring in s s and g g", 
            "Women's Legends Cobra Double Coil Ring in s s and g g", 
            NULL), product.images = list(structure(c("http://site", 
        "http://site/on/d.static/-/Sites-master-catalog/default/dw7468a557/images/large/EZ96144_Mainj"
        ), .Dim = 2:1), structure(c("http://site", 
        "http://site/on/d.static/-/Sites-master-catalog/default/dw36a43881/images/large/RZ93275_Mainj"
        ), .Dim = 2:1), NULL), product.url = list("http://site/legends-cobra-drop-earring-in-s-and-g-g/226406", 
            "http://site/legends-cobra-double-coil-ring-in-s-and-g-g/227077", 
            NULL), product.stockQuantity = list(1L, 1L, NULL), 
            product.price = list("595", "595", NULL), product.currency = list(
                "USD", "USD", NULL), product.retailer = list(
                "site.com", "site.com", NULL), product.id = list(
                "site.com226406", "site.com227077", 
                "235862"), product.name = list(NULL, NULL, "g g Link Cord b"), 
            product.category = list(NULL, NULL, "Visit Us in Bali"), 
            product.metadata.gender = list("female", "female", 
                NULL), product.metadata.age_group = list("adult", 
                "adult", NULL), product.metadata.size = list(
                "NO SIZE", "8", NULL), product.metadata.style_number = list(
                "EZ96144", "RZ93275", NULL), product.metadata.material = list(
                "s s/g Yellow g", "s s/g Yellow g", 
                NULL), product.merchant.objectId = list("R9xuoK1HBe", 
                "R9xuoK1HBe", NULL), product.merchant.className = list(
                "ParentMerchant", "ParentMerchant", NULL)), class = "data.frame", row.names = c(NA, 
        3L)), list(), list(), list(), list(), list(), list(), 
        list(), structure(list(quantity = list(1L, 1L), subtotal = list(
            495L, 695L), product.time = list(1522040405886, 1522040406674), 
            product.sku_code = list("215732", "228753"), product.categories = list(
                "site/Women's/b/Chain Collection/s s", 
                "site/Women's/b/Chain Collection/s s"), 
            product.title = list("Women's c Chain 5MM b in s s with Amethyst", 
                "Women's c Chain 5MM b in s s with Mandarin Garnet"), 
            product.images = list(structure(c("http://site", 
            "http://site"
            ), .Dim = 2:1), structure(c("http://site", 
            "http://site"
            ), .Dim = 2:1)), product.url = list("http://site", 
                "http://site/c-chain-5mm-b-in-s-with-gemstone/228753"), 
            product.stockQuantity = list(1L, 1L), product.price = list(
                "495", "695"), product.currency = list("USD", 
                "USD"), product.retailer = list("site.com", 
                "site.com"), product.id = list("site.com215732", 
                "site.com228753"), product.metadata.gender = list(
                "female", "female"), product.metadata.age_group = list(
                "adult", "adult"), product.metadata.size = list(
                "L", "L"), product.metadata.style_number = list(
                "BBS96002SNP", "BBS96002SNP"), product.metadata.material = list(
                "s s", "s s"), product.merchant.objectId = list(
                "R9xuoK1HBe", "R9xuoK1HBe"), product.merchant.className = list(
                "ParentMerchant", "ParentMerchant")), class = "data.frame", row.names = 1:2), 
        list(), list(), structure(list(quantity = list(1L), subtotal = list(
            595L), product.time = list(1522213215964), product.sku_code = list(
            "230431"), product.categories = list("site/Men's/Rings/Chain Collection/s s"), 
            product.title = list("Men's c Chain 9MM Band Ring in s s, Black Volcanic, Pave White Diamond (0.05ct)"), 
            product.images = list(structure(c("http://site", 
            "http://site"
            ), .Dim = 2:1)), product.url = list("http://site"), 
            product.stockQuantity = list(1L), product.price = list(
                "595"), product.currency = list("USD"), product.retailer = list(
                "site.com"), product.id = list("site.com230431"), 
            product.metadata.gender = list("male"), product.metadata.age_group = list(
                "adult"), product.metadata.size = list("10"), 
            product.metadata.style_number = list("RBS9333512VODI"), 
            product.metadata.material = list("s s"), 
            product.merchant.objectId = list("R9xuoK1HBe"), product.merchant.className = list(
                "ParentMerchant")), class = "data.frame", row.names = 1L), 
        list()), `_source.eventData.transaction.user` = list(
        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
        NULL, "osQiHOe5zC", "bNS0OelJos", NULL, "BYEiBYXD8V", 
        "bkhGf7Xlim", "biD8QcBhJy", "IqxLN22aNc", "cbFPIKEokD", 
        "BYEiBYXD8V", "MidADe5pM8", NULL, "yVJmCs8p66", "12RqBiPvpV", 
        NULL, "B5gAS2Fcuf"), `_source.merchant.className` = list(
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant", "ParentMerchant", "ParentMerchant", 
        "ParentMerchant"), `_source.merchant.objectId` = list(
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", "R9xuoK1HBe", 
        "R9xuoK1HBe"), `_source.sitemetrics.totalItems` = list(
        1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 
        1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), `_source.sitemetrics.initialCurrency` = list(
        "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
        "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
        "USD", "USD", "USD", "USD", "USD", "USD", "USD", "USD", 
        "USD"), `_source.sitemetrics.exchangeRate` = list(0.7255, 
        0.7184, 0.7209, 0.7255, 0.7255, 0.7255, 0.7255, 0.7203, 
        0.7232, 0.7203, 0.7145, 0.7114, 0.707, 0.708, 0.7101, 
        0.7101, 0.7101, 0.7101, 0.7101, 0.7101, 0.703, 0.707, 
        0.7066, 0.7066, 0.7134), `_source.sitemetrics.exchangeRateFloat` = list(
        0.7255, 0.7184, 0.7209, 0.7255, 0.7255, 0.7255, 0.7255, 
        0.7203, 0.7232, 0.7203, 0.7145, 0.7114, 0.707, 0.708, 
        0.7101, 0.7101, 0.7101, 0.7101, 0.7101, 0.7101, 0.703, 
        0.707, 0.7066, 0.7066, 0.7134), `_source.sitemetrics.shipping` = list(
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 2521L, 0L, 0L, 1786L, 0L, 
        1767L, 0L, 0L, 0L, 1775L, 0L, 0L, 1775L, 0L, 0L, 0L, 
        0L, 0L), `_source.sitemetrics.promotion` = list(0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
        0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), `_source.sitemetrics.totalPaid` = list(
        89269L, 91220L, 75829L, 154862L, 115256L, 45755L, 124680L, 
        29464L, 54243L, 30158L, 31774L, 15104L, 90021L, 22505L, 
        30538L, 44933L, 53793L, 29731L, 22571L, 62095L, 90558L, 
        22655L, 37775L, 44145L, 56847L), `_source.sitemetrics.tax` = list(
        5840L, 5366L, 4461L, 10131L, 7161L, 2590L, 8968L, 1734L, 
        0L, 1707L, 1764L, 1231L, 5889L, 1619L, 2490L, 2683L, 
        4442L, 1683L, 1623L, 3867L, 6902L, 1799L, 2798L, 2102L, 
        3344L), `_source.sitemetrics.productRevenue` = list(89269L, 
        91220L, 75829L, 154862L, 115256L, 45755L, 124680L, 26943L, 
        54243L, 30158L, 29988L, 15104L, 88254L, 22505L, 30538L, 
        44933L, 52018L, 29731L, 22571L, 60320L, 90558L, 22655L, 
        37775L, 44145L, 56847L), `_source.sitemetrics.netsiteRevenue` = list(
        89269L, 91220L, 75829L, 154862L, 115256L, 45755L, 124680L, 
        26943L, 54243L, 30158L, 29988L, 15104L, 88254L, 22505L, 
        30538L, 44933L, 52018L, 29731L, 22571L, 60320L, 90558L, 
        22655L, 37775L, 44145L, 56847L)), row.names = c(NA, 25L
), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

因此,由于我没有示例,因此您必须调整我的解决方案以解决您的问题,但是您可以开始使用(按要求使用循环):

我建立了一个类似的例子:

df <- data.frame(a=1:2,b=I(list(1:2,1:3)))

df
  a       b
1 1    1, 2
2 2 1, 2, 3

然后我初始化结果大小:

res <- matrix(nrow = nrow(df), ncol = max(sapply(df$b, length)))

我循环:

for (i in 1:nrow(df)){
  res[i, 1:length(df$b[[i]])] = df$b[[i]]
}

然后你去

print(res)
     [,1] [,2] [,3]
[1,]    1    2   NA
[2,]    1    2    3

以您的示例为例:

哦,您的示例结构非常难看!包含一个包含data.frame:O

的列表的data.frame

所以这根本不是通用的方式

但是我想我做到了:

col = "_source.eventData.transaction.line_items"
res <- matrix(nrow = nrow(df), ncol = max(sapply(df[[col]], function(x){length(x[["product.sku_code"]])})))
for (i in 1:nrow(df)){
  values = df[i, col][[1]][["product.sku_code"]]
  if (length(values) > 0){ # Check that there is something to paste)
    res[i, 1:length(values)] = unlist(values)  
  }

}

注意:有一个[[1]],因为从我所看到的来看,每个列表中只有一个元素是data.frame。

编辑:我添加了一项检查,以确认是否有粘贴内容。

这是我的结果

print(res)
     [,1]     [,2]    
[1,] "116932" NA      
[2,] "235454" NA      
[3,] "216658" "228922"
[4,] "229108" NA      
[5,] "147982" "204668"

答案 1 :(得分:0)

您可以将每一行转换为一个数据框/小标题,然后像下面这样对它们进行行绑定:

library(tidyverse)

test <- tibble(a = list(list(L1="A1", L2="B1", L3="C1"),
                        list(L1="A2", L3="C2"),
                        list(L2="B3")))


test %>%
  rowwise() %>% 
  transmute(a = list(as.tibble(a))) %>% 
  ungroup() %>% 
  pull(a) %>% 
  bind_rows()

结果:

  L1    L2    L3   
  <chr> <chr> <chr>
1 A1    B1    C1   
2 A2    NA    C2   
3 NA    B3    NA