Prevent unlist to drop NULL values

前端 未结 4 663
梦毁少年i
梦毁少年i 2020-12-04 16:19

I have a vector of lists and I use unlist on them. Some of the elements in the vectors are NULL and unlist seems to be dropping them.<

4条回答
  •  粉色の甜心
    2020-12-04 16:53

    The correct way to indicate a missing value is NA (not NULL). Here is another version that is working:

       a = c(list("p1"=2, "p2"=5),
          list("p1"=3, "p2"=4),
          list("p1"=NA, "p2"=NA),
          list("p1"=4, "p2"=5))
      unlist(a)
    
    p1 p2 p1 p2 p1 p2 p1 p2 
     2  5  3  4 NA NA  4  5 
    

提交回复
热议问题