如何在保留列表名称和值的同时展平一列列表? [r] [重复]

Posted

技术标签:

【中文标题】如何在保留列表名称和值的同时展平一列列表? [r] [重复]【英文标题】:How to flatten a column of lists while keeping list names and values? [r] [duplicate] 【发布时间】:2020-03-17 22:28:03 【问题描述】:

我已经阅读了 4-5 个类似的问题,但他们的案例不同,我不知道如何将我阅读的内容应用到我的案例中。

我有一个(公共)数据框,其中最后一列是一个列表:

点击放大镜显示如下结构:

当我unnest() 时,我会丢失度量名称(即visitshits 等):

如何在保留指标名称的同时展平该列表?

样本数据:

structure(list(fullVisitorId = c("2248281639583218707", "8647436381089107732", 
"2055839700856389632", "0750846065342433129", "0573427169410921198"
), date = c("20170801", "20170801", "20170801", "20170801", "20170801"
), visitStartTime = c(1501583974L, 1501616585L, 1501583344L, 
1501573386L, 1501651467L), 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), 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), list(visits = 1L, hits = 1L, 
        pageviews = 1L, timeOnSite = NA_integer_, bounces = 1L, 
        transactions = NA_integer_, transactionRevenue = NA_integer_, 
        newVisits = NA_integer_, screenviews = NA_integer_, uniqueScreenviews = NA_integer_, 
        timeOnScreen = NA_integer_, totalTransactionRevenue = NA_integer_, 
        sessionQualityDim = 1L))), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

【问题讨论】:

cbind(df[-4], do.call(rbind.data.frame, df$totals)) 呢? 谢谢@RuiBarradas!这在样本数据上效果很好,但有更快的选择吗?实际数据有数百万行 =\. 【参考方案1】:

tidyr::unnest_wider()是你想要的吗?

tidyr::unnest_wider(sample_data,totals)

# A tibble: 5 x 16
  fullVisitorId date  visitStartTime visits  hits pageviews timeOnSite bounces transactions transactionReve~ newVisits screenviews
  <chr>         <chr>          <int>  <int> <int>     <int>      <int>   <int>        <int>            <int>     <int>       <int>
1 224828163958~ 2017~     1501583974      1     1         1         NA       1           NA               NA         1          NA
2 864743638108~ 2017~     1501616585      1     1         1         NA       1           NA               NA         1          NA
3 205583970085~ 2017~     1501583344      1     1         1         NA       1           NA               NA         1          NA
4 075084606534~ 2017~     1501573386      1     1         1         NA       1           NA               NA         1          NA
5 057342716941~ 2017~     1501651467      1     1         1         NA       1           NA               NA        NA          NA
# ... with 4 more variables: uniqueScreenviews <int>, timeOnScreen <int>, totalTransactionRevenue <int>, sessionQualityDim <int>

【讨论】:

以上是关于如何在保留列表名称和值的同时展平一列列表? [r] [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 R 数据框中取消嵌套列表,同时保留键和值?

如何将 java 映射展平为列表,以便列表交替键和值?

python 展平一系列Excel列,这些列在单元格中包含列表,同时保留行。允许为不可打印的U设置错误级别

在 R 中使用 sapply 时如何保留列表的元素名称?

Python:展平内部列表时保留外部列表

Java:展平列表或对象的列表,保留类