【问题标题】:Convert a list into a dataframe with <0 rows> (or 0-length row.names)将列表转换为具有 <0 行>(或 0 长度行名称)的数据框
【发布时间】:2015-11-17 17:08:50
【问题描述】:

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

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

第三个元素显示为 (或 0-length 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")

【问题讨论】:

  • 我实际上并没有在您的问题正文中看到rbind,但也许do.call(rbind, L) 是您正在寻找的......?相当于rbind(L[[1]], L[[2]], ..., L[[length(L)]])
  • 你可能想试试data.table::rbindlist
  • @Frank。是的。这就是我对 do.call(rbind,L) 所做的。 rbind 将忽略 [[3]] [1] full.address (或 0-length row.names)
  • @RichardScriven data.table::rbindlist 会做同样的事情。 rbind 和 data.table::rbindlist 都将跳过绑定第三个元素。
  • 你能把dput(list[[3]]) 的输出放上去吗?这取决于那个元素的具体结构是什么

标签: r rbind


【解决方案1】:

问题在于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"))

【讨论】:

  • 谢谢@jeremycg 我真的很感激。
  • 等等,rbind() 什么时候开始这样做的?我认为那会出错
  • 我想永远?来自?rbindFor ‘cbind’ (‘rbind’), vectors of zero length (including ‘NULL’) are ignored unless the result would have zero rows (columns), for S compatibility. (Zero-extent matrices do not occur in S3 and are not ignored in R.)
猜你喜欢
  • 1970-01-01
  • 2018-10-30
  • 2013-06-10
  • 2021-11-17
  • 2020-05-31
  • 2017-10-31
  • 1970-01-01
  • 2017-01-20
  • 1970-01-01
相关资源
最近更新 更多