r - How to name each variable using melt -


i have matrix want reform plotting in ggplo2 using melt function reshape2 cannot find way add custom header names.

#create toy data mydata <- matrix(rnorm(15,500), nrow = 5, ncol = 3, dimnames = list(     c("unknown","0-4","4-9","10-14","15-19"),c("area1","area2","area3")))  dat2 <- melt(mydata, value.name = "count")  #reform data using melt, define count value name mydata2 <- melt(mydata, value.name = "count") 

this gets me want operations follow have refere var1 , var2.

i tried naming them explicitly using variable.name:

mydata2 <- melt(mydata, value.name = "count",     variable.name = c("agegroup", "geo")) 

i can of course name them after fact using colnames() using melt. possible? need up?

thanks!

use varnames argument:

melt(mydata, value.name = "count", varnames=c('agegroup', 'geo'))    agegroup   geo    count 1   unknown area1 501.6685 2       0-4 area1 499.2812 3       4-9 area1 500.3892 4     10-14 area1 498.6380 5     15-19 area1 500.5904 6   unknown area2 499.4590 7       0-4 area2 500.5464 8       4-9 area2 500.5635 9     10-14 area2 500.7211 10    15-19 area2 500.8381 11  unknown area3 498.8154 12      0-4 area3 499.1818 13      4-9 area3 499.6678 14    10-14 area3 499.3586 15    15-19 area3 500.3962 

your mydata matrix (so uses melt.array uses varnames) not dataframe (melt.data.frame uses variable.name). ?melt.array.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -