[R] (no subject)

jim holtman jholtman at gmail.com
Sun Oct 4 21:44:51 CEST 2015


You need to break down your expression into parts and see what the data is:

> Empl[c(2,4)]
$family
$family$spouse
[1] "Fred"

$family$children
[1] 3

$family$child.ages
[1] 4 7 9


$family
$family$spouse
[1] "Mary"

$family$children
[1] 2

$family$child.ages
[1] 14 17


> Empl[c(2,4)]$family  #>> notice only picks the first object
$spouse
[1] "Fred"

$children
[1] 3

$child.ages
[1] 4 7 9


To get multiple objects, then you need to use some of the 'apply'
functions; in this case 'sapply':

 > sapply(Empl[c(2,4)], '[[', 'spouse')
family family
"Fred" "Mary"



Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Sun, Oct 4, 2015 at 2:31 PM, FERNANDO MANSITO CABALLERO <
fernando.mansito at gmail.com> wrote:

> Dear Madam/Sir,
>
> I  am   trying to understand  R and I have come to a stumbling block. i
> have written:
>
> >Empl <- list(employee="Anna",family=list(spouse="Fred",children=3,
> +child.ages=c(4,7,9)),employee="John",family=list(spouse="Mary",children=2,
> +child.ages=c(14,17)))
> >Empl[c(2,4)]$family$spouse
> [1] "Fred"
> >#instead of [1] "Fred" "Mary"
>
> Where am I wrong?
>
> Thank you very much for your patience
> Yours truly,
> Fernando Mansito
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list