r - 如何将命名数字列表转换为具有基于列表名称的 id 的 data.frame?
问题描述
我正在尝试将命名数字列表作为 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
我已经尝试过unlist()
这些数据,并且我知道有一种简单的方法可以做到这一点。也许在 data.table 或 dplyr 中。但我总是得到有趣的结果。
所需的形式是:
dat1 <- data.frame(ngram = 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"),
freq = c( 816, 609, 427, 426, 426, 419, 390, 353, 326, 301),
text = c("Blogs"))
dat2 <- data.frame(ngram = 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"),
freq = c(96, 95, 94, 74, 66, 59, 51, 50, 43, 42),
text = "News")
dat3 <- data.frame(ngram = 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"),
freq = c(2219, 1742, 1689, 1590, 995, 823, 587, 562, 554, 535),
text = "Twitter")
dat <- rbind(dat1,dat2,dat3)
dat
解决方案
也许
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
仍然需要重命名两个变量,但我认为这非常接近?
推荐阅读
- javascript - 提取 JS 数组出现次数的重复值
- azure - Azure DevOps:分支创建策略
- spring-boot - 在我的 Spring Boot 应用程序中,如何调试/判断为什么会出现 302 错误?
- asp.net - PrincipalContext.ValidateCredentials 究竟是如何工作的?
- html - 在使用 React-hook-form 进行验证的情况下,标签转换不起作用
- javascript - 如何根据键值项列表更新现有的嵌套对象属性
- c# - 如何删除从ckeditor下载的word文档中的htmltags
- vue.js - 是否有可能获得引用的元素,即。this.$ref 在 fullCalendar 中的 dateClick 函数内?
- r - 注释绘图边缘而不更改绘图限制或将“扩展”设置为 0
- wordpress - 在 wordpress 和 pagebuilders 上缓存