I wish to pass a list of arguments as a vector to another command in R. I do not want to repeat the same set of arguments every time.
This is the code that I have to run 6 times for each $full_text
column of data frames ranging t1 to t6
.
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)
Is there a way to pass a one-time argument.
As mentioned in the error message tokens
expect character vector, corpus or tokens as input. You are passing a dataframe to it. Pass the respective column of text to it instead.
Also tokens
can process vectors so you can pass multiple columns together as one vector.
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)
Based on the update and taking an example from the help page of ?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)
We can create a function to apply it to all dataframes
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)
}
Then use mget
to get dataframes t1
, t2
, t3
etc and apply the function to "full_text"
column of each dataframe.
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