I have a list of character vectors in R that represents sets of cooccuring words. From this, I would like to extract a character vector capturing all the words that appear in the list of character vectors. I think I know how to efficiently go from a character vector of words to a unique character vector of the words that appeared. What I don't know how to do is efficiently collapse the list of character vectors into a single character vector. Any tips on how to approach this or the overall problem efficiently would be great appreciated!
To find unique values in a column in a data frame, use the unique() function in R. In Exploratory Data Analysis, the unique() function is crucial since it detects and eliminates duplicate values in the data.
Character/string – each element in the vector is a string of one or more characters. Built in character vectors are letters and LETTERS which provide the 26 lower (and upper) case letters, respecitively. > y = c("a", "bc", "def")
In this example, we used the paste() function to collapse the elements of a single character vector. paste() can also be used to join the elements of multiple character vectors.
Use unlist()
:
> x <- list(l1=c("a","b","c"), l2=c("b","d"))
> unlist(x)
l11 l12 l13 l21 l22
"a" "b" "c" "b" "d"
And to get the unique values, just use unique
:
> unique(unlist(x))
[1] "a" "b" "c" "d"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With