1

I have data of the form of

           menthlth    avedrnk2     alcday5
menthlth 1.00000000  0.07997551  0.03524646
avedrnk2 0.07997551  1.00000000 -0.02859211
alcday5  0.03524646 -0.02859211  1.00000000

i used melt from reshape2 and got data in the following format:

> melt(corr)
      Var1     Var2       value
1 menthlth menthlth  1.00000000
2 avedrnk2 menthlth  0.07997551
3  alcday5 menthlth  0.03524646
4 menthlth avedrnk2  0.07997551
5 avedrnk2 avedrnk2  1.00000000
6  alcday5 avedrnk2 -0.02859211
7 menthlth  alcday5  0.03524646
8 avedrnk2  alcday5 -0.02859211
9  alcday5  alcday5  1.00000000

But when i use gather it gives the following error:

> corr %>% gather(Var1,Var2,convert = TRUE)
Error in UseMethod("gather_") : 
  no applicable method for 'gather_' applied to an object of class "c('matrix', 'double', 'numeric')"

1 Answers1

2

We can either convert to a data.frame or a tibble and then do the gather

library(tidyverse)
m1 %>%
  as.data.frame  %>% 
  rownames_to_column(., 'Var1') %>% 
  gather(Var2, value, -Var1, convert = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662