## 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

`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`

.`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

Converting Data Frame into a List of Lists in R

Conditionally Replace Values of Subset of Rows With Column Name in R Using Only Tidy

Remove Ids With Fewer Than 9 Unique Observations

How to Fix Spaces in Column Names of a Data.Frame (Remove Spaces, Inject Dots)

Extract Rows for the First Occurrence of a Variable in a Data Frame

Sum Across Multiple Columns With Dplyr

Remove Last N Rows in Data Frame With the Arbitrary Number of Rows

R: How to Get the Percentage Change from Two Different Columns

Filter a Data Frame According to Minimum and Maximum Values

Combine Two Lists in a Dataframe in R

Easier Way to Use Grepl and Ifelse Across Multiple Columns

Create Counter Within Consecutive Runs of Values

Create and Assign Multiple New Dataframe Columns in Ifelse Statement

Ggplot2: Setting Geom_Bar Baseline to 1 Instead of Zero

How to Sort a Data Frame by Alphabetic Order of a Character Variable in R

Combine (Rbind) Data Frames and Create Column With Name of Original Data Frames