How can I (iteratively) save list elements to a data frame?


How can I (iteratively) save list elements to a data frame?



I have a dataframe that contains several lists.



The list I want to focus on specifically (df$list) contains product codes that I want to extract and save into a separate data frame.


df$list



To view the product code directly, I would write


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



As my data frame contains 528 rows, it is not plausible to do this manually.



I have put together the following as a starting point, which prints out the value at each list location:


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



However, the problem here is that not all rows follow the same format; some contain lists, others don't, and the ones that do don't always contain a product code.



As such, I get some results back and then I run into undefined columns selected errors and the loop exits


undefined columns selected



How can I perform the extraction of the codes if they are present and save them into a data frame?



If it helps, the field within the list I am looking at specifically is always called SKU as I am aware that fields can often be referred to by name.


SKU



EDIT:
I have now discovered how to solve the first part of the problem:


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



However, I want to know how to now write these values to a data frame, with each row corresponding to the list element that it came from. (Row 1 refers to the first list element.)



EDIT:
Here is a dput of the first five rows of my data frame.


dput



The field that contains the list is called _source.eventData.transaction.line_items (df$'_source.eventData.transaction.line_items')


_source.eventData.transaction.line_items


df$'_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")





So you want a data.frame with as many rows as df, and i'm not quite sure about the number of columns... 1 with the list inside once again, or as many columns as you have product in the list?
– Emmanuel-Lin
Jun 28 at 14:02





I would like a data frame with as many rows as list elements, and as many columns as components within the list. So if list[[1]] has three components, then it will take up one row, spread across three columns.
– MusTheDataGuy
Jun 28 at 14:13


list[[1]]





Does all your list have as many elements? Otherwise what do we put in column 4 if you only have 3 elements?
– Emmanuel-Lin
Jun 28 at 14:15





No, the number of list components varies.
– MusTheDataGuy
Jun 28 at 14:16





So you want the number of columns to be the max number of elements? What do we put in the last columns if the number of element is lower than the number of columns?
– Emmanuel-Lin
Jun 28 at 14:18




2 Answers
2



So you will have to adapt my solution to your problem since I don't have an example but here you go (using loops as asked):



I build a similar example:


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



Then i initialize the result size:


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



I loop:


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



And here you go


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



With your example:



Woooh, your example structure is quite ugly! a data.frame containing a list containing a data.frame :O



So it isn't at all a generic way



But I think I managed to do it:


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)
}

}



NB: There is a [[1]] because from what i have seen there is only one element in each list which is a data.frame.


[[1]]



Edit: I added a check that there something to paste.



And here is my result


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





Yes, it's an awful dataset! This is so close, but I am only seeing a single element: > print(res) [,1] [1,] "116932". What do you think has happened?
– MusTheDataGuy
Jun 29 at 8:10



> print(res) [,1] [1,] "116932"





Did you run it on the sample you gave me or on your data set? Also, please check the structure (dim(res)) just after initialization
– Emmanuel-Lin
Jun 29 at 8:11



dim(res)





It was my dataset. In fact, I'll dput a larger portion of it if that helps. Although, I don't know why you would get a data frame of results and I would only get a single-element result because I have more data than you. Would a larger dput help?
– MusTheDataGuy
Jun 29 at 8:13


dput


dput





If your sample didn't represent the actual structure, it might change something. First try to run it on the sample you gave me. If it works, then maybe a larger dput might help.
– Emmanuel-Lin
Jun 29 at 8:23





I managed to get it working on my dataset, however it gives res five columns and only gives me results in the first 10 rows; after that, all other rows are NA and there is nothing in columns 3, 4 and 5. I'll provide a larger dput. The dataset is 528 rows deep, so I'll see what I can do.
– MusTheDataGuy
Jun 29 at 8:47


res


NA


dput



You can convert each row to a dataframe / tibble and rowbind them afterwards like this:


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()



Result:


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift