fre*_*ddy 3 indexing components r list
我试图从R中的列表中删除一个组件,但它无法正常工作.
我有这样的列表:
> myList
[[1]]
[[1]][[1]]
[1] "Sunny" "Cloudy" "Rainy"
[[1]][[2]]
[1] "Warm" "Cold"
[[1]][[3]]
[1] "Normal" "High"
[[1]][[4]]
[1] "Strong" "Weak"
[[1]][[5]]
[1] "Warm" "Cool"
[[1]][[6]]
[1] "Same" "Change"
[[2]]
[[2]][[1]]
[1] "Sunny" "Cloudy" "Rainy"
[[2]][[2]]
[1] "Warm" "Cold"
[[2]][[3]]
[1] "Normal" "High"
[[2]][[4]]
[1] "Strong" "Weak"
[[2]][[5]]
[1] "Warm" "Cool"
[[2]][[6]]
[1] "Same" "Change"
Run Code Online (Sandbox Code Playgroud)
我可以删除这样的组件:> myList = mylist[[-particularIndex]]?我想要一个这样的结果:
[[1]]
[[1]][[1]]
[1] "Sunny" "Cloudy" "Rainy"
[[1]][[2]]
[1] "Warm" "Cold"
[[1]][[3]]
[1] "Normal" "High"
[[1]][[4]]
[1] "Strong" "Weak"
[[1]][[5]]
[1] "Warm" "Cool"
[[1]][[6]]
[1] "Same" "Change"
Run Code Online (Sandbox Code Playgroud)
我无法为该列表创建字符串名称标签.我必须引用行index(particular index)(使用rownames(myList)删除的东西不会帮助我).
我认为你非常接近正确答案:
> x <- list(list(1:3, 4:6), list(4:7, 2:3), list(4:6,1:2))
> x[-2]
[[1]]
[[1]][[1]]
[1] 1 2 3
[[1]][[2]]
[1] 4 5 6
[[2]]
[[2]][[1]]
[1] 4 5 6
[[2]][[2]]
[1] 1 2
Run Code Online (Sandbox Code Playgroud)
以上工作摆脱了原来的第二个组件.注意单方括号,并与:
X [[ - 2]]
Error in x[[-2]] : attempt to select more than one element
Run Code Online (Sandbox Code Playgroud)
双重支架不起作用.(实际上,如果列表中只有两个组件,那么它确实有效,但不依赖于它.)
有很多地方可以解释单方括号和双方括号.其中之一是'The R Inferno'的Circle 8.1.54 http://www.burns-stat.com/pages/Tutor/R_inferno.pdf
| 归档时间: |
|
| 查看次数: |
5262 次 |
| 最近记录: |