rtidyversernoaa

converting nested tbl_df into tibble or data frame


I'm running a function in the 'rnoaa' package that finds the 5 nearest weather stations to a data frame that contains nest box locations. This produces a nested tbl_df, a tibble for each nest box ID. I'd like to convert the tbl_df into a tibble or data frame that retains the corresponding nest box ID, but I'm not sure how to do it. Here's my code and an example of the data.

Import the data:

 nests<-structure(list(id = structure(1:5, .Label = c("29", "36", "39", 
    "41", "42", "43", "45", "47", "48", "50", "51", "52", "53", "54", 
    "55", "57", "58", "59", "60", "61", "62", "64", "65", "67", "69", 
    "70", "71", "72", "73", "75", "77", "78", "79", "80", "81", "82", 
    "84", "87", "88", "89", "90", "91", "92", "93", "95", "97", "99", 
    "100", "102", "106", "108", "109", "110", "118", "123", "124", 
    "125", "126", "127", "129", "130", "131", "133", "134", "136", 
    "138", "140", "141", "144", "147", "149", "151", "155", "157", 
    "158", "160", "161", "162", "163", "165", "167", "168", "169", 
    "172", "174", "175", "177", "178", "179", "180", "181", "182", 
    "186", "189", "190", "193", "195", "202", "205", "207", "208", 
    "215", "217", "218", "225", "229", "230", "236", "240", "241", 
    "243", "244", "246", "247", "248", "249", "251", "253", "254", 
    "255", "257", "258", "259", "260", "261", "262", "263", "269", 
    "270", "276", "292", "294", "295", "296", "297", "298", "300", 
    "301", "302", "303", "305", "306", "307", "308", "309", "311", 
    "316", "317", "318", "322", "323", "324", "326", "329", "330", 
    "331", "332", "333", "334", "335", "336", "337", "338", "339", 
    "342", "345", "346", "350", "351", "353", "358", "362", "363", 
    "365", "366", "368", "369", "372", "379", "380", "381", "382", 
    "384", "386", "387", "388", "390", "391", "392", "393", "394", 
    "395", "396", "397", "398", "400", "401", "403", "404", "406", 
    "410", "411", "414", "415", "416", "418", "420", "424", "425", 
    "426", "428", "429", "430", "432", "433", "435", "436", "440", 
    "441", "442", "445", "446", "447", "448", "449", "450", "451", 
    "453", "458", "459", "461", "462", "463", "464", "465", "466", 
    "469", "470", "471", "478", "479", "488", "490", "497", "503", 
    "504", "506", "507", "508", "509", "512", "513", "514", "515", 
    "516", "517", "518", "519", "520", "521", "527", "528", "529", 
    "530", "531", "534", "540", "542", "545", "552", "553", "554", 
    "556", "558", "561", "562", "563", "565", "566", "568", "569", 
    "570", "571", "572", "573", "574", "575", "576", "577", "578", 
    "580", "583", "584", "585", "591", "592", "595", "606", "608", 
    "610", "612", "614", "615", "616", "617", "620", "621", "627", 
    "628", "634", "635", "636", "637", "638", "639", "643", "647", 
    "648", "651", "652", "653", "654", "656", "661", "662", "663", 
    "664", "665", "667", "669", "670", "673", "674", "676", "677", 
    "679", "680", "681", "684", "685", "690", "693", "694", "695", 
    "706", "708", "716", "717", "719", "720", "728", "757", "759", 
    "761", "777", "798", "801", "803", "818", "838", "839", "855", 
    "856", "864", "865", "867", "868", "880", "890", "899", "901", 
    "914", "915", "924", "985", "998", "999", "1002", "1003", "1004", 
    "1019", "1020", "1021", "1022", "1058", "1059", "1116", "1139", 
    "1146", "1164", "1169", "1170", "1178", "1183", "1186", "1188", 
    "1193", "1211", "1233", "1235", "1236", "1237", "1251", "1263", 
    "1285", "1288", "1289", "1294", "1296", "1298", "1299", "1300", 
    "1302", "1303", "1305", "1307", "1310", "1311", "1328", "1331", 
    "1332", "1333", "1334", "1335", "1455", "1456", "1459", "1461", 
    "1462", "1463", "1466", "1467", "1469", "1473", "1474", "1475", 
    "1476", "1478", "1479", "1480", "1482", "1485", "1487", "1503", 
    "1506", "1520", "1534", "1564", "1572", "1575", "1582", "1587", 
    "1588", "1592", "1593", "1594", "1597", "1602", "1607", "1611", 
    "1612", "1613", "1615", "1616", "1617", "1619", "1633", "1656", 
    "1657", "1658", "1660", "1663", "1664", "1667", "1668", "1669", 
    "1676", "1677", "1679", "1691", "1704", "1716", "1734", "1735", 
    "1736", "1766", "1771", "1772", "1773", "1775", "1777", "1783", 
    "1801", "1814", "1818", "1834", "1835", "1836", "1837", "1838", 
    "1840", "1843", "1845", "1846", "1847", "1850", "1852", "1856", 
    "1857", "1858", "1859", "1860", "1882", "1883", "1890", "1891", 
    "1897", "1899", "1901", "1902", "1909", "1910", "1912", "1914", 
    "1923", "1926", "1928", "1929", "1935", "1941", "1956", "1958", 
    "1960", "1968", "1991", "1994", "1998", "2002", "2010", "2012", 
    "2016", "2019", "2024", "2026", "2029", "2030", "2032", "2033", 
    "2034", "2035", "2036", "2039", "2042", "2046", "2049", "2053", 
    "2055", "2056", "2057", "2059", "2093", "2101", "2103", "2121", 
    "2134", "2146", "2147", "2152", "2184", "2185", "2186", "2187", 
    "2188", "2190", "2197", "2201", "2239", "2240", "2249", "2250", 
    "2291", "2313", "2322", "2347", "2351", "2353", "2354", "2355", 
    "2360", "2361", "2369", "2370", "2372", "2373", "2374", "2375", 
    "2376", "2402", "2426", "2427", "2445", "2447", "2449", "2459", 
    "2460", "2462", "2467", "2468", "2469", "2471", "2484", "2485", 
    "2486", "2488", "2490", "2494", "2496", "2517", "2613", "2623", 
    "2624", "2625", "2641", "2696", "2697", "2709", "2711", "2712", 
    "2713", "2714", "2997", "3000", "3004"), class = "factor"), latitude = c(43.29515222, 
    44.02074565, 44.44193, 44.146666, 43.98897), longitude = c(-89.29077182, 
    -92.04753707, -121.40635, -121.347223, -121.18639)), .Names = c("id", 
    "latitude", "longitude"), row.names = c(NA, -5L), class = c("tbl_df", 
    "tbl", "data.frame"))

This grabs the 5 nearest weather stations and produces a tbl_df:

nearest_station<-meteo_nearby_stations(lat_lon_df = nests, station_data = station_data,
                      limit = 5, var = c("TAVG"),
                      year_min = 2011, year_max = 2016)
nearest_station

Finally, I used do.call to produce a single data frame:

ns <- do.call(rbind, lapply(nearest_station, data.frame, stringsAsFactors=FALSE))
head(ns)

While the resulting data table shows the nest box ID next to the weather station ID (under id), the first column really only contains the weather station ID:

 id                     name latitude longitude  distance
29.1 USW00014837     MADISON DANE RGNL AP  43.1406  -89.3453  17.74438
29.2 USR0000WDDG     DODGEVILLE WISCONSIN  43.1000  -90.0000  61.44939
29.3 USW00014839    MILWAUKEE MITCHELL AP  42.9550  -87.9044 118.69939
29.4 USW00094822 ROCKFORD GTR ROCKFORD AP  42.1928  -89.0931 123.63416
29.5 USW00094908          DUBUQUE RGNL AP  42.3978  -90.7036 152.38709
36.1 USW00014925        ROCHESTER INTL AP  43.9042  -92.4917  37.83807

ns[,1]

USW00014837

Is there a way to keep the nest box information in the weather station data frame?


Solution

  • (sorry for late reply)

    If you use something like dplyr::bind_rows, you can do:

    dplyr::bind_rows(nearest_station, .id = "nest_box_id")
    

    to get

    #> # A tibble: 25 x 6
    #>    nest_box_id id          name                        latitude longitude distance
    #>    <chr>       <chr>       <chr>                          <dbl>     <dbl>    <dbl>
    #>    29          USW00014837 MADISON DANE RGNL AP            43.1     -89.3     17.7
    #>    29          USR0000WDDG DODGEVILLE WISCONSIN            43.1     -90.0     61.4
    #>    29          USW00014839 MILWAUKEE MITCHELL AP           43.0     -87.9    119.
    #>    29          USW00094822 ROCKFORD GTR ROCKFORD AP        42.2     -89.1    124.
    #>    29          USW00094908 DUBUQUE RGNL AP                 42.4     -90.7    152.
    #>    36          USW00014925 ROCHESTER INTL AP               43.9     -92.5     37.8
    #>    36          USW00014920 LA CROSSE MUNI AP               43.9     -91.3     65.5
    #>    36          USR0000WBRF BLACK RIVER FALLS WISCONSIN     44.3     -90.8    102.
    #>    36          USR0000WAUG AUGUSTA WISCONSIN               44.7     -91.1    105.
    #>    36          USW00014922 MINNEAPOLIS/ST PAUL AP          44.9     -93.2    134.
    #> # ... with 15 more rows