2012-09-12 51 views
0

我发现这个博客的嵌入式表格变成乳胶格式(Blog Link)。我喜欢结果,但想在rownames后面的开头插入一个列。我习惯于处理数据帧,因此处理这个野兽比典型的列索引更困难。在表格对象中插入一列

这是我现在有:

  pre   post  
approach mean sd mean sd 
1  24.17 8.310 54.33 11.01 
2  25.50 9.434 65.25 16.32 
3  26.33 9.139 63.17 12.53 

这里还有想什么我它看起来像:

   pre   post  
approach n mean sd mean sd 
1  12 24.17 8.310 54.33 11.01 
2  12 25.50 9.434 65.25 16.32 
3  12 26.33 9.139 63.17 12.53 

这里是z的dput以及N-就是我想要的列喜欢插入。

预先感谢您。

z <- structure(list(24.1666666666667, 25.5, 26.3333333333333, 8.31027111835746, 
    9.4339811320566, 9.13866245766587, 54.3333333333333, 65.25, 
    63.1666666666667, 11.0068848977136, 16.3157759685081, 12.5323822978956), .Dim = 3:4, .Dimnames = list(
    NULL, c("term", "term", "term", "term")), rowLabels = structure(c("1", 
"2", "3"), .Dim = c(3L, 1L), .Dimnames = list(NULL, "approach"), justification = structure(c(NA_character_, 
NA_character_, NA_character_), .Dim = c(3L, 1L)), colnamejust = NA_character_, justify = NA, suppress = 0), colLabels = structure(c("pre", 
"mean", NA, "sd", "post", "mean", NA, "sd"), .Dim = c(2L, 4L), justification = structure(c(NA_character_, 
NA_character_, NA_character_, NA_character_, NA_character_, NA_character_, 
NA_character_, NA_character_), .Dim = c(2L, 4L)), colnamejust = character(0), justify = NA, suppress = 0), table = value * 
    v * approach ~ variable2 * result_variable, formats = structure(c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = 3:4, .Dimnames = list(
    NULL, c("format", "format", "format", "format"))), justification = structure(c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Dim = 3:4, .Dimnames = list(
    NULL, c("justification", "justification", "justification", 
    "justification"))), class = "tabular") 

structure(c(12L, 12L, 12L), .Names = c("1", "2", "3")) 
+0

我想实际了解未来需求的结构。 –

+0

你尝试过你发布的'structure'吗?它确实给了你显示的data.frame。 –

回答

2

唯一的(?知道)的方法是重新分配重新排序:

R> mockup <- data.frame(B=21:23, C=31:33) 
R> mockup 
    B C 
1 21 31 
2 22 32 
3 23 33 
R> 

现在添加列A:

R> mockup[,"A"] <- 1:3 
R> mockup 
    B C A 
1 21 31 1 
2 22 32 2 
3 23 33 3 
R> 

和重新排列:

R> mockup <- mockup[,c("A", "B", "C")] 
R> mockup 
    A B C 
1 1 21 31 
2 2 22 32 
3 3 23 33 
R> 

Presto。开始的新列。

+0

我做了'str',看到了列表,并尝试了一些列表索引'[[]]',它不起作用。 “表格式”不起作用,我假设(不正确)它不是data.frame对象(忘记data.frame是列表)。完美的作品,对我来说无疑是一个时刻。 –

1

事情是这样的:

z <- data.frame(approach = gl(3, 12), pre = rnorm(36)*50, post = rnorm(36)*60) 
library(tables) 
tabular(approach ~ (pre + post) * (mean + sd)) 

     pre   post   
approach mean sd mean sd 
1  -5.431 61.01 3.766 54.76 
2  20.408 29.14 -9.261 54.58 
3  -7.854 53.55 -30.046 62.41 
tabular(approach ~ (n=1) + (pre + post) * (mean + sd)) 

    pre   post   
approach n mean sd mean sd 
1  12 -5.431 61.01 3.766 54.76 
2  12 20.408 29.14 -9.261 54.58 
3  12 -7.854 53.55 -30.046 62.41 
tabular(approach + 1 ~ (n=1) + (pre + post) * (mean + sd)) 

    pre   post   
approach n mean sd mean sd 
1  12 -5.431 61.01 3.766 54.76 
2  12 20.408 29.14 -9.261 54.58 
3  12 -7.854 53.55 -30.046 62.41 
All  36 2.374 50.06 -11.847 57.46 

欲了解更多详情,请参阅该tablesvignette