在R中重新排列从短到长的数据

时间:2014-04-22 17:54:55

标签: r

我有以下方式的数据:

         level1  level2  level3  level4
controls  x_11    x_12    x_13    x_14
cases     x_21    x_22    x_23    x_24

最好的方式是什么?具体来说,我希望x_11行在0级,然后指示0,x_12行在级别指示器0?

我试图将其放入independence_test库中的coin函数以及它所需的数据。谢谢!

修改

我有这个矩阵:

         1 2 3 4
controls 9 7 7 7
cases    0 1 1 5

如何获得37x2的矩阵。每一行都有" status"和" bin"。例如,我会得到9行

0,1(控制,bin = 1)

然后7行: 0,2(对照,bin = 2)

...

0行: 0,1(案例,bin = 1)

1,2(case,bin = 2)

谢谢!

编辑2 将输入/输出输出到以下一个解决方案:

> dput(mtx)
structure(c(9L, 0L, 7L, 1L, 7L, 1L, 7L, 5L), .Dim = c(2L, 4L), .Dimnames = list(
    c("controls", "cases"), c("1", "2", "3", "4")))
  

dput(长)   结构(c(" 1"," 1"," 1"," 1"," 1",& #34; 1"," 1"," 1"," 1"," 3",   " 3"," 3"," 3"," 3"," 3"," 3& #34;," 4"," 1"," 1"," 1"," 1",& #34; 1"," 1",   " 1"," 2"," 3"," 3"," 3"," 3& #34;," 3"," 3"," 3"," 4"," 4",& #34; 4"," 4",   " 4","控制","控制","控制","控制","控制& #34 ;,   "控制","控件","控件","控件","控件","控件& #34 ;,   "控制","控制","控制","控制","控制","案例& #34 ;,   "控制","控件","控件","控件","控件","控件& #34 ;,   "控制","案例","控件","控件","控件","控件& #34 ;,   "控制","控制","控制","案例","案例","案例& #34 ;,   "案例","案例"),. Dim = c(37L,2L),. Dimnames = list(NULL,       c("","状态")))

3 个答案:

答案 0 :(得分:2)

如果您想要将数据从广泛更改为长,melt功能非常有用。我试图创建一个玩具数据集,以便回答你的问题,虽然它可能不是你想要的(如果没有特定的,可重复的示例数据,很难“猜测”某人想要做什么设定)。

首先,我们将在R:

中创建一个玩具数据集
df.wide <- as.data.frame(matrix(1:8,2))
colnames(df.wide) <- c("Level 1", "Level 2", "Level 3", "Level 4")
rownames(df.wide) <- c("Controls", "Cases")

# creating an id variable for the rows
df.wide$id <- rownames(df.wide)

# examining the dataframe
print(df.wide)
Level 1 Level 2 Level 3 Level 4       id
Controls       1       3       5       7 Controls
Cases          2       4       6       8    Cases

现在我们从宽到长转换:

require(reshape2)
df.long <- melt(df.wide)
print(df.long)

id variable value
1 Controls  Level 1     1
2    Cases  Level 1     2
3 Controls  Level 2     3
4    Cases  Level 2     4
5 Controls  Level 3     5
6    Cases  Level 3     6
7 Controls  Level 4     7
8    Cases  Level 4     8

答案 1 :(得分:1)

as.data.frame.table函数和rep函数可以一起执行您想要的操作:

> m <- matrix(1:12, 4)
> df <- as.data.frame.table(m)
> df[ rep(1:nrow(df), df$Freq), ]
      Var1 Var2 Freq
1        A    A    1
2        B    A    2
2.1      B    A    2
3        C    A    3
3.1      C    A    3
3.2      C    A    3
4        D    A    4
4.1      D    A    4
4.2      D    A    4
4.3      D    A    4
5        A    B    5
5.1      A    B    5
.
.
.

另一种选择可能是查看reshap2或plyr包。

答案 2 :(得分:1)

我们假设应变矩阵称为mtx:

     cbind( bin=unlist(mapply( rep, times=mtx, rownames(mtx)[row(mtx)] )), 
            status=unlist(mapply( rep, times=mtx, colnames(mtx)[col(mtx)] ))
          )
  #--------------------------
      bin        status
 [1,] "controls" "1"   
 [2,] "controls" "1"   
 [3,] "controls" "1"   
 [4,] "controls" "1"   
 [5,] "controls" "1"   
 [6,] "controls" "1"   
 [7,] "controls" "1"   
 [8,] "controls" "1"   
 [9,] "controls" "1"   
[10,] "controls" "2"   
[11,] "controls" "2"   
[12,] "controls" "2"   
[13,] "controls" "2"   
[14,] "controls" "2"   
[15,] "controls" "2"   
[16,] "controls" "2"   
[17,] "cases"    "2"   
[18,] "controls" "3"   
[19,] "controls" "3"   
[20,] "controls" "3"   
[21,] "controls" "3"   
[22,] "controls" "3"   
[23,] "controls" "3"   
[24,] "controls" "3"   
[25,] "cases"    "3"   
[26,] "controls" "4"   
[27,] "controls" "4"   
[28,] "controls" "4"   
[29,] "controls" "4"   
[30,] "controls" "4"   
[31,] "controls" "4"   
[32,] "controls" "4"   
[33,] "cases"    "4"   
[34,] "cases"    "4"   
[35,] "cases"    "4"   
[36,] "cases"    "4"   
[37,] "cases"    "4"   

要了解其工作原理,您可以使用这样的矩阵:

dput(mtx)
structure(c(9, 0, 7, 1, 7, 1, 7, 5), .Dim = c(2L, 4L), .Dimnames = list(
    c("controls", "cases"), c("1", "2", "3", "4")))