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