将 NULL 分配给 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"
列表元素向上移动。 也许这正如预期的那样,但我正在尝试实现一个函数,其中合并列表的两个元素并删除一个。我基本上想丢失该列表索引或将其设置为 NULL。
有什么方法可以将 NULL 分配给它并且看不到上述行为吗?
感谢您的建议。
I found this behaviour odd and wanted more experienced users to share their thoughts and workarounds.
On running the code sample below in 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.
Is there any way I can assign NULL to it and not see the above behaviour?
Thank you for your suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我花了一段时间才弄清楚这个列表的列表。我的解决方案是:
Took me a while to figure this one out for a list of lists. My solution was:
好问题。
查看R-FAQ:
请考虑以下示例:
更新:
作为 R Inferno评论道,处理NULL时可能会出现更微妙的情况。考虑相当一般的代码结构:
现在请注意,如果
some_function()
返回NULL
,您可能不会得到您想要的:某些元素将消失 。你应该使用lapply
函数。看看这个玩具示例:
Good question.
Check out the R-FAQ:
consider the following example:
UPDATE:
As the author of the R Inferno commented, there can be more subtle situations when dealing with NULL. Consider pretty general structure of code:
Now be aware, that if
some_function()
returnsNULL
, you maybe will not get what you want: some elements will just disappear. you should rather uselapply
function.Take a look at this toy example:
你的问题让我有点困惑。
将 null 分配给现有对象本质上会删除该对象(例如,如果您有一个数据框并希望删除特定列,这会非常方便)。这就是你所做的。我无法确定你想要什么。您可以尝试
代替 NULL,但如果“我想失去”您的意思是删除它,那么您已经成功了。这就是为什么“列表元素向上移动”。
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
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 值列表,稍后您可以使用值(例如数据帧)填充这里没有抱怨:
上面的答案是相似的,但我认为这是值得发布的。
If you need to create a list of NULL values which later you can populate with values (dataframes, for example) here is no complain:
The above answers are similar, but I thought this was worth posting.