list 将 NULL 分配给 R 中的列表元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7944809/
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
Assigning NULL to a list element in R?
提问by harshsinghal
I found this behaviour odd and wanted more experienced users to share their thoughts and workarounds. On running the code sample below in R:
我发现这种行为很奇怪,并希望更多有经验的用户分享他们的想法和解决方法。在 R 中运行下面的代码示例:
sampleList <- list()
d<- data.frame(x1 = letters[1:10], x2 = 1:10, stringsAsFactors = FALSE)
for(i in 1:nrow(d)) {
sampleList[[i]] <- d$x1[i]
}
print(sampleList[[1]])
#[1] "a"
print(sampleList[[2]])
#[1] "b"
print(sampleList[[3]])
#[1] "c"
print(length(sampleList))
#[1] 10
sampleList[[2]] <- NULL
print(length(sampleList))
#[1] 9
print(sampleList[[2]])
#[1] "c"
print(sampleList[[3]])
#[1] "d"
The list elements get shifted up. Maybe this is as expected, but I am trying to implement a function where I merge two elements of a list and drop one. I basically want to lose that list index or have it as NULL.
列表元素向上移动。也许这正如预期的那样,但我正在尝试实现一个函数,在该函数中我合并列表的两个元素并删除一个。我基本上想丢失该列表索引或将其设为 NULL。
Is there any way I can assign NULL to it and not see the above behaviour?
有什么办法可以为它分配 NULL 而看不到上述行为吗?
Thank you for your suggestions.
谢谢你的建议。
回答by Max
Good question.
好问题。
Check out the R-FAQ:
查看R-FAQ:
In R, if x is a list, then x[i] <- NULL and x[[i]] <- NULL remove the specified elements from x. The first of these is incompatible with S, where it is a no-op. (Note that you can set elements to NULL using x[i] <- list(NULL).)
在 R 中,如果 x 是一个列表,则 x[i] <- NULL 和 x[[i]] <- NULL 从 x 中删除指定的元素。其中第一个与 S 不兼容,它是一个空操作。(请注意,您可以使用 x[i] <- list(NULL) 将元素设置为 NULL。)
consider the following example:
考虑以下示例:
> t <- list(1,2,3,4)
> t[[3]] <- NULL # removing 3'd element (with following shifting)
> t[2] <- list(NULL) # setting 2'd element to NULL.
> t
[[1]]
[2] 1
[[2]]
NULL
[[3]]
[3] 4
UPDATE:
更新:
As the author of the R Infernocommented, there can be more subtle situations when dealing with NULL. Consider pretty general structure of code:
正如R Inferno的作者所评论的,在处理 NULL 时可能会有更微妙的情况。考虑非常通用的代码结构:
# x is some list(), now we want to process it.
> for (i in 1:n) x[[i]] <- some_function(...)
Now be aware, that if some_function()
returns NULL
, you maybe will not get what you want: some elements will just disappear. you should rather use lapply
function.
Take a look at this toy example:
现在请注意,如果some_function()
返回NULL
,您可能不会得到您想要的:某些元素会消失。你应该使用lapply
函数。看看这个玩具示例:
> initial <- list(1,2,3,4)
> processed_by_for <- list(0,0,0,0)
> processed_by_lapply <- list(0,0,0,0)
> toy_function <- function(x) {if (x%%2==0) return(x) else return(NULL)}
> for (i in 1:4) processed_by_for[[i]] <- toy_function(initial[[i]])
> processed_by_lapply <- lapply(initial, toy_function)
> processed_by_for
[[1]]
[1] 0
[[2]]
[1] 2
[[3]]
NULL
[[4]]
[1] 4
> processed_by_lapply
[[1]]
NULL
[[2]]
[1] 2
[[3]]
NULL
[[4]]
[1] 4
回答by Tyler Rinker
Your question is a bit confusing to me.
你的问题让我有点困惑。
Assigning null to an existing object esentially deletes that object (this can be very handy for instance if you have a data frame and wish to delete specific columns). That's what you've done. I am unable to determine what it is that you want though. You could try
为现有对象分配 null 本质上会删除该对象(例如,如果您有一个数据框并希望删除特定列,这可能非常方便)。这就是你所做的。我无法确定你想要的是什么。你可以试试
sampleList[[2]] <- NA
instead of NULL, but if by "I want to lose" you mean delete it, then you've already succeeded. That's why, "The list elements get shifted up."
而不是 NULL,但如果“我想输”你的意思是删除它,那么你已经成功了。这就是为什么“列表元素向上移动”。
回答by Nicholas Hamilton
obj = list(x = "Some Value")
obj = c(obj,list(y=NULL)) #ADDING NEW VALUE
obj['x'] = list(NULL) #SETTING EXISTING VALUE
obj
回答by calycolor
If you need to create a list of NULL values which later you can populate with values (dataframes, for example) here is no complain:
如果您需要创建一个 NULL 值列表,稍后您可以用值(例如数据帧)填充,这里没有抱怨:
B <-vector("list", 2)
a <- iris[sample(nrow(iris), 10), ]
b <- iris[sample(nrow(iris), 10), ]
B[[1]]<-a
B[[2]]<-b
The above answers are similar, but I thought this was worth posting.
上面的答案是相似的,但我认为这值得发布。