Home > Enterprise >  Why does mtcars["mpg"] work, but cbind(A = 1:5, B = 1:5)["A"] does not?
Why does mtcars["mpg"] work, but cbind(A = 1:5, B = 1:5)["A"] does not?

Time:10-17

cbind(A = 1:5, B = 1:5)["A"] is NA, implying that this is somehow an invalid way to subset a matrix. However, for the data frame mtcars, mtcars["mpg"] works just fine. Is this normal R behavior? If so, where is it documented?

CodePudding user response:

Two cases here,

m = cbind(A = 1:5, B = 11:15)
typeof(m)
"integer"

And

typeof(mtcars)
"list"

So reading is different. First case needs comma,

cbind(A = 1:5, B = 11:15)[,"A"]
[1] 1 2 3 4 5

CodePudding user response:

cbind() creates a matrix, by default. mtcars is a data frame.

class(cbind(A = 1:5, B = 1:5))
# [1] "matrix" "array"

class(mtcars)
# [1] "data.frame"

Because data frames are built a lists of columns, dataframe["column_name"], using one argument in [, defaults to treating the data frame as a list, allowing you to select columns, mostly the same as dataframe[, "column_name"].

A matrix has no such list underpinnings, so if you use [ with one argument, it doesn't assume you want columns. Use matrix[, "column_name"] to select columns.

cbind is a bad way to create data frames from scratch. You can specify cbind.data.frame(A = 1:5, B = 1:5), but it's simpler and clearer to use data.frame(A = 1:5, B = 1:5). However, if you are adding multiple columns to an existing data frame then cbind(my_data_frame, A = 1:5, B = 1:5) is fine.

  • Related