[R] conversion of a list element to a vector

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue May 20 13:47:15 CEST 2003

Philippe Hupé wrote:
> Hello,
> I would like to extract unique elements of a variable which belongs to a 
> list
> liste <- list(V1=c(1,2,3,5,2), V2=c(1,2,3,4,5))
> var <- "V1"
> uni <- unique(liste[var]) #does not work

Need to use "[[" here. liste[var] is still a list. liste[[var]] is a 
vector. I would avoid using "var" as a variable name since it is also 
the function for computing the variance.

> I know that liste[var]$V1 works but for my problem, the label variable 
> "V1" is only know through the var variable.
> I can I do
> Thanks in advance.

lapply(liste, unique) returns a list of length(liste) containing the 
unique values of each element in the list.


More information about the R-help mailing list