我有一个如下所示的 data.frame:
name1 feat x y perc.x perc.y
1 foo z 100 10 0.1 1
2 bar w 200 20 0.2 2
3 qux x 300 30 0.3 3
4 qux y 400 40 0.4 4
5 bar v 500 50 0.5 5
它是使用以下代码生成的:
name1 <- c("foo","bar","qux","qux","bar")
feat <- c("z","w","x","y","v")
x <- c(100,200,300,400,500)
y <- c(10,20,30,40,50)
perc.x <- c(0.1,0.2,0.3,0.4,0.5)
perc.y <- c(1,2,3,4,5)
df <- data.frame(name1,feat,x,y,perc.x,perc.y)
df
如何创建这样的融化数据:
name1 feat variable value value2.perc
1 foo z x 100.0 0.1
2 bar w x 200.0 0.2
3 qux x x 300.0 0.3
4 qux y x 400.0 0.4
5 bar v x 500.0 0.5
6 foo z y 10.0 1
7 bar w y 20.0 2
8 qux x y 30.0 3
9 qux y y 40.0 4
10 bar v y 50.0 5
我试过这个但失败了:
library(reshape2)
melt(df)