我希望将参数列表作为向量传递给R中的另一个命令。我不想每次都重复相同的参数集。
对于每行$full_text
的数据帧的t1 to t6
列,这是我必须运行6次的代码。
library(quanteda)
t1t <- tokens(t1$full_text, what = 'word', remove_numbers = TRUE,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_separators = TRUE,
remove_twitter = TRUE,
remove_hyphens = TRUE,
remove_url = TRUE)
t1t <- tokens_tolower(t1t)
t1t <- tokens_select(t1t, stopwords(), selection = "remove")
t1t <- unlist(t1t)
t1t <- unique(t1t)
t1t <- as.data.frame(t1t)
t1t <- as.data.frame.matrix(t1t)
有没有一种方法可以传递一次性参数。
答案 0 :(得分:0)
如错误消息tokens
中所述,期望字符向量,语料库或标记作为输入。您正在向其传递数据帧。而是将相应的文本列传递给它。
tokens
也可以处理向量,因此您可以将多个列作为一个向量一起传递。
library(quanteda)
tokens(c(t1$colname, t2$colname, t3$colname), what = "word", remove_numbers = TRUE,
remove_punct = TRUE, remove_symbols = TRUE, remove_separators = TRUE,
remove_twitter = TRUE, remove_hyphens =TRUE, remove_url = TRUE)
基于更新并以?tokens
的帮助页面为例
t1 <- data.frame(full_text = "#textanalysis is MY <3 4U @myhandle gr8 #stuff :-)",
stringsAsFactors = FALSE)
t2 <- data.frame(full_text = c("This is $10 in 999 different ways,\n up and down;
left and right!", "@kenbenoit working: on #quanteda 2day\t4ever,
http://textasdata.com?page=123."), stringsAsFactors = FALSE)
我们可以创建一个函数以将其应用于所有数据框
complete_function <- function(x) {
t1t <- tokens(x, what = 'word', remove_numbers = TRUE,
remove_punct = TRUE,
remove_symbols = TRUE,
remove_separators = TRUE,
remove_twitter = TRUE,
remove_hyphens = TRUE,
remove_url = TRUE)
t1t <- tokens_tolower(t1t)
t1t <- tokens_select(t1t, stopwords(), selection = "remove")
t1t <- unlist(t1t)
t1t <- unique(t1t)
t1t <- as.data.frame(t1t)
t1t <- as.data.frame.matrix(t1t)
}
然后使用mget
获取数据帧t1
,t2
,t3
等,然后将该函数应用于每个数据帧的"full_text"
列。
lapply(mget(ls(pattern = "^t\\d+")), function(x) complete_function(x$full_text))
#$t1
# t1t
#1 textanalysis
#2 4u
#3 myhandle
#4 gr8
#5 stuff
#$t2
# t1t
#1 different
#2 ways
#3 left
#4 right
#5 kenbenoit
#6 working
#7 quanteda
#8 2day
#9 4ever