Change a matrix from character data to numeric data without changing the row and column names?

Hi.
I have a dataset as a data.frame and I want to create a barplot from it. But then I first need to make it into a matrix. When I make it into a matrix class, the values are for whatever reason changed from integer to character. Is there any way to change the character values into numeric, without altering the row/column names? Or is it possible to create a matrix from a data.frame without the values being changed to character...?

My data (as a data.frame):

data.frame(
stringsAsFactors = FALSE,
check.names = FALSE,
row.names = c("M.bro", "Or.tip", "Paint.l", "Pea", "Red.ad", "Ring"),
1996 = c(" 88", " 90", " 50", " 48", " 6", "190"),
1997 = c(" 47", " 14", " 0", "110", " 3", " 80"),
1998 = c("13", "36", " 0", "85", " 8", "96"),
1999 = c(" 33", " 24", " 0", " 54", " 10", "179"),
2000 = c(" 86", " 47", " 4", " 65", " 15", "145")
)

The numbers are characters in your data. If you want them to be numeric, don't include numbers in the double quotation marks. Moreover, numbers can't be used as variable names, they should be assigned as characters.

data.frame(
  stringsAsFactors = FALSE,
  check.names = FALSE,
  row.names = c("M.bro", "Or.tip", "Paint.l", "Pea", "Red.ad", "Ring"),
  `1996` = c( 88,  90,  50,  48,  6, 190),
  `1997` = c( 47,  14,  0, 110,  3,  80),
  `1998` = c(13, 36,  0, 85,  8, 96),
  `1999` = c( 33,  24,  0,  54,  10, 179),
  `2000` = c( 86,  47,  4,  65,  15, 145)
)

They do not get converted, they were characters from the beginning, if you convert your columns into numeric before calling as.matrix() then the matrix is numeric.

library(dplyr)

sample_df <- data.frame(
    stringsAsFactors = FALSE,
    check.names = FALSE,
    row.names = c("M.bro", "Or.tip", "Paint.l", "Pea", "Red.ad", "Ring"),
    `1996` = c(" 88", " 90", " 50", " 48", " 6", "190"),
    `1997` = c(" 47", " 14", " 0", "110", " 3", " 80"),
    `1998` = c("13", "36", " 0", "85", " 8", "96"),
    `1999` = c(" 33", " 24", " 0", " 54", " 10", "179"),
    `2000` = c(" 86", " 47", " 4", " 65", " 15", "145")
)

sample_df %>% 
    mutate(across(.fns = as.numeric)) %>% 
    as.matrix()
#>         1996 1997 1998 1999 2000
#> M.bro     88   47   13   33   86
#> Or.tip    90   14   36   24   47
#> Paint.l   50    0    0    0    4
#> Pea       48  110   85   54   65
#> Red.ad     6    3    8   10   15
#> Ring     190   80   96  179  145

Created on 2022-05-15 by the reprex package (v2.0.1)

BTW, is not necessary to transform a dataset into a matrix in order to plot a barplot, for example

library(tidyverse)

sample_df <- data.frame(
    stringsAsFactors = FALSE,
    check.names = FALSE,
    row.names = c("M.bro", "Or.tip", "Paint.l", "Pea", "Red.ad", "Ring"),
    `1996` = c(" 88", " 90", " 50", " 48", " 6", "190"),
    `1997` = c(" 47", " 14", " 0", "110", " 3", " 80"),
    `1998` = c("13", "36", " 0", "85", " 8", "96"),
    `1999` = c(" 33", " 24", " 0", " 54", " 10", "179"),
    `2000` = c(" 86", " 47", " 4", " 65", " 15", "145")
)

sample_df %>% 
    mutate(across(.fns = as.numeric)) %>% 
    rownames_to_column() %>% 
    pivot_longer(cols = -rowname, names_to = 'Year', values_to = 'Value') %>% 
    ggplot(aes(x = Year, y = Value, fill = rowname)) +
    geom_col()

Created on 2022-05-15 by the reprex package (v2.0.1)

1 Like

Thank you. However:

  1. My data is not character to begin with, it is "integer". So why doesn't it stay that way?
    image

  2. Also, when I tried your solution, the values first became numeric but then when I made it into a matrix the values were then changed to integers. Why?

Well, te sample data you provided contains characters not integers so I assumed it was representative of your actual data.

To help you with specific coding problems we need you to provide a proper REPRoducible EXample (reprex) illustrating your issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.