如何将命名数字列表转换为基于列表名称的ID为的data.frame?

如何将命名数字列表转换为基于列表名称的ID为的data.frame?,r,list,R,List,我正在尝试将命名数字列表作为data.frame,以便在ggplot2中更轻松地打印。我的列表如下所示: dat <- list() dat[[1]] <- c( 816, 609, 427, 426, 426, 419, 390, 353, 326, 301) dat[[2]] <- c(96, 95, 94, 74, 66, 59, 51, 50, 43, 42) dat[[3]] <- c(2219, 1742, 1689, 1590, 995, 823, 587,

我正在尝试将命名数字列表作为data.frame,以便在ggplot2中更轻松地打印。我的列表如下所示:

dat <- list()
dat[[1]] <- c( 816, 609, 427, 426, 426, 419, 390, 353, 326, 301)
dat[[2]] <- c(96, 95, 94, 74, 66, 59, 51, 50, 43, 42)
dat[[3]] <- c(2219, 1742, 1689, 1590, 995, 823, 587, 562, 554, 535)
names(dat[[1]]) <-
    c("new york city", "new york times", "amazon services llc", "services llc amazon",
      "llc amazon eu", "couple weeks ago", "incorporated item pp", "two years ago",
      "new york n.y", "world war ii")
names(dat[[2]]) <-
    c("new york city", "president barack obama", "two years ago" ,
      "st louis county",     "gov chris christie", "first time since" ,
      "world war ii", "three years ago", "new york times", "four years ago")
names(dat[[3]]) <-
    c("let us know", "happy mothers day", "happy new year",
      "happy mother's day", "cinco de mayo", "looking forward seeing",
      "just got back", "keep good work", "come see us", "love love love")
names(dat) <- c("blogs","news","twitter")

dat
也许吧

purrr::map\u dfr(.x=dat,tibble::enframe,.id=“text”)
#一个tibble:30x3
文本名称值
1.纽约市816
2博客《纽约时报》609
3博客亚马逊服务有限责任公司427
4博客服务有限责任公司亚马逊426
5博客有限责任公司亚马逊欧盟426
6个博客几周前419
7个博客合并项目第390页
两年前的8个博客353
纽约州9博客326
10博客二战301
# ... 还有20行

仍然需要重命名两个变量,但我认为这非常接近?

使用
do.call
gather
的解决方案:

library(tidyverse)
do.call(cbind, dat) %>% as.data.frame() %>% rownames_to_column("ngram") %>%
  gather(text, freq, - ngram)


#                   ngram    text  freq
# 1         new york city   blogs   816
# 2        new york times   blogs   609
# 3   amazon services llc   blogs   427
# 4   services llc amazon   blogs   426
# 5         llc amazon eu   blogs   426
# 6      couple weeks ago   blogs   419
# 7  incorporated item pp   blogs   390
# 8         two years ago   blogs   353
# 9          new york n.y   blogs   326
# 10         world war ii   blogs   301
# 11        new york city    news    96
# 12       new york times    news    95
# 13  amazon services llc    news    94
# 14  services llc amazon    news    74
# 15        llc amazon eu    news    66
# 16     couple weeks ago    news    59
# 17 incorporated item pp    news    51
# 18        two years ago    news    50
# 19         new york n.y    news    43
# 20         world war ii    news    42
# 21        new york city twitter  2219
# 22       new york times twitter  1742
# 23  amazon services llc twitter  1689
# 24  services llc amazon twitter  1590
# 25        llc amazon eu twitter   995
# 26     couple weeks ago twitter   823
# 27 incorporated item pp twitter   587
# 28        two years ago twitter   562
# 29         new york n.y twitter   554
# 30         world war ii twitter   535
map\u-dfr(dat,tibble::enframe,name=“news”,value=“twitter”,“.id=“text”)
为一体式解决方案。
purrr::map_dfr(.x = dat,tibble::enframe,.id = "text")

# A tibble: 30 x 3
    text                 name value
   <chr>                <chr> <dbl>
 1 blogs        new york city   816
 2 blogs       new york times   609
 3 blogs  amazon services llc   427
 4 blogs  services llc amazon   426
 5 blogs        llc amazon eu   426
 6 blogs     couple weeks ago   419
 7 blogs incorporated item pp   390
 8 blogs        two years ago   353
 9 blogs         new york n.y   326
10 blogs         world war ii   301
# ... with 20 more rows
library(tidyverse)
do.call(cbind, dat) %>% as.data.frame() %>% rownames_to_column("ngram") %>%
  gather(text, freq, - ngram)


#                   ngram    text  freq
# 1         new york city   blogs   816
# 2        new york times   blogs   609
# 3   amazon services llc   blogs   427
# 4   services llc amazon   blogs   426
# 5         llc amazon eu   blogs   426
# 6      couple weeks ago   blogs   419
# 7  incorporated item pp   blogs   390
# 8         two years ago   blogs   353
# 9          new york n.y   blogs   326
# 10         world war ii   blogs   301
# 11        new york city    news    96
# 12       new york times    news    95
# 13  amazon services llc    news    94
# 14  services llc amazon    news    74
# 15        llc amazon eu    news    66
# 16     couple weeks ago    news    59
# 17 incorporated item pp    news    51
# 18        two years ago    news    50
# 19         new york n.y    news    43
# 20         world war ii    news    42
# 21        new york city twitter  2219
# 22       new york times twitter  1742
# 23  amazon services llc twitter  1689
# 24  services llc amazon twitter  1590
# 25        llc amazon eu twitter   995
# 26     couple weeks ago twitter   823
# 27 incorporated item pp twitter   587
# 28        two years ago twitter   562
# 29         new york n.y twitter   554
# 30         world war ii twitter   535