[R] Fwd: Extracting a particular column from list

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Mon Jan 20 08:03:29 CET 2020


Or simply

lapply(u, "[", 1)
$a
[1] 1

$b
[1] "a"

Cheers
Petr

> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Eric Berger
> Sent: Thursday, January 16, 2020 1:36 PM
> To: R mailing list <r-help using r-project.org>
> Subject: [R] Fwd: Extracting a particular column from list
> 
> [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_YGro
> wth_AndroidEmailSig__AndroidUsers&af_wl=ym&af_sub1=Internal&af_sub
> 2=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.


More information about the R-help mailing list