1

What is the difference between the following two ways of extracting columns from a data frame?

df = data.frame(a=1:5, b=6:10, c=11:15)
cols = c("a","b")
df[, cols]  # Method A
df[cols]    # Method B

Most references that I've found use Method A. However, as far as I can tell, Method B works just as well.

EDIT: I noticed a difference when cols = c("a"). Method A gives

[1] 1 2 3 4 5

whereas Method B gives the (expected?) result of

  a
1 1
2 2
3 3
4 4
5 5
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
tskuzzy
  • 35,812
  • 14
  • 73
  • 140

0 Answers0