如何在rbind函数中将空值识别为“ NA”?

时间:2019-11-23 10:54:09

标签: r json dataframe jsonlite

我将rbind用作将json数据写入R中的简单数据帧的函数的一部分。我几乎成功创建了df,但没有将NULL值写入“ NA”,而是通过r脚本打印了框架中的下一个可用变量,应为null。换句话说,我看到数据不正确地放置在行中(例如,“ access_method.created_at”和“ access_method.method”的第3行值为空,因此该行被两个变量偏移(“ access_method.created_at “和“ access_method.method”显示“地址”和“容量”,依此类推。)是否有办法解决这些空值?

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

我试图利用rbindlist(它具有空值函数fill = TRUE)来避免运气。我希望您能提出任何建议来识别这些NULL值并正确生成平面数据帧。谢谢!

1 个答案:

答案 0 :(得分:1)

这可能是一个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>