【问题标题】:NA to replace NULL in list/for loopNA 替换列表/for 循环中的 NULL
【发布时间】:2017-09-13 06:50:59
【问题描述】:

我正在尝试用从 API 提取的列表中的 NA 替换 NULL 值,但长度不同,因此无法替换。

我尝试使用toxboot包中的nullToNA函数(找到here),但是当我尝试调用它时它不会在R中找到该函数(我不知道是否对我找不到的包,或者是因为列表不是从 MongoDB 中提取的)。我还尝试了所有函数调用检查 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)) 但对那个eiter没有任何运气。

【问题讨论】:

  • 我认为“显示您在项目中某处使用的所有库”和“显示与此 minimal example 相关的库”之间存在平衡。我相信您不需要包含openxlsxlubridatereshape2。另外,我建议您可以使用httr::content() 并否定fromJSONrawToChar 的显式使用,但也许我错过了一些东西。

标签: r null na data-cleaning


【解决方案1】:

试试这个:

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

【讨论】:

  • 非常感谢您的帮助和详细的解释,这非常有帮助。这确实简化了我的代码。
猜你喜欢
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 2021-10-22
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多