convert named list with mixed content to data frame

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












Is there a better and nicer way to convert named list with mixed content to data frame?



The working example:



my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

my_df <- data.frame(
"key" = names(my_list),
stringsAsFactors = F
)

my_df[["value"]] <- unname(my_list)


Is it possible to do this conversion in one step?










share|improve this question



























    up vote
    6
    down vote

    favorite
    1












    Is there a better and nicer way to convert named list with mixed content to data frame?



    The working example:



    my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

    my_df <- data.frame(
    "key" = names(my_list),
    stringsAsFactors = F
    )

    my_df[["value"]] <- unname(my_list)


    Is it possible to do this conversion in one step?










    share|improve this question

























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      Is there a better and nicer way to convert named list with mixed content to data frame?



      The working example:



      my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

      my_df <- data.frame(
      "key" = names(my_list),
      stringsAsFactors = F
      )

      my_df[["value"]] <- unname(my_list)


      Is it possible to do this conversion in one step?










      share|improve this question















      Is there a better and nicer way to convert named list with mixed content to data frame?



      The working example:



      my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

      my_df <- data.frame(
      "key" = names(my_list),
      stringsAsFactors = F
      )

      my_df[["value"]] <- unname(my_list)


      Is it possible to do this conversion in one step?







      r dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 8 hours ago









      Ronak Shah

      27.3k93551




      27.3k93551










      asked 9 hours ago









      RSzT

      655




      655






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          9
          down vote



          accepted










          We can use stack from base R



          stack(my_list)


          According to ?stack




          The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.





          Or with enframe



          library(tidyverse)
          enframe(my_list) %>% # creates the 'value' as a `list` column
          mutate(value = map(value, as.character)) %>% # change to single type
          unnest





          share|improve this answer






















          • Hi @akun, stack is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
            – Jiaxiang
            9 hours ago










          • @Jiaxiang it is from base R. According to ?stack The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
            – akrun
            9 hours ago











          • @akrun if I understand correctly, stack solution needs another step with colnames to change names of columns to key and value? There is no other trick to do this in one operation?
            – RSzT
            9 hours ago










          • @RSzT You can use setNames(stack(my_list)[2:1], c("key", "value")) as the default columns are named with 'ind' and 'values' where as inenframe there is an option to change the name in the argument
            – akrun
            9 hours ago











          • @akrun It seems that ind column (or key after above) is a factor.
            – RSzT
            6 hours ago


















          up vote
          1
          down vote













          You can use dplyr::as_tibble to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.





          library(dplyr)
          library(tidyr)

          my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

          as_tibble(my_list)
          #> # A tibble: 1 x 3
          #> a b c
          #> <dbl> <chr> <lgl>
          #> 1 1 foo TRUE


          To reshape into the two-column format you have, pipe it into tidyr::gather, where the default column names are key and value. Because of the different data types in the column value, this will coerce all the values to character.



          as_tibble(my_list) %>%
          gather()
          #> # A tibble: 3 x 2
          #> key value
          #> <chr> <chr>
          #> 1 a 1
          #> 2 b foo
          #> 3 c TRUE


          Created on 2018-11-09 by the reprex package (v0.2.1)






          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230519%2fconvert-named-list-with-mixed-content-to-data-frame%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            9
            down vote



            accepted










            We can use stack from base R



            stack(my_list)


            According to ?stack




            The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.





            Or with enframe



            library(tidyverse)
            enframe(my_list) %>% # creates the 'value' as a `list` column
            mutate(value = map(value, as.character)) %>% # change to single type
            unnest





            share|improve this answer






















            • Hi @akun, stack is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
              – Jiaxiang
              9 hours ago










            • @Jiaxiang it is from base R. According to ?stack The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
              – akrun
              9 hours ago











            • @akrun if I understand correctly, stack solution needs another step with colnames to change names of columns to key and value? There is no other trick to do this in one operation?
              – RSzT
              9 hours ago










            • @RSzT You can use setNames(stack(my_list)[2:1], c("key", "value")) as the default columns are named with 'ind' and 'values' where as inenframe there is an option to change the name in the argument
              – akrun
              9 hours ago











            • @akrun It seems that ind column (or key after above) is a factor.
              – RSzT
              6 hours ago















            up vote
            9
            down vote



            accepted










            We can use stack from base R



            stack(my_list)


            According to ?stack




            The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.





            Or with enframe



            library(tidyverse)
            enframe(my_list) %>% # creates the 'value' as a `list` column
            mutate(value = map(value, as.character)) %>% # change to single type
            unnest





            share|improve this answer






















            • Hi @akun, stack is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
              – Jiaxiang
              9 hours ago










            • @Jiaxiang it is from base R. According to ?stack The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
              – akrun
              9 hours ago











            • @akrun if I understand correctly, stack solution needs another step with colnames to change names of columns to key and value? There is no other trick to do this in one operation?
              – RSzT
              9 hours ago










            • @RSzT You can use setNames(stack(my_list)[2:1], c("key", "value")) as the default columns are named with 'ind' and 'values' where as inenframe there is an option to change the name in the argument
              – akrun
              9 hours ago











            • @akrun It seems that ind column (or key after above) is a factor.
              – RSzT
              6 hours ago













            up vote
            9
            down vote



            accepted







            up vote
            9
            down vote



            accepted






            We can use stack from base R



            stack(my_list)


            According to ?stack




            The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.





            Or with enframe



            library(tidyverse)
            enframe(my_list) %>% # creates the 'value' as a `list` column
            mutate(value = map(value, as.character)) %>% # change to single type
            unnest





            share|improve this answer














            We can use stack from base R



            stack(my_list)


            According to ?stack




            The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.





            Or with enframe



            library(tidyverse)
            enframe(my_list) %>% # creates the 'value' as a `list` column
            mutate(value = map(value, as.character)) %>% # change to single type
            unnest






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 9 hours ago

























            answered 9 hours ago









            akrun

            387k13172249




            387k13172249











            • Hi @akun, stack is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
              – Jiaxiang
              9 hours ago










            • @Jiaxiang it is from base R. According to ?stack The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
              – akrun
              9 hours ago











            • @akrun if I understand correctly, stack solution needs another step with colnames to change names of columns to key and value? There is no other trick to do this in one operation?
              – RSzT
              9 hours ago










            • @RSzT You can use setNames(stack(my_list)[2:1], c("key", "value")) as the default columns are named with 'ind' and 'values' where as inenframe there is an option to change the name in the argument
              – akrun
              9 hours ago











            • @akrun It seems that ind column (or key after above) is a factor.
              – RSzT
              6 hours ago

















            • Hi @akun, stack is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
              – Jiaxiang
              9 hours ago










            • @Jiaxiang it is from base R. According to ?stack The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
              – akrun
              9 hours ago











            • @akrun if I understand correctly, stack solution needs another step with colnames to change names of columns to key and value? There is no other trick to do this in one operation?
              – RSzT
              9 hours ago










            • @RSzT You can use setNames(stack(my_list)[2:1], c("key", "value")) as the default columns are named with 'ind' and 'values' where as inenframe there is an option to change the name in the argument
              – akrun
              9 hours ago











            • @akrun It seems that ind column (or key after above) is a factor.
              – RSzT
              6 hours ago
















            Hi @akun, stack is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
            – Jiaxiang
            9 hours ago




            Hi @akun, stack is simple and quick but I don't find the help document. Would you like to add the package name (base?) or explain it a little bit?
            – Jiaxiang
            9 hours ago












            @Jiaxiang it is from base R. According to ?stack The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
            – akrun
            9 hours ago





            @Jiaxiang it is from base R. According to ?stack The stack function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The unstack function reverses this operation.. Here, it is a named list. So, it unlist the values and at the same time, replicate the names of the list as a separate column
            – akrun
            9 hours ago













            @akrun if I understand correctly, stack solution needs another step with colnames to change names of columns to key and value? There is no other trick to do this in one operation?
            – RSzT
            9 hours ago




            @akrun if I understand correctly, stack solution needs another step with colnames to change names of columns to key and value? There is no other trick to do this in one operation?
            – RSzT
            9 hours ago












            @RSzT You can use setNames(stack(my_list)[2:1], c("key", "value")) as the default columns are named with 'ind' and 'values' where as inenframe there is an option to change the name in the argument
            – akrun
            9 hours ago





            @RSzT You can use setNames(stack(my_list)[2:1], c("key", "value")) as the default columns are named with 'ind' and 'values' where as inenframe there is an option to change the name in the argument
            – akrun
            9 hours ago













            @akrun It seems that ind column (or key after above) is a factor.
            – RSzT
            6 hours ago





            @akrun It seems that ind column (or key after above) is a factor.
            – RSzT
            6 hours ago













            up vote
            1
            down vote













            You can use dplyr::as_tibble to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.





            library(dplyr)
            library(tidyr)

            my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

            as_tibble(my_list)
            #> # A tibble: 1 x 3
            #> a b c
            #> <dbl> <chr> <lgl>
            #> 1 1 foo TRUE


            To reshape into the two-column format you have, pipe it into tidyr::gather, where the default column names are key and value. Because of the different data types in the column value, this will coerce all the values to character.



            as_tibble(my_list) %>%
            gather()
            #> # A tibble: 3 x 2
            #> key value
            #> <chr> <chr>
            #> 1 a 1
            #> 2 b foo
            #> 3 c TRUE


            Created on 2018-11-09 by the reprex package (v0.2.1)






            share|improve this answer
























              up vote
              1
              down vote













              You can use dplyr::as_tibble to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.





              library(dplyr)
              library(tidyr)

              my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

              as_tibble(my_list)
              #> # A tibble: 1 x 3
              #> a b c
              #> <dbl> <chr> <lgl>
              #> 1 1 foo TRUE


              To reshape into the two-column format you have, pipe it into tidyr::gather, where the default column names are key and value. Because of the different data types in the column value, this will coerce all the values to character.



              as_tibble(my_list) %>%
              gather()
              #> # A tibble: 3 x 2
              #> key value
              #> <chr> <chr>
              #> 1 a 1
              #> 2 b foo
              #> 3 c TRUE


              Created on 2018-11-09 by the reprex package (v0.2.1)






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                You can use dplyr::as_tibble to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.





                library(dplyr)
                library(tidyr)

                my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

                as_tibble(my_list)
                #> # A tibble: 1 x 3
                #> a b c
                #> <dbl> <chr> <lgl>
                #> 1 1 foo TRUE


                To reshape into the two-column format you have, pipe it into tidyr::gather, where the default column names are key and value. Because of the different data types in the column value, this will coerce all the values to character.



                as_tibble(my_list) %>%
                gather()
                #> # A tibble: 3 x 2
                #> key value
                #> <chr> <chr>
                #> 1 a 1
                #> 2 b foo
                #> 3 c TRUE


                Created on 2018-11-09 by the reprex package (v0.2.1)






                share|improve this answer












                You can use dplyr::as_tibble to coerce the list into a data frame / tibble. This will automatically create a data frame where the list's names are column names, and the list items correspond to rows.





                library(dplyr)
                library(tidyr)

                my_list <- list("a" = 1.0, "b" = "foo", "c" = TRUE)

                as_tibble(my_list)
                #> # A tibble: 1 x 3
                #> a b c
                #> <dbl> <chr> <lgl>
                #> 1 1 foo TRUE


                To reshape into the two-column format you have, pipe it into tidyr::gather, where the default column names are key and value. Because of the different data types in the column value, this will coerce all the values to character.



                as_tibble(my_list) %>%
                gather()
                #> # A tibble: 3 x 2
                #> key value
                #> <chr> <chr>
                #> 1 a 1
                #> 2 b foo
                #> 3 c TRUE


                Created on 2018-11-09 by the reprex package (v0.2.1)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 8 hours ago









                camille

                6,20431227




                6,20431227



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230519%2fconvert-named-list-with-mixed-content-to-data-frame%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?