将列表转换为具有< 0行>的数据帧(或0长度的row.names)

时间:2015-11-17 17:08:50

标签: r rbind

我正在使用RDSTK包将地址转换为lat / lon。我想将以下列表转换为数据帧。不知道如何处理我得到的第三个元素。这是清单:

[[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

第三个元素显示为&lt; 0 rows&gt; (或0长度的row.names)。

我想要实现的是

    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

这是我用dput得到的:

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

1 个答案:

答案 0 :(得分:3)

问题是rbind忽略任何空的data.frames并且不添加它们。

因此,我们可以更改您的数据,以便现在为空的NA:

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

然后我们可以使用rbind

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

使用的数据:

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"))