Following code subsets a dataframe, but includes the row names in the subset:
mpg = subset(mtcars, cyl=='6', select=mpg)
How can I convert the resulting mpg object to a vector containing the values only? In other words, I need to get rid of the row names from the resulting mpg object
You have a number of options.
Firstly, since you are using
subset, you can specify the argumentdrop=TRUE, but see the warning later in this answer):But in the longer run you should get familiar with the R subset operators
[,[[, and$:And you can simplify that last line of code by using the function
with(but, again, see the warning):Warning: Some aspects of
subsetandwithmay be considered harmful. All objects are first evaluated in the context of the data frame. So, if you happen to use a variable with the same name as a column in your data frame, your expression will evaluate to the column name, not your variable:The
withandsubsetcall has gotgearfrom the data frame, and not from where you set it to 6.