[R] Fwd: Extracting a particular column from list

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Sat Jan 18 07:22:23 CET 2020


Trickier, but shorter:

> lapply(u,'[',1)
$a
[1] 1

$b
[1] "a"

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jan 17, 2020 at 10:04 PM Eric Berger <ericjberger using gmail.com> wrote:

> [Putting back onto r-help]
>
> You could try sapply() and lapply().
>
> > e <- sapply( 1:length(u), function(i) u[[i]][1] )
> > e
> # [1] "1" "a"    (note that the integer 1 became a character string "1")
>
> > f <- lapply( 1:length(u), function(i) u[[i]][1] )
> > f
> [[1]]
> [1] 1
>
> [[2]]
> [1] "a"
>
> In this case sapply will try to create a vector, and all its elements will
> need to be of the same type, so you end up with a character vector.
> The second method returns a list, which may or may not be of use to you,
> but each element of the list retains its type.
>
>
>
> ---------- Forwarded message ---------
> From: Faheem Jan <faheemjan93 using yahoo.com>
> Date: Thu, Jan 16, 2020 at 1:31 PM
> Subject: Re: [R] Extracting a particular column from list
> To: ericjberger using gmail.com <ericjberger using gmail.com>
>
>
> In my problem i want to extract the first value of a and b and so on... So
> using such data for further analysis
>
> Sent from Yahoo Mail on Android
> <
> https://go.onelink.me/107872968?pid=InProduct&c=Global_Internal_YGrowth_AndroidEmailSig__AndroidUsers&af_wl=ym&af_sub1=Internal&af_sub2=Global_YGrowth&af_sub3=EmailSignature
> >
>
> On Thu, 16 Jan 2020 at 4:24 PM, Eric Berger
> <ericjberger using gmail.com> wrote:
> > u <- list(a=1:5, b=letters[1:3])
> > u
> # $a
> # [1] 1 2 3 4 5
> #
> # $b
> # [1] "a" "b" "c"
> > u[["a"]]
> [1] 1 2 3 4 5
>
>
>
>
>
> On Thu, Jan 16, 2020 at 1:04 PM Faheem Jan via R-help <
> r-help using r-project.org>
> wrote:
>
> Hi. How to extract a column from the list.. I will be thanks full..
>
> Sent from Yahoo Mail on Android
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using 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]]
>
> ______________________________________________
> R-help using 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