Luzhi Deng
Luzhi Deng

Reputation: 45

Convert a list into a dataframe with <0 rows> (or 0-length row.names)

I am using RDSTK package to convert address to lat/lon. I want to convert the following list into a dataframe. Not sure how to deal with the third element I got. Here is the list:

[[1]]
                                   full.address country_code3 latitude  country_name longitude      street_address region
1 25462 Alabama Hwy. 127 35620 Elkmont AL 35620           USA 34.92968 United States -86.98871 25462 State Rte 127     AL
  confidence street_number locality   street_name fips_county country_code
1      0.791         25462  Elkmont State Rte 127       01083           US

[[2]]
                                  full.address country_code3 latitude  country_name longitude         street_address region
1 270 Industrial Blvd. 35982 Leesburg AL 35982           USA 33.99676 United States -86.11737 270 Industrial Blvd SE     AL
  confidence street_number locality        street_name fips_county country_code
1      0.678           270  Attalla Industrial Blvd SE       01055           US

[[3]]
[1] full.address
<0 rows> (or 0-length row.names)

[[4]]
                                full.address country_code3 latitude  country_name longitude street_address region confidence
1 934 Adams Avenue 36104 Montgomery AL 36104           USA 32.37545 United States -86.29605  934 Adams Ave     AL      0.883
  street_number   locality street_name fips_county country_code
1           934 Montgomery   Adams Ave       01101           US

[[5]]
                                full.address country_code3 latitude  country_name longitude street_address region confidence
1 8189 Vaughn Road 36116 Montgomery AL 36116           USA 32.33882 United States -86.17086 8189 Vaughn Rd     AL      0.883
  street_number   locality street_name fips_county country_code
1          8189 Montgomery   Vaughn Rd       01101           US

The third element appears as <0 rows> (or 0-length row.names).

What I want to achieve is

    full.address country_code3 latitude  country_name longitude         street_address region
    1 25462 Alabama Hwy. 127 35620 Elkmont AL 35620           USA 34.92968 United States -86.98871    25462 State Rte 127     AL
    2  270 Industrial Blvd. 35982 Leesburg AL 35982           USA 33.99676 United States -86.11737 270 Industrial Blvd SE     AL
    3  NA                                       NA            NA
    4    934 Adams Avenue 36104 Montgomery AL 36104           USA 32.37545 United States -86.29605          934 Adams Ave     AL
    5    8189 Vaughn Road 36116 Montgomery AL 36116           USA 32.33882 United States -86.17086         8189 Vaughn Rd     AL
      confidence street_number   locality        street_name fips_county country_code
    1      0.791         25462    Elkmont      State Rte 127       01083           US
    2      0.678           270    Attalla Industrial Blvd SE       01055           US
    3
           NA             NA     NA                NA              NA               NA
    4      0.883           934 Montgomery          Adams Ave       01101           US
    5      0.883          8189 Montgomery          Vaughn Rd       01101           US

Here is what I got with dput:

> dput(geocode[[3]])
structure(list(full.address = character(0)), .Names = "full.address", row.names = integer(0), class = "data.frame")

Upvotes: 2

Views: 1032

Answers (1)

jeremycg
jeremycg

Reputation: 24945

The problem is that rbind ignores any empty data.frames and doesn;t add them.

So, we can change your data so that what was empty is now NA:

geocode <- lapply(geocode, function(x) if(nrow(x)==0) NA else x)

Then we can use rbind:

do.call(rbind, geocode)
  full.address blah
1         <NA> <NA>
2            a    b

data used:

list(structure(list(full.address = character(0)), .Names = "full.address", row.names = integer(0), class = "data.frame"), 
    structure(list(full.address = structure(1L, .Label = "a", class = "factor"), 
        blah = structure(1L, .Label = "b", class = "factor")), .Names = c("full.address", 
    "blah"), row.names = c(NA, -1L), class = "data.frame"))

Upvotes: 3

Related Questions