如何在我的 rbind 函数中将空值识别为 "NA"?

How can I recognize null values as "NA" in my rbind function?

我使用 rbind 作为函数的一部分将 json 数据写入 R 中的一个简单数据框。我几乎成功地创建了我的 df,而不是将 NULL 值写入 "NA" 我的 r 脚本打印框架中应该为空的下一个可用变量。换句话说,我看到数据被错误地放置在行中(例如 "access_method.created_at" 和 "access_method.method" 的第 3 行值为空,因此该行被两个变量偏移("access_method.created_at" 和 "access_method.method" 显示 "address" 和 "capacity",等等)。有没有办法解释这些空值?

library(httr)
library(jsonlite)


perpage <- "per_page="
pagenumber <- "page="
pp <- 5000
pn <- 0


vpg <- GET("https://api.seatgeek.com/2/venues?country=US&per_page=5000&page=1&client_id=NTM2MzE3fDE1NzM4NTExMTAuNzU&client_secret=77264dfa5a0bc99095279fa7b01c223ff994437433c214c8b9a08e6de10fddd6")

vpgc <- content(vpg)
vpgcv <- (vpgc$venues)




 json_file <-
  as.data.frame(
    Reduce(function(x, y) {
      rbind(unlist(x), unlist(y))
    }, vpgcv)
  )


venues.dataframe <- as.data.frame(t(json_file))

我试图利用具有空值函数 fill = TRUErbindlist 但没有成功。对于识别这些 NULL 值并正确生成平面数据框的任何建议,我将不胜感激。谢谢!

这可能是 merge() 问题而不是 rbind() 问题。首先,使用 unlist() 并在每个列表对象中创建数据框。第二,merge() 中的所有列表 Reduce()。 (请注意,这会在您的 5k 列表中运行一段时间!

l <- lapply(vpgcv, function(x) as.data.frame(t(cbind(unlist(x)))))
result <- Reduce(function(...) merge(..., all=TRUE), l)

head(result, 3)
# metro_code postal_code        timezone has_upcoming_events    id      city
# 1        623       76011 America/Chicago                TRUE  4965 Arlington
# 2        623       76011 America/Chicago                TRUE    16 Arlington
# 3        623       76011 America/Chicago                TRUE 11491 Arlington
# stats.event_count    extended_address display_location state      score location.lat
# 1                23 Arlington, TX 76011    Arlington, TX    TX  0.9751414      32.7459
# 2                 5 Arlington, TX 76011    Arlington, TX    TX 0.84144884      32.7506
# 3                 5 Arlington, TX 76011    Arlington, TX    TX  0.4188409      32.7385
# location.lon num_upcoming_events capacity                 slug                 name
# 1     -97.0957                  23    80000         at-t-stadium         AT&T Stadium
# 2     -97.0824                   5    49115      globe-life-park      Globe Life Park
# 3     -97.1072                   5     1056 arlington-music-hall Arlington Music Hall
# url country popularity
# 1         https://seatgeek.com/venues/at-t-stadium/tickets      US          0
# 2      https://seatgeek.com/venues/globe-life-park/tickets      US          0
# 3 https://seatgeek.com/venues/arlington-music-hall/tickets      US          0
# name_v2                 address access_method.employee_only
# 1         AT&T Stadium              1 AT&T Way                       FALSE
# 2      Globe Life Park       1000 Ballpark Way                       FALSE
# 3 Arlington Music Hall 224 North Center Street                        <NA>
#   access_method.created_at access_method.method
# 1     2019-05-06T17:03:30Z               QRCODE
# 2     2015-07-06T00:00:00Z               PDF417
# 3                     <NA>                 <NA>