簡體   English   中英

R:如何從列表的每個內部元素中刪除第一個元素而不將其轉換為矩陣?

[英]R: How do I remove the first element from each inner element of a list without converting it to matrix?

我有一個這樣的清單

[[1]]
[1] a1 b1 c1
[[2]]
[1] a2 b2 c2
[[3]]
[1] a3 b3 c3

我希望從每個部分刪除特定元素:

[[1]]
[1] a1 c1
[[2]]
[1] a2 c2
[[3]]
[1] a3 c3

我嘗試了tail但刪除了“外部”元素。 也許一些索引會做什么?

假設模式只是你想要刪除第二個元素,

lapply(List, function(x) x[-2])

使用purrr::map它會更短

# setup some example data
nestedList = list(list(4,5,6),list(1,2,3))

# remove first element from each sublist
map(nestedList, tail, -1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM