pranav nerurkar
pranav nerurkar

Reputation: 648

Count occurance of NULL in list in R

i want to count occurances of NULL in my list named ->

wallet_to_tx_text_json$txs$outputs

For this i tried length(is.NULL(wallet_to_tx_text_json$txs$outputs)) but it returns 1 even when there are 7 NULL

MWE

install.packages('httr')
require(httr)
install.packages('jsonlite')
require(jsonlite)

caller <- "myemail@add"
i<-"091bc8fe2f7aa8fd"
baseurl1<-"http://www.walletexplorer.com/api/1/wallet?wallet="
end1<-"&from=0&count=100&caller="

call1 <- paste(baseurl1,i,end1,caller,sep="")
wallet_to_tx_address <- GET(call1)
wallet_to_tx_text <- content(wallet_to_tx_address, "text")
wallet_to_tx_text_json <- fromJSON(wallet_to_tx_text, flatten = TRUE)
wallet_to_tx_text_json[["total_coinbase"]] <- length(is.NULL(wallet_to_tx_text_json$txs$outputs)) 

how to do it in R

Upvotes: 1

Views: 77

Answers (2)

akrun
akrun

Reputation: 887158

We can use

sum(unlist(lapply(wallet_to_tx_text_json$txs$outputs, is.null)))

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388982

wallet_to_tx_text_json$txs$outputs is a list, you cannot directly use is.null on a list. Iterate over the list (using sapply or any other method), compare with is.null and count the occurrence using sum.

sum(sapply(wallet_to_tx_text_json$txs$outputs, is.null))

Upvotes: 2

Related Questions