0

Say, I have a dataframe df in R as follows,

     id     inflam
1   1 0.03093764
2   2 0.50115406
3   3 0.82153770
4   4 0.01985961
5   5 0.04994588
6   6 0.91714810
7   7 0.83438400
8   8 0.80832225
9   9 0.12360681
10 10 0.08490079

I can access the entirety of the inflam column by indexing as df[,2] or df[2]. However, typeof(df[,2]) returns double, whereas typeof(df[2]) returns list. The comma seems to be the differentiator, but why is this the case? What is going on under the hood?

algebroo
  • 132
  • 6
  • 3
    See e.g. https://ms.mcmaster.ca/~bolker/misc/brackets.html. To get the equivalent of `df[, 2]` one needs to use `df[[2]]`. Part of the confusion comes from the fact that data.frame allow both matrix-like subsetting (`m[r, c]`) and list subsetting (`l[e]` and `l[[e]]`). – Axeman Jun 21 '22 at 19:10
  • 1
    Possible (probable) duplicate: https://stackoverflow.com/q/1169456/324364 – joran Jun 21 '22 at 19:17

0 Answers0