使用 get() 时无法修改语料库的文档名
Cannot modify docnames of corpus when using get()
我正在尝试通过 for 循环修改 corpus
对象列表的 docnames
。通常,我在循环中移动时使用函数 get()
来访问给定的对象。看来我无法在包 quanteda 的函数 docnames()
中执行此操作。我总是收到此错误(取决于您的输入对象,在我的情况下是 listofcorpora
):
Error in get(listofcorpora[i]) <- `*vtmp*` :
could not find function "get<-"
请在下面找到一个只有两个语料库的最小值。本来我还有很多
library(quanteda)
#> Package version: 2.0.0
#> Parallel computing: 2 of 8 threads used.
#> See https://quanteda.io for tutorials and examples.
#>
#> Attaching package: 'quanteda'
#> The following object is masked from 'package:utils':
#>
#> View
library(stringr)
corp_2015_qtr1 <- corpus( c("The first document of the first corpus.",
"The second document of the first corpus" ) )
corp_2015_qtr2 <- corpus( c("The first document of the second corpus.",
"The second document of the second corpus" ) )
listofcorpora <- objects( pattern = "corp_\d+" )
for ( i in seq_along( listofcorpora ) ) {
current_year <- as.integer( str_extract( listofcorpora[ i ], "\d+" ) ) current_qtr <- as.integer( str_extract( listofcorpora[ i ], "(?<=QTR)\d" ) )
current_docname <- str_c( current_year,
"_qtr_",
current_qtr, "_",
formatC( seq_len( ndoc( get( listofcorpora[ i ] ) ) ),
width = 5, flag = "0" ) )
docnames( get( listofcorpora[ i ] ) ) <- current_docname
}
#> Error in get(listofcorpora[i]) <- `*vtmp*`: could not find function "get<-"
由 reprex package (v0.3.0)
于 2020-04-15 创建
每当我以相同的方式使用 docvars()
时,都会出现相同的错误。
谢谢!
我不知道你从哪里得到 RData
文件,但通常通过 saveRDS
保存对象并用 object <- readRDS
加载它们更有意义,这样你就可以控制对象名称或将文件直接加载到列表中。
在你的情况下,我会通过(正如@phiver 在评论中所建议的那样)将你的对象变成一个列表:
corpora_l <- lapply(listofcorpora, get)
names(corpora_l) <- listofcorpora
为了获得更干净的环境,您现在可以删除多余的对象:
# remove unnecessary objects
rm(list = c(listofcorpora, "listofcorpora"))
在我看来,使用此列表似乎更容易,更重要的是:docnames()
使用列表对象:
for (i in seq_along(corpora_l)) {
current_name <- names(corpora_l)[i]
current_year <- as.integer( str_extract( current_name, "\d+" ) )
current_qtr <- as.integer( str_extract( current_name, "(?<=qtr)\d" ) )
current_docname <- str_c( current_year,
"_qtr_",
current_qtr, "_",
formatC( seq_len( ndoc( corpora_l[[i]] ) ),
width = 5, flag = "0" ) )
docnames( corpora_l[[i]] ) <- current_docname
}
另外:我不知道您对 docnames
的计划是什么,但似乎 year-qtr 更像是一个文档变量。因此,您可以将循环中的最后一行更改为:
docvars(corpora_l[[i]], field = "quarter") <- str_c(current_year,
"_qtr_",
current_qtr)
抱歉,我破坏了你的风格。我不习惯你在代码中留下的空格数量。
我正在尝试通过 for 循环修改 corpus
对象列表的 docnames
。通常,我在循环中移动时使用函数 get()
来访问给定的对象。看来我无法在包 quanteda 的函数 docnames()
中执行此操作。我总是收到此错误(取决于您的输入对象,在我的情况下是 listofcorpora
):
Error in get(listofcorpora[i]) <- `*vtmp*` :
could not find function "get<-"
请在下面找到一个只有两个语料库的最小值。本来我还有很多
library(quanteda)
#> Package version: 2.0.0
#> Parallel computing: 2 of 8 threads used.
#> See https://quanteda.io for tutorials and examples.
#>
#> Attaching package: 'quanteda'
#> The following object is masked from 'package:utils':
#>
#> View
library(stringr)
corp_2015_qtr1 <- corpus( c("The first document of the first corpus.",
"The second document of the first corpus" ) )
corp_2015_qtr2 <- corpus( c("The first document of the second corpus.",
"The second document of the second corpus" ) )
listofcorpora <- objects( pattern = "corp_\d+" )
for ( i in seq_along( listofcorpora ) ) {
current_year <- as.integer( str_extract( listofcorpora[ i ], "\d+" ) ) current_qtr <- as.integer( str_extract( listofcorpora[ i ], "(?<=QTR)\d" ) )
current_docname <- str_c( current_year,
"_qtr_",
current_qtr, "_",
formatC( seq_len( ndoc( get( listofcorpora[ i ] ) ) ),
width = 5, flag = "0" ) )
docnames( get( listofcorpora[ i ] ) ) <- current_docname
}
#> Error in get(listofcorpora[i]) <- `*vtmp*`: could not find function "get<-"
由 reprex package (v0.3.0)
于 2020-04-15 创建每当我以相同的方式使用 docvars()
时,都会出现相同的错误。
谢谢!
我不知道你从哪里得到 RData
文件,但通常通过 saveRDS
保存对象并用 object <- readRDS
加载它们更有意义,这样你就可以控制对象名称或将文件直接加载到列表中。
在你的情况下,我会通过(正如@phiver 在评论中所建议的那样)将你的对象变成一个列表:
corpora_l <- lapply(listofcorpora, get)
names(corpora_l) <- listofcorpora
为了获得更干净的环境,您现在可以删除多余的对象:
# remove unnecessary objects
rm(list = c(listofcorpora, "listofcorpora"))
在我看来,使用此列表似乎更容易,更重要的是:docnames()
使用列表对象:
for (i in seq_along(corpora_l)) {
current_name <- names(corpora_l)[i]
current_year <- as.integer( str_extract( current_name, "\d+" ) )
current_qtr <- as.integer( str_extract( current_name, "(?<=qtr)\d" ) )
current_docname <- str_c( current_year,
"_qtr_",
current_qtr, "_",
formatC( seq_len( ndoc( corpora_l[[i]] ) ),
width = 5, flag = "0" ) )
docnames( corpora_l[[i]] ) <- current_docname
}
另外:我不知道您对 docnames
的计划是什么,但似乎 year-qtr 更像是一个文档变量。因此,您可以将循环中的最后一行更改为:
docvars(corpora_l[[i]], field = "quarter") <- str_c(current_year,
"_qtr_",
current_qtr)
抱歉,我破坏了你的风格。我不习惯你在代码中留下的空格数量。