在一些不太相关的答案中,我查看了这两个答案:
答案1
答案2
然而,这些方案并没有解决我的问题。
我可能误解了我的问题,并试图用错误的方法去做正确的事情。非常感谢任何帮助。
下面是我的代码,我构建了一个字符串列表,并尝试删除列表的第二个元素:
> my_strings <- "string1 string2 string3 string4 string5"
> my_list <- strsplit(my_strings,split=" ")
> #Now trying to delete one element from my_list using positive indexing
>
> my_list[[2]] <- NULL #does not work
> my_list[2] <- NULL #nope. Doesn't work either
> my_list[[1]][2] <- NULL #error: replacement has length zero
> my_list[[1]][[2]] <- NULL # error: more elements supplied than there are to replace
那么,我的问题是:如何删除my_list的第二个元素(或多个元素,比如1和3)? my_list的元素没有名称,我想通过数字索引访问它们。
mylist[[1]] <- mylist[[1]][-2]
。 - JustinNULL
。 - Rolandmylist[[1]][3:7]
保留第3到第7个元素)或负索引范围(mylist[[1]][-3:-7]
删除第3到第7个元素)。再次感谢! - bomgarotomylist[[1]][var1:var2]
才能正常工作。 - bomgaroto