简体   繁体   中英

converting a nested data frame containing multiple lists into a useable dataframe/tibble

I'm working with a package in R that spits out weather variables for a list of sites that I provide in a csv file. The data comes as a dataframe containing multiple lists, so one for each site. In each list for each site there is info on the site name, lat, long, altitude, and tile, the data for that site, which is a data frame containing 9 variables. It's probably best to just take a look at the output by running the code here:

library(tidyverse)
if(!require(devtools)){install.package(devtools)}
devtools::install_github("khufkens/daymetr")
library(daymetr)

the 'my_sites.csv' below looks like this, where this is no column headings, just the site name and lat long (3 columns total):

Alpha 43.29515  -89.29077  
Delta  44.14667 -121.34722

weather_by_site<-download_daymet_batch(file_location = '/Users/Jay/Desktop/my_sites.csv',
                      start = 2012,
                      end = 2016,
                      internal = TRUE)

This gives you a better idea of what the output looks like:

str(weather_by_site)

I'm looking to combine the output with the 9 weather variables with the site name and tile for each site. The goal is to run this same code but for >600 sites, and to be able to analyze the data in a neater looking data frame or tibble format. So basically I would have a data from 2012-2016 for one site, and all the sites would be binded together.

Using purrr, we can loop through each result, grab the dataframe from the results, then push the other variables onto the end of that dataframe:

library(dplyr)
library(purrr)

# write a function to do pull out the data frame
# then append the other values
tidyweather  <- function(x) {

    datadf  <- pluck(x, "data")
    datadf$site       <- x[[1]]
    datadf$lattitude  <- x[[2]]
    datadf$longitude  <- x[[3]]
    datadf$altitude   <- x[[4]]
    datadf$tile       <- x[[5]]

    return(datadf)
}


weather_df  <- weather_by_site %>%
    # run tidyweather() on each result and return one big data frame
    map_df(tidyweather) 

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