0

I've a data frame with both characters and numbers and wish to convert it into a numeric matrix (As I need to make a heatmap out of it)

I've tried data.matrix and as.matrix functions but both are giving errors.

The data is currently in this form

season     team          average
1           a               45
1           b               32
1           c               15
2           a               43
2           b               45
2           c               34
3           a               45

and so on

And I wish to convert it into this

season       a           b             c
1            45          32            15
2            43          45            34
3            45          ...          ... 

Sorry, If I've edited the question wrongly, I'm a bit new to both this platform and R

wibeasley
  • 5,000
  • 3
  • 34
  • 62

1 Answers1

0

You can re-shape your data frame with spread:

library(tidyr)

(df2 <- spread(df, key = team, value = average))

  season  a  b  c
1      1 45 32 15
2      2 43 45 34
3      3 45 NA NA

and then turn it into a matrix:

as.matrix(df2)
Paul
  • 2,877
  • 1
  • 12
  • 28