NA 替换 list/for 循环中的 NULL

NA to replace NULL in list/for loop

我试图用从 API 中拉出的列表中的 NA 替换 NULL 值,但长度不同,因此无法替换。

我尝试使用 toxboot 包中的 nullToNA 函数(找到 here), but it won't locate the function in R when I try to call it (I don't know if there have been changes to the package which I can't locate or whether it is because the list is not pulled from a MongoDB). I have also tried all the function call checks here。我的代码在下面。有帮助吗?

library(httr)
library(toxboot)
library(RJSONIO)
library(lubridate)
library(xlsx)
library(reshape2)

resUrl <- "http://api.eia.gov/series/?api_key=2B5239FA427673D22505DBF45664B12E&series_id=NG.N3010CO3.M"

comUrl <- "http://api.eia.gov/series/?api_key=2B5239FA427673D22505DBF45664B12E&series_id=NG.N3020CO3.M"

indUrl <- "http://api.eia.gov/series/?api_key=2B5239FA427673D22505DBF45664B12E&series_id=NG.N3035CO3.M"

apiList <- list(resUrl, comUrl, indUrl)

results <- vector("list", length(apiList))

for(i in length(apiList)){
  raw <- GET(url = as.character(apiList[i]))
  char <- rawToChar(raw$content)
  list <- fromJSON(char)
    for (j in length(list$series[[1]]$data)){
      if (is.null(list$series[[1]]$data[[j]][[2]])== TRUE)
        ##nullToNA(list$series[[1]]$data[[j]][[2]])
        ##list$series[1]$data[[j]][[2]] <- NA
      else
        next
    }
  ##seriesData <- list$series[[1]]$data
  unlistResult <- lapply(list, unlist)
  ##unlistResult <- lapply(seriesData, unlist)
  ##unlist2 <- lapply(unlistResult,unlist)
  ##results[[i]] <- unlistResult
  results[[i]] <- unlistResult
}

我的标签有一些我尝试过的东西。不过还有几个方法我没试过。

我已经看过 lapply(list, function(x) ifelse (x == "NULL", NA, x)) 但没能成功。

试试这个:

library(httr)
resUrl <- "http://api.eia.gov/series/?api_key=2B5239FA427673D22505DBF45664B12E&series_id=NG.N3010CO3.M"
x <- GET(resUrl)
y <- content(x)
str(head(y$series[[1]]$data))
# List of 6
#  $ :List of 2
#   ..$ : chr "201701"
#   ..$ : NULL
#  $ :List of 2
#   ..$ : chr "201612"
#   ..$ : num 6.48
#  $ :List of 2
#   ..$ : chr "201611"
#   ..$ : num 7.42
#  $ :List of 2
#   ..$ : chr "201610"
#   ..$ : num 9.75
#  $ :List of 2
#   ..$ : chr "201609"
#   ..$ : num 12.1
#  $ :List of 2
#   ..$ : chr "201608"
#   ..$ : num 14.3

在第一个 URL 中,只有 $series[[1]]$data 中的第一个包含一个 NULL。顺便说一句:要清楚地区分 NULL(文字)和 "NULL"(一个 character 字符串,有 4 个字母)。

这里有一些方法(使用各种数据类型)来检查 NULLs:

is.null(NULL)
# [1] TRUE
length(NULL)
# [1] 0

到目前为止很简单,让我们尝试用 NULLs 列出:

l <- list(NULL, 1)
is.null(l)
# [1] FALSE
sapply(l, is.null)
# [1]  TRUE FALSE
length(l)
# [1] 2
lengths(l)
# [1] 0 1
sapply(l, length)
# [1] 0 1

(“0”长度表示 NULLs。)我将在此处使用 lengths

y$series[[1]]$data <- lapply(y$series[[1]]$data, function(z) { z[ lengths(z) == 0 ] <- NA; z; })
str(head(y$series[[1]]$data))
# List of 6
#  $ :List of 2
#   ..$ : chr "201701"
#   ..$ : logi NA
#  $ :List of 2
#   ..$ : chr "201612"
#   ..$ : num 6.48
#  $ :List of 2
#   ..$ : chr "201611"
#   ..$ : num 7.42
#  $ :List of 2
#   ..$ : chr "201610"
#   ..$ : num 9.75
#  $ :List of 2
#   ..$ : chr "201609"
#   ..$ : num 12.1
#  $ :List of 2
#   ..$ : chr "201608"
#   ..$ : num 14.3