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)
Related
This question already has answers here:
Removing rows with zero numbers [duplicate]
(1 answer)
remove rows that sum zero based on one column as key
(2 answers)
How to remove columns and rows that sum to 0 while preserving non-numeric columns
(2 answers)
Closed 2 years ago.
I have a large dataframe and need to remove a few rows from it as rowSums shows these to be 0 and thus impacting my downstream analysis. I've seen that you can do df[rowSums(df[, -1])>0, ] to achieve this, but I get an error stating that "x must be numeric" as both the first and last columns are not numeric. Any ideas of how to get around this?
We need to remove the first and last columns by indexing
i1 <- sapply(df, is.numeric) # // get a logical index for numeric columns
df[rowSums(df[i1]) > 0,]
This question already has answers here:
R: Count number of objects in list [closed]
(5 answers)
Closed 2 years ago.
I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
You can use lengths to count number of elements in each value of column. Set them to NA where the length is greater than 1. Here I am considering dataframe name as df and column name as col_name. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA
This question already has answers here:
How to create a binary vector with 1 if elements are part of the same vector?
(4 answers)
Closed 7 years ago.
I want to create a vector of 0's and 1's such that all elements in y that are 4 become 1 and all other values become 0. which(y == 4) returns the indices in y that are 4, and then y[which(y == 1)] returns a vector of all 4's. How would I produce a vector of 1's where they were 4's and 0's otherwise?
x <- y[which(y == 4)]
Use logical coercion:
as.integer(y==4)
The shorter version:
+(y==4)
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]
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.