Reputation: 449
I was fiddling around in text cleaning when I ran into an interesting occurrence.
Reproducible Code:
trimws(list(c("this is an outrante", " hahaha", " ")))
Output:
[1] "c(\"this is an outrante\", \" hahaha\", \" \")"
I've checked out the trimws documentation and it doesn't go into any specifics besides the fact that it expects a character vector, and in my case, I've supplied with a list of a list of character vectors. I know I can use lapply to easily solve this, but what I want to understand is what is going on with my trimws as is?
Upvotes: 1
Views: 699
Reputation: 887541
The trimws
would be directly applied to vector
and not on a list
.
According to ?trimws
documentation, the usage is
trimws(x, which = c("both", "left", "right"))
where
x- a character vector
It is not clear why the vector
is wrapped in a list
trimws(c("this is an outrante", " hahaha", " "))
If it really needs to be in a list
, then use one of the functions that goes into the list
elements and apply the trimws
lapply(list(c("this is an outrante", " hahaha", " ")), trimws)
Also, note that the OP's list
is a list
of length 1, which can be converted back to a vector
either by [[1]]
or unlist
(more general)
trimws(list(c("this is an outrante", " hahaha", " "))[[1]])
Regarding why a function behaves this, it is supposed to have an input argument as a vector
. The behavior is similar for other functions that expect a vector
, for e.g.
paste(list(c("this is an outrante", " hahaha", " ")))
as.character(list(c("this is an outrante", " hahaha", " ")))
If we check the trimws
function, it is calling regex sub
which requires a vector
mysub <- function(re, x) sub(re, "", x, perl = TRUE)
mysub("^[ \t\r\n]+", list(c("this is an outrante", " hahaha", " ")))
#[1] "c(\"this is an outrante\", \" hahaha\", \" \")"
Pass it a vector
mysub("^[ \t\r\n]+", c("this is an outrante", " hahaha", " "))
#[1] "this is an outrante" "hahaha" ""
Upvotes: 3