I have a count matrix in .csv format. The data is structured like this:
| Genes | cond. 1 | cond. 2 | cond. 3 |
|---|---|---|---|
| Alpha | 77 | 51 | 98 |
| Beta | 0 | 0 | 71 |
| Cena | 823 | 856 | 0 |
I'm trying to filter out the matrix where the new matrix will have rows with a sum that is above 0.
To filter out the rows whose sum is 0, I wrote the code in this way:
Counts <- Count[which(rowSums(Counts) > 0), ]
but it gave me an error saying:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for
function 'which': 'x' must be numeric
I checked the data to see if there were any NA's but there were none. There are only numeric numbers.
Here is my full code:
Counts <- read.delim("RiboTag_count_matrix_10-05-2023.csv",
header = TRUE, sep=",")
Counts ## shows the matrix visually
Counts <- Counts[which(rowSums(Counts > 0,] ## filter out rows with 0's
I'm not sure where in my code it produced an error. Any advice is greatly appreciated. Thank you.
I even tried
## Convert Counts to a matrix
Counts <- as.matrix(Counts)
## Convert them to numeric
Counts <- apply(Counts, 2, as.numeric)
# Check for and handle missing values
if (any(is.na(Counts))) {
## Handle missing values (e.g., replace with 0)
Counts[is.na(Counts)] <- 0
}
to troubleshoot my problem but still gave me same error message.
Wait, you try to drink the coffee without milk, but buy it with milk first and take it out later.
Use
read.delimproperlyIn documentation
?read.delimfurther down in section "CSV files" we read,which also applies to
read.delim. In section just above we learn, the Value we get is adata.frame, and need doingas.matrix()to get a matrix.So to get the
matrixwith desired integers and row and column names, doand everything is fine.
Note: If your .csv file is actually comma separated, use
read.csvinstead; your use ofsep=','confuses me a little.The thing with the
whichwhich"cares" forNAs. (I've added one in the Epsilon row to demonstrate.)While using
whichalso removes rows withNAs,not using it, fails.
However, you may want to decide if you really want to remove these rows, or do something else with them, e.g. impute or check where the
NAcomes from in the first place.Appendix
matrixes, unlikedata.frames can only contain onetypeofdata, e.g. either numeric (integer, double) or character. If we had a numeric matrixm,and change only one element to character,
we get a character matrix,
which was the case with your
Countmatrix.Data:
foo.csv file:
or alternatively