subsetting 1-column matrix deletes rownames [duplicate] - r

This question already has an answer here:
How to subset matrix to one column, maintain matrix data type, maintain row/column names?
(1 answer)
Closed 8 years ago.
When I try to subset a 1-colum matrix by it's row names the subsetting works but an numeric vector is returned.
can you somehow prevent that behaviour and keep the row names?
M<-as.matrix(rnorm(5))
rownames(M)<-LETTERS[1:5]
M
[,1]
A 0.6250957
B 0.7330598
C -0.7127075
D 0.2162602
E 0.2223444
M <- M[which(rownames(M) != "A")]
M
## [1] 0.7330598 -0.7127075 0.2162602 0.2223444

you can read about argument drop in the help page: ?'['
M[which(rownames(M) != "A"), ,drop=FALSE]

Related

Convert each list element of different length into a column in R [duplicate]

This question already has answers here:
How to cbind or rbind different lengths vectors without repeating the elements of the shorter vectors?
(6 answers)
How to convert a list consisting of vector of different lengths to a usable data frame in R?
(6 answers)
Closed last month.
I was wondering if there is an efficient way to convert each element (each with different length) in my List to a column in a data frame to achieve my Desired_output?
I tried the following without success:
dplyr::bind_cols(List)
Note: This data is toy, a functional answer is appreciated.
List <- list(`1000`=letters[1:2], `2000`=letters[1:3], `3000`=letters[1:4])
Desired_output <-
data.frame(`1000`= c(letters[1:2],"",""),
`2000`= c(letters[1:3],""),
`3000`= letters[1:4])
You could try this.
library(purrr)
l <- list(`1000`=letters[1:2], `2000`=letters[1:3], `3000`=letters[1:4])
# get max lengths across all list items
max_len = max(lengths(l))
# using purrr::modify add as many empty characters needed until each
# item has the same number of rows as the item with the most
l = modify(l, function(f) {
f = c(f, rep("", max_len-length(f)))
})
as.data.frame(l)
X1000 X2000 X3000
1 a a a
2 b b b
3 c c
4 d

How to turn a 1D named int into a wide dataframe [duplicate]

This question already has answers here:
Filtering single-column data frames
(1 answer)
How to subset matrix to one column, maintain matrix data type, maintain row/column names?
(1 answer)
Closed 4 months ago.
I have a large matrix from which I want to select rows to further process them.
Ultimatly, I want to convert the matrix to a dataframe that only contains my rows of interest.
The easiest thing would be to simple run something like data.frame(my_matrix)[c(3,5),]. The problem with this is, that I am working with a very big sparse matrix, so converting all of it to a dataframe just to select a few rows is ineffective.
This option does what I want, but somehow only returns the result that I intend if I indicate at least 2 indices.
m <- matrix(1:25,nrow = 5)
rownames(m) <- 1:5
colnames(m) <- c("A","B","C","D","E")
data.frame(m[c(3,5),])
If i only want to select 1 row, and if I use the code above, the result is not a "wide" dataframe, but instead a long one, which looks like this:
data.frame(m[c(2),])
m.c.2....
A 2
B 7
C 12
D 17
E 22
Is there a simple way to get a dataframe with just one row out of the matrix without converting the whole matrix first? It feels like I am overlooking something very obvious here...
Any help is much appreciated!
You need to use drop=FALSE in the matrix subset, otherwise it will turn the matrix into a vector, as you saw.
m <- matrix(1:25,nrow = 5)
rownames(m) <- 1:5
colnames(m) <- c("A","B","C","D","E")
data.frame(m[c(2),, drop=FALSE])
#> A B C D E
#> 2 2 7 12 17 22
Created on 2022-10-12 by the reprex package (v2.0.1)

Need help in using R "as." function [duplicate]

This question already has answers here:
issue summing columns
(2 answers)
Closed 6 years ago.
a <- x
where x is data frame with 120 columns in it.
sum(a$column1) == 0
this condition works fine.
I have a situation where I have to find each column’s sum.
So created vector m with list of all column names in it
m <- colnames(a)
tried calling the vector values inside the sum function it is throwing error.
sum(m[1]) == 0
throws some error. Not sure which as. Function to use here.
is sapply(x, sum) what you want?

Countif function in R [duplicate]

This question already has answers here:
Sum rows in data.frame or matrix
(7 answers)
Count number of columns by a condition (>) for each row
(4 answers)
Closed 6 years ago.
I have a matrix of n*m dimension. I wanted to count the number of columns in a row which has a value greater than "X". How to do this in R? Please let me know.
You can try rowSums
X <- 0.5
rowSums(m1 > X)
explanation
m1 > X will create a TRUE/FALSE logical matrix. Since TRUE values are treated as 1 and FALSE values are treated as 0, rowSums(m1 > X) will give you a count for each row of the number of values in that row that is greater than X.
data
set.seed(24)
m1 <- matrix(rnorm(5*10), ncol=5)

R, accessing a column vector of a matrix by name [duplicate]

This question already has answers here:
Extract matrix column values by matrix column name
(2 answers)
Closed 7 years ago.
In R I can access the data in a column vector of a column matrix by the following:
mat2[,1]
Each column of mat2 has a name. How can I retrieve the data from the first column by using the name attribute instead of [,1]?
For example suppose my first column had the name "saturn". I want something like
mat2[,1] == mat2[saturn]
The following should do it:
mat2[,'saturn']
For example:
> x <- matrix(1:21, nrow=7, ncol=3)
> colnames(x) <- paste('name', 1:3)
> x[,'name 1']
[1] 1 2 3 4 5 6 7
Bonus information (adding to the first answer)
x[,c('name 1','name 2')]
would return two columns just as if you had done
x[,1:2]
And finally, the same operations can be used to subset rows
x[1:2,]
And if rows were named...
x[c('row 1','row 2'),]
Note the position of the comma within the brackets and with respect to the indices.

Resources