Hi,
I propose here a solution that relies on names of elements:
# From a list, with any names ll=list(v1=1:4,v2=1:2,v3=5:7,v4=9:11,v5=1,v6=rnorm(4)) # Make a copy to be able to change names ll2=ll
names(ll2)=rep("a",lengh(ll2))
# Use unlist, which "autobuilds" names based on # previous names and indexes in # vectors. # Extracts 3 elements when they exists
unlist(ll2)[substring(names(unlist(ll2)),2)=="3"]
Eric
At 13:43 10/11/2003 +1300, Hadley Wickham wrote:
Hi,
I'm trying to subset a list which contains variable length vectors.
What I want to do is extract (eg.) the 3rd item in each vector (with length >= 3). At the moment I'm using sapply(list.of.vectors, function(x) {x[3]}). The problem with this is that sapply returns a list of the same length of list.of.vectors so I end up with a whole lot of null entries from those vectors that aren't long enough. I have a similar problem if I want to select all the vectors where the 3rd item is a specified value.
Does anyone have any better solutions?
Thanks for you help,
Hadley
-------------------------------------------------- L'erreur est certes humaine, mais un vrai d�sastre n�cessite un ou deux ordinateurs. Citation anonyme -------------------------------------------------- Eric Lecoutre Informaticien/Statisticien Institut de Statistique / UCL
TEL (+32)(0)10473050 [EMAIL PROTECTED] URL http://www.stat.ucl.ac.be/ISpersonnel/lecoutre
______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
