简体   繁体   中英

Selecting an item from list object and adding it to a dataframe in R

This is my first question on stack overflow:) To add information to a biodiveristy database I have written a program loops calls to an API on the basis of a latin name, then returns the common names of the species, and adds this to a dataframe. The program is working fine, but I have a problem with the following situation:

This is an example of the output of each individual loop:

          taxonname      primary     language
1   Polynesian Chestnut   FALSE      eng
2     Tahitian Chestnut    TRUE      eng
3 Chataignier de Tahiti   FALSE      fre

The current program adds the first line to the dataframe. However, I only want to add the name that is the primary result. In some cases, like the one demonstrated above, the primary result is in the second line, and sometimes there is no primary result at all. My problem is that this object seems to be a list, and that I do not know how to select the primary result column. I could really use some help with this.

After having selected the list item, I think i need to make a if/else loop that accounts for these three situations:

Under the condition that the name (used in loop) = name (output) (1) If primary result = TRUE, the row can be added to the data frame (2) if primary result = FALSE in the first row, the program needs to move on to the next row until it finds a primary result = TRUE, then add this row to the data frame. (3) If all rows contain primary result = FALSE, I want the program to fill in the text "No main common name"

Here is the code:

# loop the API call
for (i in encoded.api.names) {
  name <- i
 
  # if i is empty ("") then add NULL values to dataframe and move on to next entry,
  # if else (non empty) move on with loop
  if (i == "") {
    commondf <- rbind(commondf, "NULL")
   
    # if i is not empty, move on with loop
  } else{
   
    call1 <- paste(base,endpoint,name,"?token=",token, sep ="")
   
    print(call1)
   
    # make call to the API
    get_species <- GET(call1)
   
    # convert response API into text
    get_species_text <- content(get_species, "text")
   
    # convert text into JSON format
    get_species_json <- fromJSON(get_species_text, flatten = TRUE)
   
    print(get_species_json)
   

Until here everything works fine, here comes the part where I will need to adapt something:

    # turn into dataframe
    if(length(get_species_json$result) > 0){
      get_species_test <- as.data.frame(get_species_json)[1,c('name','result.taxonname','result.primary','result.language')]
    }else{
      get_species_test <- "NULL"
    }
   
    # use rbind to append each API call to the get_species dataframe
   
    commondf <- rbind(commondf, get_species_test)
   
  }
 
}

The simplest way to work with lists for me is to turn them into tables, operate on the table, and go back into lists

library(tidyverse)

example_data <- read_table('taxonname      primary     language
PolynesianChestnut FALSE eng
TahitianChestnut TRUE eng
ChataignierdeTahiti FALSE fre')

example_list <- example_data |> 
  as.list()

example_list |> 
  as_tibble() |> 
  filter(primary) |> 
  as.list()
#> $taxonname
#> [1] "TahitianChestnut"
#> 
#> $primary
#> [1] TRUE
#> 
#> $language
#> [1] "eng"
#> 
#> attr(,"spec")
#> cols(
#>   taxonname = col_character(),
#>   primary = col_logical(),
#>   language = col_character()
#> )

Created on 2022-01-24 by the reprex package (v2.0.1)

Apparently the item is not a list but a dataframe, and the correct column can easily be accessed with get_species_json$result$primary. Thanks anyways for responding and the comments!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM