I have a data frame as follows:
planets type diameter rotation rings
Mercury Terrestrial planet 0.382 58.64 FALSE
Venus Terrestrial planet 0.949 -243.02 FALSE
Earth Terrestrial planet 1.000 1.00 FALSE
Mars Terrestrial planet 0.532 1.03 FALSE
Jupiter Gas giant 11.209 0.41 TRUE
Saturn Gas giant 9.449 0.43 TRUE
Uranus Gas giant 4.007 -0.72 TRUE
Neptune Gas giant 3.883 0.67 TRUE
I wanted to select last 3 rows:
planets_df[nrow(planets_df)-3:nrow(planets_df),]
However, I've got something I didn't expect:
planets type diameter rotation rings
Jupiter Gas giant 11.209 0.41 TRUE
Mars Terrestrial planet 0.532 1.03 FALSE
Earth Terrestrial planet 1.000 1.00 FALSE
Venus Terrestrial planet 0.949 -243.02 FALSE
Mercury Terrestrial planet 0.382 58.64 FALSE
With trial and error method, I've learned that
> (nrow(planets_df)-3):nrow(planets_df)
[1] 5 6 7 8
and
> nrow(planets_df)-3:nrow(planets_df)
[1] 5 4 3 2 1 0
How does exactly R evaluate :
statement (with reference to brackets)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…