I have a matrix with: character class entries.
sapply(mat,class)
"character"
I would like to apply rowSums to this matrix but I get the error:
Error in rowSums(mat) : 'x' must be numeric
If I do as.numeric(mat) then I get a vector.
Is there a way to change from character to numeric but keep the matrix structure?
You could change the storage mode of your matrix:
mmat <- matrix(c("2","3","7","0"), ncol = 2)
storage.mode(mmat) <- "double" # changed from "numeric"
rowSums(mmat)
# [1] 9 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With