I need to transform every columns of a R matrix object from numeric to integer.
A description of the matrix:
> dim(path_abundances)
202 48
It looks like this (but with 202 rows and 48 columns):
When looking for questions made here by other users, I found this solution to transform, from numeric to integer (the inverse of what I want), the columns 2 to 13 of a matrix named "dades":
dades[2:13] <- lapply(dades[2:13], as.numeric)
Applied to my specific situation, I tried with:
> path_abundances[1:202] <- lapply(path_abundances[1:202], as.integer)
when looking for the output, it was a list of 202 elements, in which each of them is the value for the i-th row of the first column but withouth the decimals (as expected for an integer):
As said above, I want to transform every column to an integer in a matrix-class output.
Thanks for reading and for your answers in advance
-- EDIT to add an example:
matrix(data= c(path_abundances[1:3,2], path_abundances[1:3,3], path_abundances[1:3,4]), ncol = 3, byrow = F)
[,1] [,2] [,3]
[1,] 512884.5 493049.7 577625.5
[2,] 379358.8 343425.7 394776.8
[3,] 319740.8 327932.6 417228.9
I want this matrix, which each column is numeric-class, to has every column as integer-class
You can use storage.mode
:
set.seed(101); m <- matrix(rnorm(9,mean=20,sd=3), 3,3)
storage.mode(m) <- "integer"
m
[,1] [,2] [,3]
[1,] 19 20 21
[2,] 21 20 19
[3,] 17 23 22
Note that this will truncate, rather than round, to integer - in case that matters to you.