How to Subset Matrix to One Column, Maintain Matrix Data Type, Maintain Row/Column Names

How to subset matrix to one column, maintain matrix data type, maintain row/column names?

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

Subsetting a matrix by row names and column names in R

Try to filter rows and columns in this way:

matrix[rownames(matrix)%in%list_individuals,colnames(matrix)%in%list_individuals]

Only rows and columns contained in list_individuals will be mantained in the output.

subsetting 1-column matrix deletes rownames

you can read about argument drop in the help page: ?'['

M[which(rownames(M) != "A"), ,drop=FALSE]

How to subset and reorder a matrix based on the row names of other matrix

You will get a lot more help if you share reproducible examples. You can use the dput( ) function to help share your data.

you can subset with this code:

marker[row.names(marker) %in% row.names(pheno),]

Add data from one matrix to another based on column name and row name

You can refer to the row and column names in the indexing.

> my_matrix[rownames(my_matrix2), colnames(my_matrix2)] <- my_matrix2
> my_matrix
a b c d e f g
a NA NA NA NA NA NA NA
b NA NA NA NA NA NA NA
c NA NA 1 NA 4 NA 7
d NA NA NA NA NA NA NA
e NA NA 2 NA 5 NA 8
f NA NA NA NA NA NA NA
g NA NA 3 NA 6 NA 9

Extract matrix column with it's name

We can use drop = FALSE without converting to data.frame

m1[,3, drop = FALSE]
# Col3
#Days 9
#Amount 3200

How to keep/assign row & column names when combining 2 vectors into a matrix in R

You just need to use data.frame() instead of matrix()

mean_sd = data.frame(
mean = sapply(mtcars[, 1:3], mean),
sd = sapply(mtcars[, 1:3], sd)
)

Subset one column from data frame keeping the subset as a data frame

We need drop = FALSE

X[, paste0("filter",c(selected_filters)), drop = FALSE]
# filter2
#1 1
#2 1
#3 0

If we look at ?Extract, the Usage shows

x[i, j, ... , drop = TRUE]

and in the description, it says

drop - For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See drop for further details.


Note that subset behavior is different because it is by default drop = FALSE

subset(X, select = paste0("filter",c(selected_filters)))

How to delete specific rows and keep the names?

It is because by default the data.frame, drop = TRUE, so it drops the dimensions when there is a single row/column to return a vector. We can specify drop = FALSE (It won't happen if the object is tibble or data.table)

DiagnosesCount2[-c(31),, drop = FALSE]

Manipulate matrix row/col names to compute values

count_intersect <- function(x,y,symbol){
x <- strsplit(x = x,split = symbol, perl = T)[[1]]
y <- strsplit(x = y,split = symbol, perl = T)[[1]]
result <- ifelse(length(intersect(x,y)),1,0)
return(result)
}

res <- outer(rows, cols, Vectorize(count_intersect), symbol = "")
rownames(res) <- rows
colnames(res) <- cols

res
#> a a c d
#> a b 1 1 0
#> a c 1 1 0
#> a 1 1 0
  1. outer takes the x and y argument and passes them directly into the function as a whole, not elementwise (with y being transposed). You can work around this issue by using Vectorize.
  2. character is always a vector in R, which means that strsplit splits the strings for each element of the function (of which you only have one) and returns a list. You thus want to use the first element only.

Otherwise, I think your attempt is pretty solid.

(One also can remove the require(stringr) part, as strsplit is implemented in base R)



Related Topics



Leave a reply



Submit