How extract values from this list (from rgbif) and convert it in data frame in R?












0














I have this list l



l

$`5216014`
$`5216014`[[1]]
name key rank
1 Animalia 1 kingdom
2 Chordata 44 phylum
3 Elasmobranchii 121 class
4 Rajiformes 884 order
5 Rajidae 5877 family
6 Raja 2342057 genus
7 Raja montagui 5216014 species


$`5216208`
$`5216208`[[1]]
name key rank
1 Animalia 1 kingdom
2 Chordata 44 phylum
3 Elasmobranchii 121 class
4 Rajiformes 884 order
5 Rajidae 5877 family
6 Amblyraja 2420436 genus
7 Amblyraja radiata 2420446 species


I would like to extract only row number 7 for column name and key and to create a database like this:



df

name key
Raja montagui 5216014
Amblyraja radiata 2420446


I tried with lapply function:



lapply(l,function(x) x[7])


but the result is this:



$`5216014`
$`5216014`[[1]]
NULL


$`5216208`
$`5216208`[[1]]
NULL


I tried to transform l in a data frame:



data.frame(matrix(unlist(l)))


but I obtained one big column with all data as:



kingdom
phylum
class
order
family
genus
species
Animalia
Chordata
Elasmobranchii
4943
Rajiformes
Rajidae
Raja
Raja montagui
1
44
121
884
5877
2342057
5216014
4954
kingdom
phylum
class
order
family
genus
species
Animalia
Chordata
Elasmobranchii
Rajiformes
4965
Rajidae
Amblyraja
Amblyraja radiata
1
44
121
884
5877
2420436
2420446
kingdom
phylum
class
order
family
genus
species


Where am I wrong?



P.S: this kind of list l is an output of packages rgbif, with code:



    keySpecies <- c(5216014,2420446)
l.1 <- occ_search(taxonKey=keySpecies,
limit=1, return='hier',
curlopts=list(verbose=F))
l <- l.1[c(1:length(unique(keySpecies)))] #cut all element of list that are not useful









share|improve this question



























    0














    I have this list l



    l

    $`5216014`
    $`5216014`[[1]]
    name key rank
    1 Animalia 1 kingdom
    2 Chordata 44 phylum
    3 Elasmobranchii 121 class
    4 Rajiformes 884 order
    5 Rajidae 5877 family
    6 Raja 2342057 genus
    7 Raja montagui 5216014 species


    $`5216208`
    $`5216208`[[1]]
    name key rank
    1 Animalia 1 kingdom
    2 Chordata 44 phylum
    3 Elasmobranchii 121 class
    4 Rajiformes 884 order
    5 Rajidae 5877 family
    6 Amblyraja 2420436 genus
    7 Amblyraja radiata 2420446 species


    I would like to extract only row number 7 for column name and key and to create a database like this:



    df

    name key
    Raja montagui 5216014
    Amblyraja radiata 2420446


    I tried with lapply function:



    lapply(l,function(x) x[7])


    but the result is this:



    $`5216014`
    $`5216014`[[1]]
    NULL


    $`5216208`
    $`5216208`[[1]]
    NULL


    I tried to transform l in a data frame:



    data.frame(matrix(unlist(l)))


    but I obtained one big column with all data as:



    kingdom
    phylum
    class
    order
    family
    genus
    species
    Animalia
    Chordata
    Elasmobranchii
    4943
    Rajiformes
    Rajidae
    Raja
    Raja montagui
    1
    44
    121
    884
    5877
    2342057
    5216014
    4954
    kingdom
    phylum
    class
    order
    family
    genus
    species
    Animalia
    Chordata
    Elasmobranchii
    Rajiformes
    4965
    Rajidae
    Amblyraja
    Amblyraja radiata
    1
    44
    121
    884
    5877
    2420436
    2420446
    kingdom
    phylum
    class
    order
    family
    genus
    species


    Where am I wrong?



    P.S: this kind of list l is an output of packages rgbif, with code:



        keySpecies <- c(5216014,2420446)
    l.1 <- occ_search(taxonKey=keySpecies,
    limit=1, return='hier',
    curlopts=list(verbose=F))
    l <- l.1[c(1:length(unique(keySpecies)))] #cut all element of list that are not useful









    share|improve this question

























      0












      0








      0







      I have this list l



      l

      $`5216014`
      $`5216014`[[1]]
      name key rank
      1 Animalia 1 kingdom
      2 Chordata 44 phylum
      3 Elasmobranchii 121 class
      4 Rajiformes 884 order
      5 Rajidae 5877 family
      6 Raja 2342057 genus
      7 Raja montagui 5216014 species


      $`5216208`
      $`5216208`[[1]]
      name key rank
      1 Animalia 1 kingdom
      2 Chordata 44 phylum
      3 Elasmobranchii 121 class
      4 Rajiformes 884 order
      5 Rajidae 5877 family
      6 Amblyraja 2420436 genus
      7 Amblyraja radiata 2420446 species


      I would like to extract only row number 7 for column name and key and to create a database like this:



      df

      name key
      Raja montagui 5216014
      Amblyraja radiata 2420446


      I tried with lapply function:



      lapply(l,function(x) x[7])


      but the result is this:



      $`5216014`
      $`5216014`[[1]]
      NULL


      $`5216208`
      $`5216208`[[1]]
      NULL


      I tried to transform l in a data frame:



      data.frame(matrix(unlist(l)))


      but I obtained one big column with all data as:



      kingdom
      phylum
      class
      order
      family
      genus
      species
      Animalia
      Chordata
      Elasmobranchii
      4943
      Rajiformes
      Rajidae
      Raja
      Raja montagui
      1
      44
      121
      884
      5877
      2342057
      5216014
      4954
      kingdom
      phylum
      class
      order
      family
      genus
      species
      Animalia
      Chordata
      Elasmobranchii
      Rajiformes
      4965
      Rajidae
      Amblyraja
      Amblyraja radiata
      1
      44
      121
      884
      5877
      2420436
      2420446
      kingdom
      phylum
      class
      order
      family
      genus
      species


      Where am I wrong?



      P.S: this kind of list l is an output of packages rgbif, with code:



          keySpecies <- c(5216014,2420446)
      l.1 <- occ_search(taxonKey=keySpecies,
      limit=1, return='hier',
      curlopts=list(verbose=F))
      l <- l.1[c(1:length(unique(keySpecies)))] #cut all element of list that are not useful









      share|improve this question













      I have this list l



      l

      $`5216014`
      $`5216014`[[1]]
      name key rank
      1 Animalia 1 kingdom
      2 Chordata 44 phylum
      3 Elasmobranchii 121 class
      4 Rajiformes 884 order
      5 Rajidae 5877 family
      6 Raja 2342057 genus
      7 Raja montagui 5216014 species


      $`5216208`
      $`5216208`[[1]]
      name key rank
      1 Animalia 1 kingdom
      2 Chordata 44 phylum
      3 Elasmobranchii 121 class
      4 Rajiformes 884 order
      5 Rajidae 5877 family
      6 Amblyraja 2420436 genus
      7 Amblyraja radiata 2420446 species


      I would like to extract only row number 7 for column name and key and to create a database like this:



      df

      name key
      Raja montagui 5216014
      Amblyraja radiata 2420446


      I tried with lapply function:



      lapply(l,function(x) x[7])


      but the result is this:



      $`5216014`
      $`5216014`[[1]]
      NULL


      $`5216208`
      $`5216208`[[1]]
      NULL


      I tried to transform l in a data frame:



      data.frame(matrix(unlist(l)))


      but I obtained one big column with all data as:



      kingdom
      phylum
      class
      order
      family
      genus
      species
      Animalia
      Chordata
      Elasmobranchii
      4943
      Rajiformes
      Rajidae
      Raja
      Raja montagui
      1
      44
      121
      884
      5877
      2342057
      5216014
      4954
      kingdom
      phylum
      class
      order
      family
      genus
      species
      Animalia
      Chordata
      Elasmobranchii
      Rajiformes
      4965
      Rajidae
      Amblyraja
      Amblyraja radiata
      1
      44
      121
      884
      5877
      2420436
      2420446
      kingdom
      phylum
      class
      order
      family
      genus
      species


      Where am I wrong?



      P.S: this kind of list l is an output of packages rgbif, with code:



          keySpecies <- c(5216014,2420446)
      l.1 <- occ_search(taxonKey=keySpecies,
      limit=1, return='hier',
      curlopts=list(verbose=F))
      l <- l.1[c(1:length(unique(keySpecies)))] #cut all element of list that are not useful






      r list dataframe lapply






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 at 14:23









      skylobo

      766




      766
























          1 Answer
          1






          active

          oldest

          votes


















          1














          lapply(l,function(x) x[7]) is close, but you need to index [row, column], not just [row]. Instead do this:



          res = lapply(l,function(x) x[7, c("name", "key")])
          # or more simply
          res = lapply(l, "[", 7, c("name", "key"))


          This will still give you a list, which you'll need to combine together into a single data frame:



          do.call(rbind, res)




          Edit: I installed your package and ran your code, and apparently l is a list of lists of data frames, not a list of data frames. So we need to extract that as well:



          res = lapply(lapply(l, "[[", 1), "[", 7, c("name", "key"))
          res = do.call(rbind, res)
          res
          # name key
          # 5216014 Raja montagui 5216014
          # 2420446 Amblyraja radiata 2420446





          share|improve this answer























          • R tell me: Error in x[7, c("name", "key")] : incorrect number of dimensions
            – skylobo
            Nov 13 at 14:36










          • Thank you very much!
            – skylobo
            Nov 13 at 14:58











          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',
          autoActivateHeartbeat: false,
          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%2f53283155%2fhow-extract-values-from-this-list-from-rgbif-and-convert-it-in-data-frame-in-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          lapply(l,function(x) x[7]) is close, but you need to index [row, column], not just [row]. Instead do this:



          res = lapply(l,function(x) x[7, c("name", "key")])
          # or more simply
          res = lapply(l, "[", 7, c("name", "key"))


          This will still give you a list, which you'll need to combine together into a single data frame:



          do.call(rbind, res)




          Edit: I installed your package and ran your code, and apparently l is a list of lists of data frames, not a list of data frames. So we need to extract that as well:



          res = lapply(lapply(l, "[[", 1), "[", 7, c("name", "key"))
          res = do.call(rbind, res)
          res
          # name key
          # 5216014 Raja montagui 5216014
          # 2420446 Amblyraja radiata 2420446





          share|improve this answer























          • R tell me: Error in x[7, c("name", "key")] : incorrect number of dimensions
            – skylobo
            Nov 13 at 14:36










          • Thank you very much!
            – skylobo
            Nov 13 at 14:58
















          1














          lapply(l,function(x) x[7]) is close, but you need to index [row, column], not just [row]. Instead do this:



          res = lapply(l,function(x) x[7, c("name", "key")])
          # or more simply
          res = lapply(l, "[", 7, c("name", "key"))


          This will still give you a list, which you'll need to combine together into a single data frame:



          do.call(rbind, res)




          Edit: I installed your package and ran your code, and apparently l is a list of lists of data frames, not a list of data frames. So we need to extract that as well:



          res = lapply(lapply(l, "[[", 1), "[", 7, c("name", "key"))
          res = do.call(rbind, res)
          res
          # name key
          # 5216014 Raja montagui 5216014
          # 2420446 Amblyraja radiata 2420446





          share|improve this answer























          • R tell me: Error in x[7, c("name", "key")] : incorrect number of dimensions
            – skylobo
            Nov 13 at 14:36










          • Thank you very much!
            – skylobo
            Nov 13 at 14:58














          1












          1








          1






          lapply(l,function(x) x[7]) is close, but you need to index [row, column], not just [row]. Instead do this:



          res = lapply(l,function(x) x[7, c("name", "key")])
          # or more simply
          res = lapply(l, "[", 7, c("name", "key"))


          This will still give you a list, which you'll need to combine together into a single data frame:



          do.call(rbind, res)




          Edit: I installed your package and ran your code, and apparently l is a list of lists of data frames, not a list of data frames. So we need to extract that as well:



          res = lapply(lapply(l, "[[", 1), "[", 7, c("name", "key"))
          res = do.call(rbind, res)
          res
          # name key
          # 5216014 Raja montagui 5216014
          # 2420446 Amblyraja radiata 2420446





          share|improve this answer














          lapply(l,function(x) x[7]) is close, but you need to index [row, column], not just [row]. Instead do this:



          res = lapply(l,function(x) x[7, c("name", "key")])
          # or more simply
          res = lapply(l, "[", 7, c("name", "key"))


          This will still give you a list, which you'll need to combine together into a single data frame:



          do.call(rbind, res)




          Edit: I installed your package and ran your code, and apparently l is a list of lists of data frames, not a list of data frames. So we need to extract that as well:



          res = lapply(lapply(l, "[[", 1), "[", 7, c("name", "key"))
          res = do.call(rbind, res)
          res
          # name key
          # 5216014 Raja montagui 5216014
          # 2420446 Amblyraja radiata 2420446






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 at 14:59

























          answered Nov 13 at 14:30









          Gregor

          62.7k988167




          62.7k988167












          • R tell me: Error in x[7, c("name", "key")] : incorrect number of dimensions
            – skylobo
            Nov 13 at 14:36










          • Thank you very much!
            – skylobo
            Nov 13 at 14:58


















          • R tell me: Error in x[7, c("name", "key")] : incorrect number of dimensions
            – skylobo
            Nov 13 at 14:36










          • Thank you very much!
            – skylobo
            Nov 13 at 14:58
















          R tell me: Error in x[7, c("name", "key")] : incorrect number of dimensions
          – skylobo
          Nov 13 at 14:36




          R tell me: Error in x[7, c("name", "key")] : incorrect number of dimensions
          – skylobo
          Nov 13 at 14:36












          Thank you very much!
          – skylobo
          Nov 13 at 14:58




          Thank you very much!
          – skylobo
          Nov 13 at 14:58


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283155%2fhow-extract-values-from-this-list-from-rgbif-and-convert-it-in-data-frame-in-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)