0

Summary:

During download of financial data (OHLC), I sometimes get the value NULL embedded in "lists within lists" as described in below code.

When a NULL appears it is considered that the line of data (OHLC) is needed to be removed.The "lists in lists" will in later stage be moved to dataframe and transform to xts. If a NULL appears in the list the move to dataframe and xts will fail due to complaints of different amount of objects in list.

Need:

I need to be able to find all NULL and identify them by their positions (which list?). Based on the identification I will then remove all values in same position, eg. if NULL exists in mylist$high[1], then I would remove:

mylist$open[1]
mylist$high[1]
mylist$low[1]
mylist$close[1]

Other observations:

Note that in below scenario the information i still in list format, thus a line of data as existing in dataframe and xts, is not yet present.

Identifying NULL in a variable that is not part of a list, works fine as described in below code.

My R code:

# NULL in lists.
open  <- list(1, 2, 3)
high  <- list(1, 2, 3)
low   <- list(1, 2, 3)
close <- list(1, 2, 3)

mylist <- list(
  open  = open,
  high  = high,
  low   = low,
  close = close
)

mylist$high[1] <- list(NULL) # Adding null (for test purpose).

ls.str(mylist)

is.null(mylist$high[[1]]) # Returns zero length, correct.
length(mylist$high[[1]])  # Returns true, correct

# NULL in variable.
c <- NULL
 length(c)  # Returns zero length, correct.
 is.null(c) # Returns true, correct

What the result looks like:

> ls.str(mylist)
close : List of 3
 $ : num 1
 $ : num 2
 $ : num 3
high : List of 3
 $ : NULL
 $ : num 2
 $ : num 3
low : List of 3
 $ : num 1
 $ : num 2
 $ : num 3
open : List of 3
 $ : num 1
 $ : num 2
 $ : num 3

What it should look like after processing wanted position:

(All values with position [1] in each list (open, high, low, close) are removed)

> ls.str(mylist)
close : List of 2
 $ : num 2
 $ : num 3
high : List of 2
 $ : num 2
 $ : num 3
low : List of 2
 $ : num 2
 $ : num 3
open : List of 2
 $ : num 2
 $ : num 3
Toolbox
  • 2,333
  • 12
  • 26
  • The questions are not identical as you are referring to, even though very similar. The referring question is about -"deleting NULL in embedded lists". My question is specific to "finding NULL in embedded lists". I am looking for a solution that returns the positions of any NULL value, so I can in later step remove them. – Toolbox Feb 28 '19 at 12:41
  • Sorry I didn't understand if working with lists is mandatory, but maybe this can help: ```rbind.fill(lapply(mylist, function(e) { as.data.frame(Filter(Negate(is.null), e)) }))```, then you can remove the columns with NA (`rbind.fill` requires `plyr` package). – tk3 Feb 28 '19 at 20:15

0 Answers0