0

I have a simple dataset of features cost and value...

enter image description here

I would like to create a matrix where Cost is ascending across the top and value is ascending down the left. The Feature name is displayed in the middle as below...

enter image description here

I feel it should be quite easy with ggplot2, but my r is very rusty and would appreciate some help.

Thanks, Gary

Gary
  • 61
  • 7

1 Answers1

0
library(reshape2)

df = data.frame(Feature = paste0("F", 1:5), Cost = c(10, 100, 20, 8, 8), Value = c(150, 150, 350, 1000, 200))

> df
  Feature Cost Value
1      F1   10   150
2      F2  100   150
3      F3   20   350
4      F4    8  1000
5      F5    8   200

dcast(df, Value ~ Cost, value.var = "Feature")

  Value    8   10   20  100
1   150 <NA>   F1 <NA>   F2
2   200   F5 <NA> <NA> <NA>
3   350 <NA> <NA>   F3 <NA>
4  1000   F4 <NA> <NA> <NA>
Vlo
  • 3,168
  • 13
  • 27
  • That's great thank you. I remember using dcast in the past now. – Gary Sep 01 '16 at 14:37
  • I would like to. I've upvoted but it doesn't show as my rep is low. I can't accept as it's been marked as a duplicate question. – Gary Sep 01 '16 at 15:28