converting string to numeric in R

converting string to numeric in R

You could change the mode of your matrix to numeric:

## example data
m <- matrix(as.character(1:10), nrow=2,
            dimnames = list(c(R1, R2), LETTERS[1:5]))
m
#    A   B   C   D   E
# R1 1 3 5 7 9
# R2 2 4 6 8 10

str(m)
#  num [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
#  - attr(*, dimnames)=List of 2
#   ..$ : chr [1:2] R1 R2
#   ..$ : chr [1:5] A B C D ...
# NULL

mode(m) <- numeric
str(m)
#  num [1:2, 1:5] 1 2 3 4 5 6 7 8 9 10
#  - attr(*, dimnames)=List of 2
#   ..$ : chr [1:2] R1 R2
#   ..$ : chr [1:5] A B C D ...
# NULL
m
#    A B C D  E
# R1 1 3 5 7  9
# R2 2 4 6 8 10

Give this a try: m <- matrix(as.numeric(lung.X), nrow = 86, ncol = 7129)

If you need it in dataframe/list format, df <- data.frame(m)

converting string to numeric in R

Leave a Reply

Your email address will not be published. Required fields are marked *