Re: [R] Accessing the elements of a list
Shubha Vishwanath Karanth wrote: > Hi R, > > > > I wanted to know how do we access the elements of a list. In particular, > > > > v=list(c(1,2,3,4,5),c(1,2,33,4,5),c(1,2,333,4,5),c(1,2,,4,5)) > > > > I want to access all the thirds items of the elements of the list. i.e., > I want to access the elements, 3,33,333,. This can be done through > sapply as: > > > > sapply(v,function(x) x[3]) > > > > But I need to access this without using 'sapply'. Like in a data frame, > we do df[2,3] etc Any ideas? > > Hi, not the most elegant, but this works: cmd > unlist(as.data.frame(v)[3,]) c.1..2..3..4..5. c.1..2..33..4..5. c.1..2..333..4..5. c.1..2....4..5. 3 33 333 Cheers, Thibaut. -- ## Thibaut JOMBART CNRS UMR 5558 - Laboratoire de Biométrie et Biologie Evolutive Universite Lyon 1 43 bd du 11 novembre 1918 69622 Villeurbanne Cedex Tél. : 04.72.43.29.35 Fax : 04.72.43.13.88 [EMAIL PROTECTED] http://lbbe.univ-lyon1.fr/-Jombart-Thibaut-.html?lang=en http://pbil.univ-lyon1.fr/software/adegenet/ __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Accessing the elements of a list
I find it hard to imagine you need to do it without using sapply. If you get over your phobia, you might like to try sapply(v, "[", 3) If the elements of the list are all the same length, as in your example, you could do data.frame(v)[3, ] I think we need to know more about why you think you can't use sapply before we can really help you, though. Bill Venables. -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Shubha Vishwanath Karanth Sent: Friday, 1 February 2008 7:05 PM To: [EMAIL PROTECTED] Subject: [R] Accessing the elements of a list Hi R, I wanted to know how do we access the elements of a list. In particular, v=list(c(1,2,3,4,5),c(1,2,33,4,5),c(1,2,333,4,5),c(1,2,,4,5)) I want to access all the thirds items of the elements of the list. i.e., I want to access the elements, 3,33,333,. This can be done through sapply as: sapply(v,function(x) x[3]) But I need to access this without using 'sapply'. Like in a data frame, we do df[2,3] etc Any ideas? BR, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Accessing the elements of a list
Hi R, I wanted to know how do we access the elements of a list. In particular, v=list(c(1,2,3,4,5),c(1,2,33,4,5),c(1,2,333,4,5),c(1,2,,4,5)) I want to access all the thirds items of the elements of the list. i.e., I want to access the elements, 3,33,333,. This can be done through sapply as: sapply(v,function(x) x[3]) But I need to access this without using 'sapply'. Like in a data frame, we do df[2,3] etc Any ideas? BR, Shubha This e-mail may contain confidential and/or privileged i...{{dropped:13}} __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.