Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

R transform output of a loop function from embedded lists to dataframe

I am running a loop to pull data from an API. Specifically I have a large dataset of species and am using the International Union for Conservation of Nature's API to pull the threats facing 10,000 plus species. The loop I am using to pull this information is -

result <- vector('list', length(df$scientific_name))

for (i in df$scientific_name) {
  result[[i]] <- rl_threats(name=i, key = '1234', parse = TRUE)
}

This successfully passes in the species name (scientific name) for i and pulls threats for that species. However the issue is the output is not particularly usable for me. The output is a list with 3 layers of nesting like this -

>Result
    >Species name
         >Results data frame _by_

This output is hard to do analyses on and I would like to transform it into a dataframe

This code below worked to transform individual species in the list into a dataframe, but I need to do this for each of the 10,000 plus species in the dataset. I am guessing writing a new loop or modifying the above loop would be the best way to do this, but couldn't figure out how to make it work.

test1<-data.frame(result[26])

Ideal output would be a dataframe that looks like

         species name  threat code  threat title 
species1
species2
species3

Thanks for any help!

Edit- Per request this is what the output looks like from the dput command

dput(head(result))

list(`Myxine glutinosa` = list(name = "Myxine glutinosa", result = structure(list(
    code = c("5.4", "5.4.2"), title = c("Fishing & harvesting aquatic resources", 
    "Intentional use: (large scale) [harvest]"), timing = c("Ongoing", 
    "Ongoing"), scope = c(NA, NA), severity = c(NA, NA), score = c("Low Impact: 3", 
    "Low Impact: 3"), invasive = c(NA, NA)), class = "data.frame", row.names = 1:2)), 
    `Myxine ios` = list(name = "Myxine ios", result = list()), 
    `Lampetra fluviatilis` = list(name = "Lampetra fluviatilis", 
        result = list()), `Lethenteron camtschaticum` = list(
        name = "Lethenteron camtschaticum", result = structure(list(
            code = c("5.4", "5.4.1", "7.2", "7.2.11"), title = c("Fishing & harvesting aquatic resources", 
            "Intentional use: (subsistence/small scale) [harvest]", 
            "Dams & water management/use", "Dams (size unknown)"
            ), timing = c("Ongoing", "Ongoing", "Ongoing", "Ongoing"
            ), scope = c("Unknown", "Unknown", "Unknown", "Unknown"
            ), severity = c("Unknown", "Unknown", "Unknown", 
            "Unknown"), score = c("Unknown", "Unknown", "Unknown", 
            "Unknown"), invasive = c(NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, 
        4L))), `Petromyzon marinus` = list(name = "Petromyzon marinus", 
        result = list()), `Carcharhinus altimus` = list(name = "Carcharhinus altimus", 
        result = structure(list(code = c("5.4", "5.4.3", "5.4.4"
        ), title = c("Fishing & harvesting aquatic resources", 
        "Unintentional effects: (subsistence/small scale) [harvest]", 
        "Unintentional effects: (large scale) [harvest]"), timing = c("Ongoing", 
        "Ongoing", "Ongoing"), scope = c("Majority (50-90%)", 
        "Majority (50-90%)", "Majority (50-90%)"), severity = c("Slow, Significant Declines", 
        "Slow, Significant Declines", "Slow, Significant Declines"
        ), score = c("Medium Impact: 6", "Medium Impact: 6", 
        "Medium Impact: 6"), invasive = c(NA, NA, NA)), class = "data.frame", row.names = c(NA, 
        3L))))
question from:https://stackoverflow.com/questions/65894220/r-transform-output-of-a-loop-function-from-embedded-lists-to-dataframe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It looks like an lapply loop works pretty well!

result_list <- lapply(result, function(spec){
  if(length(spec$result)){
    data.frame(species_name=spec$name, threat_code=spec$result$code, threat_title=spec$result$title)
  }
})
result_df <- do.call(what = rbind, result_list)
rownames(result_df) <- NULL

output


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...