Use the drop=FALSE
argument to [
.
m <- matrix(1:10,5,2)
rownames(m) <- 1:5
colnames(m) <- 1:2
m[,1] # vector
m[,1,drop=FALSE] # matrix
More Related Contents:
- Filter data.frame rows by a logical condition
- R memory management / cannot allocate vector of size n Mb
- Select rows from a data frame based on values in a vector
- Subset data frame based on number of rows per group
- Why is `[` better than `subset`?
- Subset / filter rows in a data frame based on a condition in a column
- Subset rows in a data frame based on a vector of values
- Index values from a matrix using row, col indices
- Extract matrix column values by matrix column name
- Remove groups with less than three unique observations
- Should I use a data.frame or a matrix?
- How to subset a matrix with different column positions for each row? [duplicate]
- How to remove rows of a matrix by row name, rather than numerical index?
- Drop unused factor levels in a subsetted data frame
- Use dynamic name for new column/variable in `dplyr`
- How can I remove all duplicates so that NONE are left in a data frame?
- strptime, as.POSIXct and as.Date return unexpected NA
- Why is it not advisable to use attach() in R, and what should I use instead?
- How to plot all the columns of a data frame in R
- Opposite of %in%: exclude rows with values specified in a vector
- How to remove all whitespace from a string?
- How to divide each row of a matrix by elements of a vector in R
- How to subtract/add days from/to a date?
- How can I remove an element from a list?
- Remove backslashes from character string
- What does “S3 methods” mean in R?
- Find K nearest neighbors, starting from a distance matrix
- How to get mean, median, and other statistics over entire matrix, array or dataframe?
- Calculate Euclidean distance matrix using a big.matrix object
- R: How to split a data frame into training, validation, and test sets?