递归地将`unnest_wider()`应用于所有列,直到没有更多的嵌套列表列?

Posted

技术标签:

【中文标题】递归地将`unnest_wider()`应用于所有列,直到没有更多的嵌套列表列?【英文标题】:Applying `unnest_wider()` recursively to all columns, until no more nested list columns remain? 【发布时间】:2020-03-18 01:04:52 【问题描述】:

this question 的后续行动:是否可以递归应用 unnest_wider,直到没有更多的 列表列 可用?

我正在处理一个深度嵌套的数据集,其中一个列表列有一个级别(一个 unnest_wider 调用就可以了);但另一个有 228-237 个不同深度的列表列。我想“分解”整个数据集,使其整洁,所有列都位于层次结构的顶部。

这是一个示例:

structure(list(fullVisitorId = c("2248281639583218707", "8647436381089107732"
), date = c("20170801", "20170801"), visitStartTime = c(1501583974L, 
1501616585L), totals = list(list(visits = 1L, hits = 1L, pageviews = 1L, 
    timeOnSite = NA_integer_, bounces = 1L, transactions = NA_integer_, 
    transactionRevenue = NA_integer_, newVisits = 1L, screenviews = NA_integer_, 
    uniqueScreenviews = NA_integer_, timeOnScreen = NA_integer_, 
    totalTransactionRevenue = NA_integer_, sessionQualityDim = 1L), 
    list(visits = 1L, hits = 1L, pageviews = 1L, timeOnSite = NA_integer_, 
        bounces = 1L, transactions = NA_integer_, transactionRevenue = NA_integer_, 
        newVisits = 1L, screenviews = NA_integer_, uniqueScreenviews = NA_integer_, 
        timeOnScreen = NA_integer_, totalTransactionRevenue = NA_integer_, 
        sessionQualityDim = 1L)), channelGrouping = c("Organic Search", 
"Organic Search"), hits = list(structure(list(hitNumber = 1L, 
    time = 0L, hour = 3L, minute = 39L, isSecure = NA, isInteraction = TRUE, 
    isEntrance = TRUE, isExit = TRUE, referer = "http://www.google.com/", 
    page = list(list(pagePath = "/google+redesign/electronics", 
        hostname = "shop.googlemerchandisestore.com", pageTitle = "Electronics | Google Merchandise Store", 
        searchKeyword = NA_character_, searchCategory = NA_character_, 
        pagePathLevel1 = "/google+redesign/", pagePathLevel2 = "/electronics", 
        pagePathLevel3 = "", pagePathLevel4 = "")), transaction = list(
        list(transactionId = NA_character_, transactionRevenue = NA_integer_, 
            transactionTax = NA_integer_, transactionShipping = NA_integer_, 
            affiliation = NA_character_, currencyCode = "USD", 
            localTransactionRevenue = NA_integer_, localTransactionTax = NA_integer_, 
            localTransactionShipping = NA_integer_, transactionCoupon = NA_character_)), 
    item = list(list(transactionId = NA_character_, productName = NA_character_, 
        productCategory = NA_character_, productSku = NA_character_, 
        itemQuantity = NA_integer_, itemRevenue = NA_integer_, 
        currencyCode = "USD", localItemRevenue = NA_integer_)), 
    contentInfo = list(structure(list(NULL), .Names = "")), appInfo = list(
        list(name = NA_character_, version = NA_character_, id = NA_character_, 
            installerId = NA_character_, appInstallerId = NA_character_, 
            appName = NA_character_, appVersion = NA_character_, 
            appId = NA_character_, screenName = "shop.googlemerchandisestore.com/google+redesign/electronics", 
            landingScreenName = "shop.googlemerchandisestore.com/google+redesign/electronics", 
            exitScreenName = "shop.googlemerchandisestore.com/google+redesign/electronics", 
            screenDepth = "0")), exceptionInfo = list(list(description = NA_character_, 
        isFatal = TRUE, exceptions = NA_integer_, fatalExceptions = NA_integer_)), 
    eventInfo = list(structure(list(NULL, NULL, NULL, NULL), .Names = c("", 
    "", "", ""))), product = list(structure(list(productSKU = c("GGOEGBFC018799", 
    "GGOEGESB015199", "GGOEGEVA022399", "GGOEGCBB074199", "GGOEGFKA022299", 
    "GGOEGCBB074399", "GGOEGCBC074299", "GGOEGEHQ072499", "GGOEGEHQ072599", 
    "GGOEGESB015099", "GGOEGESC014099", "GGOEGESQ016799"), v2ProductName = c("Electronics Accessory Pouch", 
    "Google Flashlight", "Micro Wireless Earbud", "Google Car Clip Phone Holder", 
    "Keyboard DOT Sticker", "Google Device Holder Sticky Pad", 
    "Google Device Stand", "Google 2200mAh Micro Charger", "Google 4400mAh Power Bank", 
    "Basecamp Explorer Powerbank Flashlight", "Rocket Flashlight", 
    "Plastic Sliding Flashlight"), v2ProductCategory = c("Home/Electronics/", 
    "Home/Electronics/", "Home/Electronics/", "Home/Electronics/", 
    "Home/Electronics/", "Home/Electronics/", "Home/Electronics/", 
    "Home/Electronics/", "Home/Electronics/", "Home/Electronics/", 
    "Home/Electronics/", "Home/Electronics/"), productVariant = c("(not set)", 
    "(not set)", "(not set)", "(not set)", "(not set)", "(not set)", 
    "(not set)", "(not set)", "(not set)", "(not set)", "(not set)", 
    "(not set)"), productBrand = c("(not set)", "(not set)", 
    "(not set)", "(not set)", "(not set)", "(not set)", "(not set)", 
    "(not set)", "(not set)", "(not set)", "(not set)", "(not set)"
    ), productRevenue = c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), localProductRevenue = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_), productPrice = c(4990000L, 59990000L, 39990000L, 
    6990000L, 1500000L, 4990000L, 4990000L, 22990000L, 37990000L, 
    22990000L, 4990000L, 12990000L), localProductPrice = c(4990000L, 
    59990000L, 39990000L, 6990000L, 1500000L, 4990000L, 4990000L, 
    22990000L, 37990000L, 22990000L, 4990000L, 12990000L), productQuantity = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_), productRefundAmount = c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), localProductRefundAmount = c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), isImpression = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
    TRUE, TRUE, TRUE, TRUE, TRUE), isClick = c(NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA), customDimensions = list(
        structure(list(index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame"))), customMetrics = list(
        structure(list(index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
            index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame"))), productListName = c("Category", 
    "Category", "Category", "Category", "Category", "Category", 
    "Category", "Category", "Category", "Category", "Category", 
    "Category"), productListPosition = 1:12, productCouponCode = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_)), row.names = c(NA, 
    -12L), class = c("tbl_df", "tbl", "data.frame"))), promotion = list(
        structure(list(promoId = character(0), promoName = character(0), 
            promoCreative = character(0), promoPosition = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame"))), promotionActionInfo = list(
        structure(list(NULL, NULL), .Names = c("", ""))), refund = list(
        structure(list(NULL, NULL), .Names = c("", ""))), eCommerceAction = list(
        list(action_type = "0", step = 1L, option = NA_character_)), 
    experiment = list(structure(list(experimentId = character(0), 
        experimentVariant = character(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame"))), publisher = list(
        structure(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, 
            NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
            NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
            NULL, NULL, NULL), .Names = c("", "", "", "", "", 
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
        "", "", "", "", "", "", "", "", "", "", "", "", ""))), 
    customVariables = list(structure(list(index = integer(0), 
        customVarName = character(0), customVarValue = character(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame"))), customDimensions = list(
        structure(list(index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame"))), customMetrics = list(
        structure(list(index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame"))), type = "PAGE", 
    social = list(list(socialInteractionNetwork = NA_character_, 
        socialInteractionAction = NA_character_, socialInteractions = NA_integer_, 
        socialInteractionTarget = NA_character_, socialNetwork = "(not set)", 
        uniqueSocialInteractions = NA_integer_, hasSocialSourceReferral = "No", 
        socialInteractionNetworkAction = " : ")), latencyTracking = list(
        structure(list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
            NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), .Names = c("", 
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
        ""))), sourcePropertyInfo = list(structure(list(NULL, 
        NULL), .Names = c("", ""))), contentGroup = list(list(
        contentGroup1 = "(not set)", contentGroup2 = "Electronics", 
        contentGroup3 = "(not set)", contentGroup4 = "(not set)", 
        contentGroup5 = "(not set)", previousContentGroup1 = "(entrance)", 
        previousContentGroup2 = "(entrance)", previousContentGroup3 = "(entrance)", 
        previousContentGroup4 = "(entrance)", previousContentGroup5 = "(entrance)", 
        contentGroupUniqueViews1 = NA_integer_, contentGroupUniqueViews2 = 1L, 
        contentGroupUniqueViews3 = NA_integer_, contentGroupUniqueViews4 = NA_integer_, 
        contentGroupUniqueViews5 = NA_integer_)), dataSource = "web", 
    publisher_infos = list(structure(list(dfpClicks = integer(0), 
        dfpImpressions = integer(0), dfpMatchedQueries = integer(0), 
        dfpMeasurableImpressions = integer(0), dfpQueries = integer(0), 
        dfpRevenueCpm = integer(0), dfpRevenueCpc = integer(0), 
        dfpViewableImpressions = integer(0), dfpPagesViewed = integer(0), 
        adsenseBackfillDfpClicks = integer(0), adsenseBackfillDfpImpressions = integer(0), 
        adsenseBackfillDfpMatchedQueries = integer(0), adsenseBackfillDfpMeasurableImpressions = integer(0), 
        adsenseBackfillDfpQueries = integer(0), adsenseBackfillDfpRevenueCpm = integer(0), 
        adsenseBackfillDfpRevenueCpc = integer(0), adsenseBackfillDfpViewableImpressions = integer(0), 
        adsenseBackfillDfpPagesViewed = integer(0), adxBackfillDfpClicks = integer(0), 
        adxBackfillDfpImpressions = integer(0), adxBackfillDfpMatchedQueries = integer(0), 
        adxBackfillDfpMeasurableImpressions = integer(0), adxBackfillDfpQueries = integer(0), 
        adxBackfillDfpRevenueCpm = integer(0), adxBackfillDfpRevenueCpc = integer(0), 
        adxBackfillDfpViewableImpressions = integer(0), adxBackfillDfpPagesViewed = integer(0), 
        adxClicks = integer(0), adxImpressions = integer(0), 
        adxMatchedQueries = integer(0), adxMeasurableImpressions = integer(0), 
        adxQueries = integer(0), adxRevenue = integer(0), adxViewableImpressions = integer(0), 
        adxPagesViewed = integer(0), adsViewed = integer(0), 
        adsUnitsViewed = integer(0), adsUnitsMatched = integer(0), 
        viewableAdsViewed = integer(0), measurableAdsViewed = integer(0), 
        adsPagesViewed = integer(0), adsClicked = integer(0), 
        adsRevenue = integer(0), dfpAdGroup = character(0), dfpAdUnits = character(0), 
        dfpNetworkId = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")), structure(list(hitNumber = 1L, time = 0L, 
    hour = 12L, minute = 43L, isSecure = NA, isInteraction = TRUE, 
    isEntrance = TRUE, isExit = TRUE, referer = "https://www.google.com/", 
    page = list(list(pagePath = "/google+redesign/apparel/mens/mens+outerwear", 
        hostname = "shop.googlemerchandisestore.com", pageTitle = "Men's Outerwear | Apparel | Google Merchandise Store", 
        searchKeyword = NA_character_, searchCategory = NA_character_, 
        pagePathLevel1 = "/google+redesign/", pagePathLevel2 = "/apparel/", 
        pagePathLevel3 = "/mens/", pagePathLevel4 = "/mens+outerwear")), 
    transaction = list(list(transactionId = NA_character_, transactionRevenue = NA_integer_, 
        transactionTax = NA_integer_, transactionShipping = NA_integer_, 
        affiliation = NA_character_, currencyCode = "USD", localTransactionRevenue = NA_integer_, 
        localTransactionTax = NA_integer_, localTransactionShipping = NA_integer_, 
        transactionCoupon = NA_character_)), item = list(list(
        transactionId = NA_character_, productName = NA_character_, 
        productCategory = NA_character_, productSku = NA_character_, 
        itemQuantity = NA_integer_, itemRevenue = NA_integer_, 
        currencyCode = "USD", localItemRevenue = NA_integer_)), 
    contentInfo = list(structure(list(NULL), .Names = "")), appInfo = list(
        list(name = NA_character_, version = NA_character_, id = NA_character_, 
            installerId = NA_character_, appInstallerId = NA_character_, 
            appName = NA_character_, appVersion = NA_character_, 
            appId = NA_character_, screenName = "shop.googlemerchandisestore.com/google+redesign/apparel/mens/mens+outerwear", 
            landingScreenName = "shop.googlemerchandisestore.com/google+redesign/apparel/mens/mens+outerwear", 
            exitScreenName = "shop.googlemerchandisestore.com/google+redesign/apparel/mens/mens+outerwear", 
            screenDepth = "0")), exceptionInfo = list(list(description = NA_character_, 
        isFatal = TRUE, exceptions = NA_integer_, fatalExceptions = NA_integer_)), 
    eventInfo = list(structure(list(NULL, NULL, NULL, NULL), .Names = c("", 
    "", "", ""))), product = list(structure(list(productSKU = c("GGOEGAAX0313", 
    "GGOEGAAX0358", "GGOEGAAX0568", "GGOEGAAX0592", "GGOEGAAX0593", 
    "GGOEGAAX0598", "GGOEGAAX0595", "GGOEGAAX0596", "GGOEGAAX0569", 
    "GGOEGAAX0567", "GGOEGAAX0731"), v2ProductName = c("Google Tri-blend Hoodie Grey", 
    "Google Men's  Zip Hoodie", "Google Men's Watershed Full Zip Hoodie Grey", 
    "Google Men's Airflow 1/4 Zip Pullover Black", "Google Men's Airflow 1/4 Zip Pullover Lapis", 
    "Google Men's Convertible Vest-Jacket Pewter", "Google Men's Microfiber 1/4 Zip Pullover Blue/Indigo", 
    "Google Men's Quilted Insulated Vest Black", "Google Men's Performance Full Zip Jacket Black", 
    "Google Men's Softshell Jacket Black/Grey", "YouTube Men's Fleece Hoodie Black"
    ), v2ProductCategory = c("Home/Apparel/Men's/Men's-Outerwear/", 
    "Home/Apparel/Men's/Men's-Outerwear/", "Home/Apparel/Men's/Men's-Outerwear/", 
    "Home/Apparel/Men's/Men's-Outerwear/", "Home/Apparel/Men's/Men's-Outerwear/", 
    "Home/Apparel/Men's/Men's-Outerwear/", "Home/Apparel/Men's/Men's-Outerwear/", 
    "Home/Apparel/Men's/Men's-Outerwear/", "Home/Apparel/Men's/Men's-Outerwear/", 
    "Home/Apparel/Men's/Men's-Outerwear/", "Home/Apparel/Men's/Men's-Outerwear/"
    ), productVariant = c("(not set)", "(not set)", "(not set)", 
    "(not set)", "(not set)", "(not set)", "(not set)", "(not set)", 
    "(not set)", "(not set)", "(not set)"), productBrand = c("(not set)", 
    "(not set)", "(not set)", "(not set)", "(not set)", "(not set)", 
    "(not set)", "(not set)", "(not set)", "(not set)", "(not set)"
    ), productRevenue = c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_), localProductRevenue = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), productPrice = c(39990000L, 55990000L, 109990000L, 69990000L, 
    69990000L, 98990000L, 74990000L, 74990000L, 119990000L, 98990000L, 
    55990000L), localProductPrice = c(39990000L, 55990000L, 109990000L, 
    69990000L, 69990000L, 98990000L, 74990000L, 74990000L, 119990000L, 
    98990000L, 55990000L), productQuantity = c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), productRefundAmount = c(NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
    ), localProductRefundAmount = c(NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_), isImpression = c(TRUE, 
    TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE
    ), isClick = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
    ), customDimensions = list(structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")), structure(list(index = integer(0), 
        value = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame"))), customMetrics = list(structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame")), structure(list(
        index = integer(0), value = integer(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame"))), productListName = c("Category", 
    "Category", "Category", "Category", "Category", "Category", 
    "Category", "Category", "Category", "Category", "Category"
    ), productListPosition = 1:11, productCouponCode = c(NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_, NA_character_, NA_character_, 
    NA_character_, NA_character_)), row.names = c(NA, -11L), class = c("tbl_df", 
    "tbl", "data.frame"))), promotion = list(structure(list(promoId = character(0), 
        promoName = character(0), promoCreative = character(0), 
        promoPosition = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame"))), promotionActionInfo = list(structure(list(
        NULL, NULL), .Names = c("", ""))), refund = list(structure(list(
        NULL, NULL), .Names = c("", ""))), eCommerceAction = list(
        list(action_type = "0", step = 1L, option = NA_character_)), 
    experiment = list(structure(list(experimentId = character(0), 
        experimentVariant = character(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame"))), publisher = list(
        structure(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, 
            NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
            NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
            NULL, NULL, NULL), .Names = c("", "", "", "", "", 
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
        "", "", "", "", "", "", "", "", "", "", "", "", ""))), 
    customVariables = list(structure(list(index = integer(0), 
        customVarName = character(0), customVarValue = character(0)), row.names = c(NA, 
    0L), class = c("tbl_df", "tbl", "data.frame"))), customDimensions = list(
        structure(list(index = integer(0), value = character(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame"))), customMetrics = list(
        structure(list(index = integer(0), value = integer(0)), row.names = c(NA, 
        0L), class = c("tbl_df", "tbl", "data.frame"))), type = "PAGE", 
    social = list(list(socialInteractionNetwork = NA_character_, 
        socialInteractionAction = NA_character_, socialInteractions = NA_integer_, 
        socialInteractionTarget = NA_character_, socialNetwork = "(not set)", 
        uniqueSocialInteractions = NA_integer_, hasSocialSourceReferral = "No", 
        socialInteractionNetworkAction = " : ")), latencyTracking = list(
        structure(list(NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
            NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL), .Names = c("", 
        "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
        ""))), sourcePropertyInfo = list(structure(list(NULL, 
        NULL), .Names = c("", ""))), contentGroup = list(list(
        contentGroup1 = "(not set)", contentGroup2 = "Apparel", 
        contentGroup3 = "(not set)", contentGroup4 = "(not set)", 
        contentGroup5 = "(not set)", previousContentGroup1 = "(entrance)", 
        previousContentGroup2 = "(entrance)", previousContentGroup3 = "(entrance)", 
        previousContentGroup4 = "(entrance)", previousContentGroup5 = "(entrance)", 
        contentGroupUniqueViews1 = NA_integer_, contentGroupUniqueViews2 = 1L, 
        contentGroupUniqueViews3 = NA_integer_, contentGroupUniqueViews4 = NA_integer_, 
        contentGroupUniqueViews5 = NA_integer_)), dataSource = "web", 
    publisher_infos = list(structure(list(dfpClicks = integer(0), 
        dfpImpressions = integer(0), dfpMatchedQueries = integer(0), 
        dfpMeasurableImpressions = integer(0), dfpQueries = integer(0), 
        dfpRevenueCpm = integer(0), dfpRevenueCpc = integer(0), 
        dfpViewableImpressions = integer(0), dfpPagesViewed = integer(0), 
        adsenseBackfillDfpClicks = integer(0), adsenseBackfillDfpImpressions = integer(0), 
        adsenseBackfillDfpMatchedQueries = integer(0), adsenseBackfillDfpMeasurableImpressions = integer(0), 
        adsenseBackfillDfpQueries = integer(0), adsenseBackfillDfpRevenueCpm = integer(0), 
        adsenseBackfillDfpRevenueCpc = integer(0), adsenseBackfillDfpViewableImpressions = integer(0), 
        adsenseBackfillDfpPagesViewed = integer(0), adxBackfillDfpClicks = integer(0), 
        adxBackfillDfpImpressions = integer(0), adxBackfillDfpMatchedQueries = integer(0), 
        adxBackfillDfpMeasurableImpressions = integer(0), adxBackfillDfpQueries = integer(0), 
        adxBackfillDfpRevenueCpm = integer(0), adxBackfillDfpRevenueCpc = integer(0), 
        adxBackfillDfpViewableImpressions = integer(0), adxBackfillDfpPagesViewed = integer(0), 
        adxClicks = integer(0), adxImpressions = integer(0), 
        adxMatchedQueries = integer(0), adxMeasurableImpressions = integer(0), 
        adxQueries = integer(0), adxRevenue = integer(0), adxViewableImpressions = integer(0), 
        adxPagesViewed = integer(0), adsViewed = integer(0), 
        adsUnitsViewed = integer(0), adsUnitsMatched = integer(0), 
        viewableAdsViewed = integer(0), measurableAdsViewed = integer(0), 
        adsPagesViewed = integer(0), adsClicked = integer(0), 
        adsRevenue = integer(0), dfpAdGroup = character(0), dfpAdUnits = character(0), 
        dfpNetworkId = character(0)), row.names = c(NA, 0L), class = c("tbl_df", 
    "tbl", "data.frame")))), row.names = c(NA, -1L), class = c("tbl_df", 
"tbl", "data.frame")))), row.names = c(NA, -2L), class = c("tbl_df", 
"tbl", "data.frame"))

这可行吗?

【问题讨论】:

【参考方案1】:

请注意,unnest_wider() 在嵌套方面不可交换:从内向外应用命令将给出与从外向内不同的答案。例如,

# Create a nested data frame
df1 <- tibble( gr = c('a', 'b', 'c'), values = list(1:2, 3:4, 5:6) )
df2 <- tibble( dfi = c(1,2), dfs = list(df1, df1) )

# Apply unnest_wider on the outer layer, then inner layer
df2 %>% unnest_wider( dfs ) %>% unnest_wider( values )
# # A tibble: 2 x 5
#     dfi gr        ...1      ...2      ...3     
#   <dbl> <list>    <list>    <list>    <list>   
# 1     1 <chr [3]> <dbl [2]> <dbl [2]> <dbl [2]>
# 2     2 <chr [3]> <dbl [2]> <dbl [2]> <dbl [2]>

# Apply unner_wider to each inner data frame, then to the entire thing 
df2 %>% mutate_at("dfs", map, unnest_wider, "values") %>% unnest_wider(dfs)
# # A tibble: 2 x 4
#     dfi gr        ...1      ...2     
#   <dbl> <list>    <list>    <list>   
# 1     1 <chr [3]> <int [3]> <int [3]>
# 2     2 <chr [3]> <int [3]> <int [3]>

普通unnest()没有这个问题:

x1 <- df2 %>% unnest(dfs) %>% unnest(values)
x2 <- df2 %>% mutate_at("dfs", map, unnest, "values") %>% unnest(dfs)
identical( x1, x2 )      # TRUE

因此,我的建议是递归应用标准unnest(),直到所有嵌套列都在同一级别。然后,您可以使用 tidyr::spread() 跟进以根据需要扩大结果。

unnest_all <- function( .df )

  lc <- purrr::keep(.df, is.list) %>% names
  if( length(lc) == 0 ) return(.df)
  tidyr::unnest( .df, lc ) %>% unnest_all()


# Example
unnest_all(df2)
# # A tibble: 12 x 3
#      dfi gr    values
#    <dbl> <chr>  <dbl>
#  1     1 a          1
#  2     1 a          2
#  3     1 b          3
# ...

您的示例数据集的内部结构似乎存在一些问题(可能是由于 NA),这会导致取消嵌套问题,即使是手动操作也是如此:

X <- structure(...)   # As in the question
X %>% unnest(hits) %>% unnest(transaction) %>% unnest(transaction)
# Error: No common type for `..1$transaction$transaction` <character> and `..2$transaction$transaction` <integer>.

【讨论】:

以上是关于递归地将`unnest_wider()`应用于所有列,直到没有更多的嵌套列表列?的主要内容,如果未能解决你的问题,请参考以下文章

递归地将文件添加到所有目录

递归地将所有文件合并到一个文件夹中

python 递归地将嵌套的dicts转换为嵌套的namedtuples,为您提供类似于不可变对象文字的东西

递归地将所有第二层 mp3 转换为第三层并更新 ID3

Laravel - 如何递归地将 API 资源转换为数组?

递归地将整个文件夹添加到存储库