[R] List Structure and Acces to data
David Carlson
dcarlson at tamu.edu
Wed Jul 24 21:59:30 CEST 2013
I may not understand, but if you are looking for a way to
extract the second item from each list element, you will need
to use lapply():
a <- list(LETTERS[1:3], 10:12, LETTERS[26:24], 15:13)
b <- list(LETTERS[4:6], 15:17, LETTERS[23:21], 10:8)
c <- list(LETTERS[7:9], 20:22, LETTERS[20:18], 5:3)
abc <- list(a=a, b=b, c=c)
abc
# . . . Output deleted
abc[[1]][[2]]
# [1] 10 11 12
abc[["a"]][[2]]
# [1] 10 11 12
abc[[1:3]][[2]] # Does not work
# Error in abc[[1:3]][[2]] : subscript out of bounds
lapply(abc, "[[", 2)
# $a
# [1] 10 11 12
#
# $b
# [1] 15 16 17
#
# $c
# [1] 20 21 22
# or less cryptically lapply(abc, function(x) x[[2]])
-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352
-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of MacQueen,
Don
Sent: Wednesday, July 24, 2013 2:26 PM
To: Eric TRAVAGLINI; r-help at r-project.org
Subject: Re: [R] List Structure and Acces to data
What you are doing looks like it is probably more complicated
than
necessary, but since I have no idea what structure you intend
to create,
it is hard to say.
Here is a suggestion:
try this:
str(my_echant_hist)
It will give you a summary of the structure of that object,
which should
help you understand how to access its elements.
The expression
my_echant_hist[[1]][[2]]
makes sense if my_enchant_hist is a list, and if its first
element is also
a list. Then you're asking for the second element within the
first element.
There is nothing in R syntax that I know of that gives meaning
to any of
these
my_echant_hist[[]]
my_echant_hist[[:]]
my_echant_hist[[;]]
so I don't know what you were trying to get.
I do suggest studying documentation for syntax related to
using '['. Type
?'['
or
help('[')
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 7/23/13 4:22 AM, "Eric TRAVAGLINI"
<Eric.TRAVAGLINI at crealp.vs.ch> wrote:
>Hello,
>
>I used some other langages as Matlab and i just began with R,
and i need
>help with data structure.
>
>Here is my code
>
>"
>fct_echant_hist <- function ( my_echant, my_vitesse, my_hist,
my_summary)
>{
> list (my_echant = my_echant,
> my_vitesse = my_vitesse,
> my_hist = my_hist,
> my_summary = my_summary)}
>
>
>my_echant_hist <- replicate(dim(pas)[1], fct_echant_hist(0L,
> 0L,
>
matrix(nrow=length(my_breaks),ncol=1),
> matrix(nrow=6,ncol=1)),
> simplify=FALSE)
>"
>Is it possible to acces to all data of my_vitesse ?
>
>
>to acces to the my_vitesse of the first record i write
>my_echant_hist[[1]][[2]]
>
>I try something like that without succes :
>my_echant_hist[[]][[2]] or
>my_echant_hist[[:]][[2]] or
>my_echant_hist[[;]][[2]]
>
>
>Thanks a lot
>
>Eric ./.
>
>P:S Sorry for my English
______________________________________________
R-help at 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.
More information about the R-help
mailing list