2017-12-27 88 views
0

我有我用熔體從reshape2並得到了下面的格式數據的收集不工作,而是融化確實爲數據轉換爲多頭形態

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

形式的數據:

> 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 

但是當我使用它收集提供了以下錯誤:

> 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')" 
+0

轉換爲data.frame,然後它應該工作,即'科爾%>%as.data.frame%>%gather..' – akrun

+2

此錯誤消息是關於知識性,因爲它得到。 .. – bouncyball

回答

1

我們可以轉換爲data.frame或tibble,然後做了gather

library(tidyverse) 
m1 %>% 
    as.data.frame %>% 
    rownames_to_column(., 'Var1') %>% 
    gather(Var2, value, -Var1, convert = TRUE) 
+1

我很確定你需要爲'as_tibble'添加一個'rownames'參數。 – A5C1D2H2I1M1N2O1R2T1

相關問題