list 防止 unlist 删除 NULL 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2991514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:30:48  来源:igfitidea点击:

Prevent unlist to drop NULL values

listrnull

提问by nico

I have a vector of lists and I use unliston them. Some of the elements in the vectors are NULLand unlistseems to be dropping them.

我有一个列表向量,并unlist在它们上使用。向量中的一些元素正在NULL并且unlist似乎正在删除它们。

How can I prevent this?

我怎样才能防止这种情况?

Here's a simple (non) working example showing this unwanted featureof unlist

下面是一个简单的(非)工作表示本实例不需要的功能unlist

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

采纳答案by Fojtasek

The issue here is that you can't have NULLin the middle of a vector. For example:

这里的问题是你不能NULL在向量中间。例如:

> c(1,NULL,3)
[1] 1 3

You can have NA in the middle though. You could could convert it to character and then back to numeric, which automatically converts the NULL values to NA (with a warning):

不过,您可以在中间放置 NA。您可以将其转换为字符,然后再转换回数字,这会自动将 NULL 值转换为 NA(带有警告):

> b <- as.numeric(as.character(a))
Warning message:
NAs introduced by coercion 

then put the names back in, because they've been dropped by the previous operation:

然后把名字放回去,因为它们已经被上一个操作删除了:

> names(b) <- names(a)
> b
p1 p2 p1 p2 p1 p2 p1 p2 
2  5  3  4 NA NA  4  5 `

回答by Marek

In this case (one level depth list) this should works too:

在这种情况下(一级深度列表)这也应该有效:

a[sapply(a, is.null)] <- NA
unlist(a)
# p1 p2 p1 p2 p1 p2 p1 p2 
#  2  5  3  4 NA NA  4  5

回答by elmaroto10

If you are dealing with a long complex JSON with several levels you should give this a try:

如果您正在处理具有多个级别的长而复杂的 JSON,您应该尝试一下:

I extracted game log data from nba.com/stats web site. The problem is, some players have a NULL value for 3 point free throws (mostly centers) and jsonlite::fromJSON seems to handle NULL values very well:

我从 nba.com/stats 网站提取了游戏日志数据。问题是,有些球员的 3 分罚球(主要是中锋)有一个 NULL 值,而 jsonlite::fromJSON 似乎可以很好地处理 NULL 值:

#### Player game logs URL: one record per player per game played ####
gameLogsURL <- paste("http://stats.nba.com/stats/leaguegamelog?Counter=1000&Direction=DESC&LeagueID=00&PlayerOrTeam=P&Season=2016-17&SeasonType=Regular+Season&Sorter=PTS")

#### Import game logs data from JSON ####
# use jsonlite::fromJSON to handle NULL values
gameLogsData <- jsonlite::fromJSON(gameLogsURL, simplifyDataFrame = TRUE)
# Save into a data frame and add column names
gameLogs <- data.frame(gameLogsData$resultSets$rowSet)
colnames(gameLogs) <- gameLogsData$resultSets$headers[[1]]

回答by George Dontas

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

指示缺失值的正确方法是 NA(非 NULL)。这是另一个正在运行的版本:

   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