r - 从列中合并数据框
问题描述
我有data.frame
两个变量:ID
并且Text
我正在使用以下文本分析命令,该命令data.frame
输出 48 列。
analysis <- textstat_readability(mydata$text, measure = c("all"), remove_hyphens = TRUE)
如何将这 48 列结果添加为单独的列mydata
?
目前我正在使用以下内容:
analysis <- cbind(mydata$ID[1:100000], textstat_readability(mydata$text[1:100000], measure = c("all"), remove_hyphens = TRUE))
但它需要永远完成。
解决方案
不知道为什么你的方法需要永远完成老实说,但我认为正确的方法如下:
# (0.) Load the package and make a random sample dataset (usually this should be
# provided in the question, just saying):
library(quanteda)
mydata <- data.frame(ID = 1:100,
text = stringi::stri_rand_strings(
n = 100,
length = runif(100, min=1, max=100),
pattern = "[A-Za-z0-9]"),
stringsAsFactors = FALSE)
# 1. Make a quanteda corpus, where the ID is stored alongside the text column:
mydata_corpus <- corpus(mydata, docid_field = "ID", text_field = "text")
# 2. Then run the readability command:
`analysis <- textstat_readability(mydata_corpus, measure = c("all"), remove_hyphens = TRUE)`
# 3. Now you can either keep this, or merge it with your original set based on
# IDs:
mydata_analysis <- merge(mydata, analysis, by.x = "ID", by.y = "document")
这应该可以工作,而您根本不必使用cbind()
。
推荐阅读
- python - Django查询集到一个字符串
- python-3.x - 将从 S3 绘制的 PDF 和 PPTX 文件转换为 JPG 格式
- python-3.x - 我应该如何在运行在谷歌云实例上的 jupyter notebook 上安装 spacy 的英文模型?
- python - 多个布尔运算符
- python - 如何从 JSON 的值列表中输出格式化文本?
- javascript - Vue 方法总是被调用(不仅仅是点击)
- spring - 使用 Spring Data 投影新的数组字段
- docker - 如何在 gitlab.com 上使用 .gitlab-ci.yml 从 Dockerfile 构建并将工件从 Dockerfile 复制到 GitLab 公共目录?
- php - 从 Bootstrap 4 中的 nav-link 元素中删除 ACTIVE 类
- python - Linux“at”任务调度程序是否有python模块