我发现这种行为很奇怪,想请更有经验的用户分享他们的想法和解决方法。 在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并且不看到上述行为?
感谢您的建议。
unlist
会对你有帮助吗? - Matt Bannert