如何从R中的列表中删除组件? [英] How can I delete component from list in R?

查看:182
本文介绍了如何从R中的列表中删除组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从R中的列表中删除一个组件,但它无法正常工作。

I am trying to remove a component from list in R, but it is not working.

我的列表如下:

> 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"

我可以删除这样的组件:> myList = mylist [[ - specialIndex]] ?我想得到这样的结果:

Can I remove a component like this: > myList = mylist[[-particularIndex]]? I want a result like this:

[[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"

我无法为该列表创建字符串名称标签。我必须通过索引(特定索引)引用行(使用 rownames(myList)删除某些内容无效me)。

I can't make a string name label for that list. I have to reference rows by index(particular index) (using rownames(myList) to delete something won't help me).

推荐答案

我认为你非常接近正确答案:

I think you came very close to the right answer:

> 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

以上方法可以摆脱原来的第二个组件。注意单方括号,并与之比较:

The above works to get rid of the original second component. Note single square brackets, and compare with:


x [[ - 2]]

x[[-2]]



Error in x[[-2]] : attempt to select more than one element

双重括号不起作用。 (实际上,如果列表中只有两个组件,但是不依赖于它,那确实有效。)

Double squrare brackets do not work. (Actually that does work if there are only two compoennts in the list, but do not depend on that.)

有很多地方可以解释单方括号和双方括号。其中一个是'The R Inferno'Cir 8.1.54 http://www.burns-stat。 com / pages / Tutor / R_inferno.pdf

There are numerous places that explain single versus double square brackets. One of them is Circle 8.1.54 of 'The R Inferno' http://www.burns-stat.com/pages/Tutor/R_inferno.pdf

这篇关于如何从R中的列表中删除组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆