我有以下数据框,称为planets.df
:
type | planets | diameter | rotation | rings
---------------------------------------------------------
Terrestrial planet| Mercury | 0.382 | 58.64 | FALSE
Terrestrial planet| Venus | 0.949 |-243.02 | FALSE
Terrestrial planet| Earth | 1.000 | 1.00 | FALSE
Terrestrial planet| Mars | 0.532 | 1.03 | FALSE
Gass giant | Jupiter | 11.209 | 0.41 | TRUE
Gass giant | Saturn | 9.449 | 0.43 | TRUE
Gass giant | Uranus | 4.007 | -0.72 | TRUE
Gass giant | Neptune| 3.883 | 0.67 | TRUE
我想获取所有具有环的植物,即rings = TRUE
使用以下代码:
ring.vector <- planets.df$rings
planets.with.rings.df <- planets.df[rings.vector,]
有人可以告诉我为什么这样做吗?我自己没有提出代码,但想了解它为什么起作用。该部分[rings.vector,]
意味着rings=TRUE
什么?
谢谢!
之所以起作用,是因为在某种df[<condition,]
类型的语句中,该condition
部分基本上是T / F的向量。保留与TRUE相对应的行号,并省略与FALSE相对应的行号。
rings.vector
已经是T / F的向量。您可以改用rings.vector == TRUE
提供相同条件的条件。
在您的情况下,这可能并不重要,但是要注意向量或要过滤的列中是否有NA
s condition
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句