Extracting a particular column from list

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Extracting a particular column from list

R help mailing list-2
Hi. How to extract a column from the list.. I will be thanks full.. 

Sent from Yahoo Mail on Android
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a particular column from list

Eric Berger
> 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 <[hidden email]>
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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Fwd: Extracting a particular column from list

Eric Berger
In reply to this post by R help mailing list-2
[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 <[hidden email]>
Date: Thu, Jan 16, 2020 at 1:31 PM
Subject: Re: [R] Extracting a particular column from list
To: [hidden email] <[hidden email]>


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
<[hidden email]> 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 <[hidden email]>
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]]

______________________________________________
[hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a particular column from list

Rui Barradas
In reply to this post by R help mailing list-2
Hello,

What column and what list?
Please post a reproducible example, see the link at the bottom of this
mail and  [1], [2], [3].

[1] https://cran.r-project.org/web/packages/reprex/index.html
[2]
https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
[3] https://stackoverflow.com/help/mcve

Hope this helps,

Rui Barradas


Às 11:02 de 16/01/20, Faheem Jan via R-help escreveu:

> Hi. How to extract a column from the list.. I will be thanks full..
>
> Sent from Yahoo Mail on Android
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a particular column from list

Rolf Turner

On 17/01/20 1:55 am, Rui Barradas wrote:

> Hello,
>
> What column and what list?
> Please post a reproducible example, see the link at the bottom of this
> mail and  [1], [2], [3].
>
> [1] https://cran.r-project.org/web/packages/reprex/index.html
> [2]
> https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example 
>
> [3] https://stackoverflow.com/help/mcve
>
> Hope this helps,

The OP should note that lists in general *do not have* columns.  Data
frames (which are a special case of lists) do have columns.

Lists have "*entries*" or "components".  It is important to get your
terminology right and to understand the concepts that you are dealing
with.  Slap-dash hammer and hope is a recipe for disaster, especially
in R.

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a particular column from list

mark leeds
I nominate the last sentence of Rolf's comment as a fortune.


On Thu, Jan 16, 2020 at 3:48 PM Rolf Turner <[hidden email]> wrote:

>
> On 17/01/20 1:55 am, Rui Barradas wrote:
>
> > Hello,
> >
> > What column and what list?
> > Please post a reproducible example, see the link at the bottom of this
> > mail and  [1], [2], [3].
> >
> > [1] https://cran.r-project.org/web/packages/reprex/index.html
> > [2]
> >
> https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
> >
> > [3] https://stackoverflow.com/help/mcve
> >
> > Hope this helps,
>
> The OP should note that lists in general *do not have* columns.  Data
> frames (which are a special case of lists) do have columns.
>
> Lists have "*entries*" or "components".  It is important to get your
> terminology right and to understand the concepts that you are dealing
> with.  Slap-dash hammer and hope is a recipe for disaster, especially
> in R.
>
> cheers,
>
> Rolf Turner
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a particular column from list

Rui Barradas
In reply to this post by Rolf Turner
Hello,

Inline.


Às 22:03 de 16/01/20, Mark Leeds escreveu:
> I nominate the last sentence of Rolf's comment as a fortune.

Second.


Rui Barradas

>
>
> On Thu, Jan 16, 2020 at 3:48 PM Rolf Turner <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>
>     On 17/01/20 1:55 am, Rui Barradas wrote:
>
>      > Hello,
>      >
>      > What column and what list?
>      > Please post a reproducible example, see the link at the bottom of
>     this
>      > mail and  [1], [2], [3].
>      >
>      > [1] https://cran.r-project.org/web/packages/reprex/index.html
>      > [2]
>      >
>     https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
>
>      >
>      > [3] https://stackoverflow.com/help/mcve
>      >
>      > Hope this helps,
>
>     The OP should note that lists in general *do not have* columns.  Data
>     frames (which are a special case of lists) do have columns.
>
>     Lists have "*entries*" or "components".  It is important to get your
>     terminology right and to understand the concepts that you are dealing
>     with.  Slap-dash hammer and hope is a recipe for disaster, especially
>     in R.
>
>     cheers,
>
>     Rolf Turner
>
>     --
>     Honorary Research Fellow
>     Department of Statistics
>     University of Auckland
>     Phone: +64-9-373-7599 ext. 88276
>
>     ______________________________________________
>     [hidden email] <mailto:[hidden email]> 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extracting a particular column from list

Jim Lemon-4
Good one. When I saw this thread beginning to lengthen, I thought:

"Ask an unanswerable question and you get philosophy."

Jim

On Fri, Jan 17, 2020 at 6:10 PM Rui Barradas <[hidden email]> wrote:

>
> Hello,
>
> Inline.
>
>
> Às 22:03 de 16/01/20, Mark Leeds escreveu:
> > I nominate the last sentence of Rolf's comment as a fortune.
>
> Second.
>
>
> Rui Barradas
>
> >
> >
> > On Thu, Jan 16, 2020 at 3:48 PM Rolf Turner <[hidden email]
> > <mailto:[hidden email]>> wrote:
> >
> >
> >     On 17/01/20 1:55 am, Rui Barradas wrote:
> >
> >      > Hello,
> >      >
> >      > What column and what list?
> >      > Please post a reproducible example, see the link at the bottom of
> >     this
> >      > mail and  [1], [2], [3].
> >      >
> >      > [1] https://cran.r-project.org/web/packages/reprex/index.html
> >      > [2]
> >      >
> >     https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
> >
> >      >
> >      > [3] https://stackoverflow.com/help/mcve
> >      >
> >      > Hope this helps,
> >
> >     The OP should note that lists in general *do not have* columns.  Data
> >     frames (which are a special case of lists) do have columns.
> >
> >     Lists have "*entries*" or "components".  It is important to get your
> >     terminology right and to understand the concepts that you are dealing
> >     with.  Slap-dash hammer and hope is a recipe for disaster, especially
> >     in R.
> >
> >     cheers,
> >
> >     Rolf Turner
> >
> >     --
> >     Honorary Research Fellow
> >     Department of Statistics
> >     University of Auckland
> >     Phone: +64-9-373-7599 ext. 88276
> >
> >     ______________________________________________
> >     [hidden email] <mailto:[hidden email]> 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.
> >
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Extracting a particular column from list

Bert Gunter-2
In reply to this post by Eric Berger
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 <[hidden email]> 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 <[hidden email]>
> Date: Thu, Jan 16, 2020 at 1:31 PM
> Subject: Re: [R] Extracting a particular column from list
> To: [hidden email] <[hidden email]>
>
>
> 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
> <[hidden email]> 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 <
> [hidden email]>
> 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]]
>
> ______________________________________________
> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Extracting a particular column from list

PIKAL Petr
In reply to this post by Eric Berger
Or simply

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

$b
[1] "a"

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Eric Berger
> Sent: Thursday, January 16, 2020 1:36 PM
> To: R mailing list <[hidden email]>
> 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 <[hidden email]>
> Date: Thu, Jan 16, 2020 at 1:31 PM
> Subject: Re: [R] Extracting a particular column from list
> To: [hidden email] <[hidden email]>
>
>
> 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 <[hidden email]> 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@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]]
>
> ______________________________________________
> [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.