Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201894

r- bind rows of a list of dataframes, while keeping empty dataframes as NA rows

$
0
0

I'm looking for a way to rbind a list of dataframes (around 7000 dataframes of one row or empty) into a single dataframe but without removing the empty dataframes of the list.

## Example of data
l <- list(Name1 = data.frame(), 
                 Name2 = data.frame(V1 = "A", V2 = "B", stringsAsFactors = F), 
                 Name3 = data.frame(V1="B", V2= NA, V3 ="C", stringsAsFactors = F))

> l
$Name1
data frame with 0 columns and 0 rows

$Name2
  V1 V2
1  A  B

$Name3
  V1 V2 V3
1  B NA  C

So i've tried some dplyr solution but it doesn't keep the empty dataframe (not suprising cause it has no row)

## rbind
library(dplyr)

df <- bind_rows(l,.id = "NAME")

> df
   NAME V1   V2   V3
1 Name2  A    B <NA>
2 Name3  B <NA>    C

Is there a simple way to get something like this ?

> df
   NAME V1   V2   V3
1 Name1 <NA> <NA> <NA>
2 Name2  A    B <NA>
3 Name3  B <NA>    C

It appears that using tidyr::complete works on my example,

df <- bind_rows(l,.id = "NAME") %>% complete(NAME = names(l))

but not for my dataset. Here the head of my actual dataset

dput(head(GNPlaceName))
list(Seydikemer = structure(list(), class = "data.frame", row.names = integer(0)), 
    Fanna = structure(list(timezone.gmtOffset = "1", timezone.timeZoneId = "Europe/Rome", 
        timezone.dstOffset = "2", bbox.east = "12.7623022680034", 
        bbox.south = "46.1770686700141", bbox.north = "46.1904973822354", 
        bbox.west = "12.7415900814293", bbox.accuracyLevel = "10", 
        asciiName = "Fanna", astergdem = "258", countryId = "3175395", 
        fcl = "P", srtm3 = "263", score = "62.2252464294434", 
        adminId2 = "3170146", adminId3 = "6537931", countryCode = "IT", 
        adminCodes2.ISO3166_2 = "PN", adminCodes1.ISO3166_2 = "36", 
        adminId1 = "3176525", lat = "46.18455", fcode = "PPLA3", 
        continentCode = "EU", elevation = "274", adminCode2 = "PN", 
        adminCode3 = "093020", adminCode1 = "06", lng = "12.75161", 
        geonameId = "3177221", toponymName = "Fanna", population = "1500", 
        adminName5 = "", adminName4 = "", adminName3 = "Fanna", 
        alternateNames.name = "Fane", alternateNames.lang = "fur", 
        adminName2 = "Province of Pordenone", name = "Fanna", 
        fclName = "city, village,...", countryName = "Italy", 
        fcodeName = "seat of a third-order administrative division", 
        adminName1 = "Friuli Venezia Giulia"), row.names = c(NA, 
    -1L), class = "data.frame"), Warsaw = structure(list(timezone.gmtOffset = "1", 
        timezone.timeZoneId = "Europe/Warsaw", timezone.dstOffset = "2", 
        bbox.east = "21.2711512942955", bbox.south = "52.0978496125492", 
        bbox.north = "52.368153944595", bbox.west = "20.8516883368428", 
        bbox.accuracyLevel = "10", asciiName = "Warsaw", astergdem = "121", 
        countryId = "798544", fcl = "P", srtm3 = "113", score = "145.618896484375", 
        adminId2 = "6695624", adminId3 = "7531926", countryCode = "PL", 
        adminCodes1.ISO3166_2 = "14", adminId1 = "858787", lat = "52.22977", 
        fcode = "PPLC", continentCode = "EU", adminCode2 = "1465", 
        adminCode3 = "146501", adminCode1 = "78", lng = "21.01178", 
        geonameId = "756135", toponymName = "Warsaw", population = "1702139", 
        adminName5 = "", adminName4 = "", adminName3 = "Warsaw", 
        alternateNames.name = "Warskou", alternateNames.lang = "af", 
        alternateNames.isShortName = "TRUE", alternateNames.isPreferredName = "TRUE", 
        adminName2 = "Warszawa", name = "Warsaw", fclName = "city, village,...", 
        countryName = "Poland", fcodeName = "capital of a political entity", 
        adminName1 = "Mazovia"), row.names = c(NA, -1L), class = "data.frame"), 
    `Gaverina Terme` = structure(list(timezone.gmtOffset = "1", 
        timezone.timeZoneId = "Europe/Rome", timezone.dstOffset = "2", 
        bbox.east = "9.89570095710424", bbox.south = "45.7540309868418", 
        bbox.north = "45.7576290305937", bbox.west = "9.8836191498607", 
        bbox.accuracyLevel = "10", asciiName = "Gaverina Terme", 
        astergdem = "502", countryId = "3175395", fcl = "P", 
        srtm3 = "494", score = "22.0922546386719", adminId2 = "3182163", 
        adminId3 = "6542955", countryCode = "IT", adminCodes2.ISO3166_2 = "BG", 
        adminCodes1.ISO3166_2 = "25", adminId1 = "3174618", lat = "45.75578", 
        fcode = "PPLA3", continentCode = "EU", elevation = "509", 
        adminCode2 = "BG", adminCode3 = "016110", adminCode1 = "09", 
        lng = "9.88666", geonameId = "6534969", toponymName = "Gaverina Terme", 
        population = "358", adminName5 = "", adminName4 = "", 
        adminName3 = "Gaverina Terme", alternateNames.name = "ITGVR", 
        alternateNames.lang = "unlc", adminName2 = "Provincia di Bergamo", 
        name = "Gaverina Terme", fclName = "city, village,...", 
        countryName = "Italy", fcodeName = "seat of a third-order administrative division", 
        adminName1 = "Lombardy"), row.names = c(NA, -1L), class = "data.frame"), 
    `Cañaveral de León` = structure(list(timezone.gmtOffset = "1", 
        timezone.timeZoneId = "Europe/Madrid", timezone.dstOffset = "2", 
        bbox.east = "-6.49382969510649", bbox.south = "37.9986794024971", 
        bbox.north = "38.0346539975029", bbox.west = "-6.53950370489351", 
        bbox.accuracyLevel = "1", asciiName = "Canaveral de Leon", 
        astergdem = "524", countryId = "2510769", fcl = "P", 
        srtm3 = "528", score = "45.6841278076172", adminId2 = "2516547", 
        adminId3 = "6358196", countryCode = "ES", adminCodes2.ISO3166_2 = "H", 
        adminCodes1.ISO3166_2 = "AN", adminId1 = "2593109", lat = "38.01667", 
        fcode = "PPLA3", continentCode = "EU", adminCode2 = "H", 
        adminCode3 = "21020", adminCode1 = "51", lng = "-6.51667", 
        geonameId = "2520292", toponymName = "Cañaveral de León", 
        population = "0", adminName5 = "", adminName4 = "", adminName3 = "Cañaveral de León", 
        alternateNames.name = "https://en.wikipedia.org/wiki/Ca%C3%B1averal_de_Le%C3%B3n", 
        alternateNames.lang = "link", adminName2 = "Huelva", 
        name = "Cañaveral de León", fclName = "city, village,...", 
        countryName = "Spain", fcodeName = "seat of a third-order administrative division", 
        adminName1 = "Andalusia"), row.names = c(NA, -1L), class = "data.frame"), 
    Voranava = structure(list(timezone.gmtOffset = "3", timezone.timeZoneId = "Europe/Minsk", 
        timezone.dstOffset = "3", bbox.east = "25.3283727016391", 
        bbox.south = "54.139144795013", bbox.north = "54.159255204987", 
        bbox.west = "25.2940272983609", bbox.accuracyLevel = "2", 
        asciiName = "Voranava", astergdem = "170", countryId = "630336", 
        fcl = "P", srtm3 = "172", score = "62.4154739379883", 
        countryCode = "BY", adminCodes1.ISO3166_2 = "HR", adminId1 = "628035", 
        lat = "54.1492", fcode = "PPLA2", continentCode = "EU", 
        adminCode1 = "03", lng = "25.3112", geonameId = "619979", 
        toponymName = "Voranava", population = "6500", adminName5 = "", 
        adminName4 = "", adminName3 = "", alternateNames.name = "Воранава", 
        alternateNames.lang = "be", adminName2 = "", name = "Voranava", 
        fclName = "city, village,...", countryName = "Belarus", 
        fcodeName = "seat of a second-order administrative division", 
        adminName1 = "Grodnenskaya"), row.names = c(NA, -1L), class = "data.frame"))

Thank you so much


Viewing all articles
Browse latest Browse all 201894

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>