2017-04-17 59 views
1

我試圖用從API中拉出的列表中的NAs替換NULL值,但長度不同,因此無法替換。不適用於替換列表中的NULL/for循環

我已經嘗試在toxboot包(找到here)中使用nullToNA函數,但是當我試圖調用它時,它不會在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)),但沒有任何運氣。

+0

I th墨水在「顯示您在項目中某處使用的所有庫」和「顯示與此[最小示例](http://stackoverflow.com/help/mcve)相關的庫」之間有一個平衡。我相信你不需要包含'openxlsx','lubridate'或'reshape2'。另外,我建議你可以使用'httr :: content()'並且否定'fromJSON'和'rawToChar'的顯式使用,但也許我錯過了一些東西。 – r2evans

回答

2

試試這個:

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個字母)。

這裏有一些方法(用不同的數據類型)來檢查NULL S:

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

夠簡單了,到目前爲止,我們嘗試用NULL s到列表:

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「的長度表示NULL s)。我將在這裏使用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 
+1

非常感謝您的幫助和詳細的解釋,這非常有幫助。這真的簡化了我的代碼。 – Sammy