在一些不那么相关的人中,我检查了这两个答案:
答案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
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是:如何删除my_list的第二个元素(或多个元素,如1和3)?my_list的元素没有命名,我想通过数字索引访问它们.
我不确定你是否打算用你的代码创建一个向量列表;仅使用字符向量可能会更容易。首先尝试使用 unlist:
my_list <- unlist(strsplit(my_strings,split=" "))
my_list <- my_list[-2]
Run Code Online (Sandbox Code Playgroud)