在一些不太相關的人刪除元素,我查了一下這兩個答案:
Answer 1
Answer 2
如何從一個字符串列表中的R
然而,提出的解決方案有沒有幫助。
我可能會誤解我自己的問題,並試圖以錯誤的方式做正確的事情。我感謝任何幫助。
我有下面的代碼,我建立一個字符串列表,並嘗試刪除列表中的第二個元素:
> 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
所以,我的問題是:我怎麼能刪除第二個元素(或多個元素,如1和3)my_list? my_list的元素沒有被命名,我想通過數字索引訪問它們。
mylist [[1]] < - mylist [[1]] [ - 2]'。我建議閱讀一下子集的方式,並在R中列出工作。 – Justin
回家信息:使用子集而不是分配NULL。 – Roland
非常感謝賈斯汀(和羅蘭)。使用你顯示的範圍而不是負面的索引完全符合我的目的。我試圖用「< - NULL」結構刪除項目,但它不起作用。子集,然後覆蓋列表完美工作,結果是一樣的。所以我想我不能簡單地刪除元素,但我寧願將子集列入子集並將其替換爲較小的列表 – bomgaroto