Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying unique terms from list of character vectors

Tags:

r

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!

like image 326
Chris Avatar asked Feb 08 '10 19:02

Chris


People also ask

How do I get unique values in a list in R?

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.

What is a character vector?

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")

Which function joins elements of a character vector?

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.


1 Answers

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"
like image 76
Shane Avatar answered Oct 02 '22 00:10

Shane