我的数据如下:
col1 col2 col3
col1 100 25 30
col2 25 200 50
col3 30 50 300
其中列和行名称为c(“ col1”,“ col2”,“ col3”)
我希望它看起来像这样:
Var1 Var2 Value
col1 col1 100
col1 col2 25
col1 col3 30
col2 col1 25
col2 col2 200
col2 col3 50
col3 col1 30
col3 col2 50
col3 col3 300
我正在尝试reshape2 :: melt,但是它没有给我我正在寻找的结果melt(df)
,我们将为您提供任何帮助!
谢谢。
答案 0 :(得分:2)
为base R
创建属性后,我们可以将as.data.frame
与table
一起使用
as.data.frame.table(m1)
# Var1 Var2 Freq
#1 col1 col1 100
#2 col2 col1 25
#3 col3 col1 30
#4 col1 col2 25
#5 col2 col2 200
#6 col3 col2 50
#7 col1 col3 30
#8 col2 col3 50
#9 col3 col3 300
m1 <- structure(c(100L, 25L, 30L, 25L, 200L, 50L, 30L, 50L, 300L),
.Dim = c(3L,
3L), .Dimnames = list(c("col1", "col2", "col3"), c("col1", "col2",
"col3")))
答案 1 :(得分:0)
您需要tidyr包裹
'//*[name()="symbol" and @id="activity"]'