Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将数据帧转换为语料库_R_Quanteda - Fatal编程技术网

R 将数据帧转换为语料库

R 将数据帧转换为语料库,r,quanteda,R,Quanteda,我使用[这里]的例子: 我用它来消除一些噪音: `mytext <- paste(unlist(df$text), collapse =" ") mytext2 <- gsub("<code>.+?</code>", "", mytext) cleanFun <- function(htmlString) { return(gsub("<.*?>", "", htmlString)) } mytext3 <- cleanFun(

我使用[这里]的例子:

我用它来消除一些噪音:

`mytext <- paste(unlist(df$text), collapse =" ")
mytext2 <- gsub("<code>.+?</code>", "", mytext)
cleanFun <- function(htmlString) {
    return(gsub("<.*?>", "", htmlString))
}
mytext3 <- cleanFun(mytext2)
df2 <- gsub("\n", "", mytext3)`
但是,该文档未列出,我收到稀疏的0.0%
myDfm不完全确定问题是什么,但是如果您想清理
df
中的文本,然后将其转换为语料库,那么下面是一种方法:

df$text <- gsub("<.*?>", "", df$text)
corp <- corpus(df, text_field = "text")
dfm <- dfm(corp, remove_punct = TRUE, remove = stopwords('en'))
> dfm
Document-feature matrix of: 3 documents, 32 features (62.5% sparse).

df$text为什么要用
粘贴(collapse=“”)
将文本字符串连接成一个字符串?由于df2中的文档数量是一个,dfm当然是0%稀疏的,因为dfm中的每个特征都必须在df2中的单个文档中呈现。@amatsuo_net让df2保持原样是正确的吗?这里的问题是
粘贴(…,collapse=”“)
,它将向量的各个元素粘贴到一个元素中。因此,当您创建dfm时,您会得到一个文档。由于您的所有功能都取自单个文档,因此根据定义,它将是0%稀疏的。要获取单个文档,请不要将元素粘贴在一起。
`mytext <- paste(unlist(df$text), collapse =" ")
mytext2 <- gsub("<code>.+?</code>", "", mytext)
cleanFun <- function(htmlString) {
    return(gsub("<.*?>", "", htmlString))
}
mytext3 <- cleanFun(mytext2)
df2 <- gsub("\n", "", mytext3)`
df$text <- gsub("<.*?>", "", df$text)
corp <- corpus(df, text_field = "text")
dfm <- dfm(corp, remove_punct = TRUE, remove = stopwords('en'))
> dfm
Document-feature matrix of: 3 documents, 32 features (62.5% sparse).