将 ftable(列联表)转换为 R 中的数据帧

发布于 2024-11-16 20:14:00 字数 1333 浏览 3 评论 0原文

我正在生成一个ftable(通过在xtabs命令的结果上运行ftable)并且我得到以下内容。

                                                                   Var1   Var2
date                 group                                                 
2007-01-01           q1                                              1     9
                     q2                                              2     8
                     q3                                              3     7
2007-01-02           q1                                              6     6
                     q2                                              7     5
                     q3                                              8     4

我知道它是一个可ftable 类,但我想存储如下。我想知道在R中是否有任何有效的方法可以做到这一点?

date                 group                                         Var1   Var2          
2007-01-01           q1                                              1     9
2007-01-01           q2                                              2     8
2007-01-01           q3                                              3     7
2007-01-02           q1                                              6     6
2007-01-02           q2                                              7     5
2007-01-02           q3                                              8     4

I am generating an ftable (by running ftable on the results of a xtabs command) and I am getting the following.

                                                                   Var1   Var2
date                 group                                                 
2007-01-01           q1                                              1     9
                     q2                                              2     8
                     q3                                              3     7
2007-01-02           q1                                              6     6
                     q2                                              7     5
                     q3                                              8     4

I understand that it is a ftable class but I'd like to store is as the following. I am wondering if there is any efficient way of doing it in R?

date                 group                                         Var1   Var2          
2007-01-01           q1                                              1     9
2007-01-01           q2                                              2     8
2007-01-01           q3                                              3     7
2007-01-02           q1                                              6     6
2007-01-02           q2                                              7     5
2007-01-02           q3                                              8     4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短暂陪伴 2024-11-23 20:14:00

你可以通过以下方式完成这项工作:

> # from ?ftable
> r <- ftable(Titanic, row.vars = 1:3)
> r
                   Survived  No Yes
Class Sex    Age                   
1st   Male   Child            0   5
             Adult          118  57

... snip ...

      Female Child            0   0
             Adult            3  20
> 
> # long format
> as.data.frame(r)
   Class    Sex   Age Survived Freq
1    1st   Male Child       No    0
2    2nd   Male Child       No    0
3    3rd   Male Child       No   35

... snip ...

30   2nd Female Adult      Yes   80
31   3rd Female Adult      Yes   76
32  Crew Female Adult      Yes   20
> 
> # wide format, but do not care the col name
> data.frame(expand.grid(rev(attr(r, "row.vars"))), unclass(r))
     Age    Sex Class  X1  X2
1  Child   Male   1st   0   5
2  Adult   Male   1st 118  57
3  Child Female   1st   0   1

... snip ...

14 Adult   Male  Crew 670 192
15 Child Female  Crew   0   0
16 Adult Female  Crew   3  20
> 
> # using reshape2 library
> library(reshape2)
> dcast(as.data.frame(r), as.formula(paste(paste(names(attr(r, "row.vars")), collapse="+"), "~", paste(names(attr(r, "col.vars"))))))
Using Freq as value column: use value_var to override.
   Class    Sex   Age  No Yes
1    1st   Male Child   0   5
2    1st   Male Adult 118  57
3    1st Female Child   0   1

... snip ...

14  Crew   Male Adult 670 192
15  Crew Female Child   0   0
16  Crew Female Adult   3  20

you can do that work by:

> # from ?ftable
> r <- ftable(Titanic, row.vars = 1:3)
> r
                   Survived  No Yes
Class Sex    Age                   
1st   Male   Child            0   5
             Adult          118  57

... snip ...

      Female Child            0   0
             Adult            3  20
> 
> # long format
> as.data.frame(r)
   Class    Sex   Age Survived Freq
1    1st   Male Child       No    0
2    2nd   Male Child       No    0
3    3rd   Male Child       No   35

... snip ...

30   2nd Female Adult      Yes   80
31   3rd Female Adult      Yes   76
32  Crew Female Adult      Yes   20
> 
> # wide format, but do not care the col name
> data.frame(expand.grid(rev(attr(r, "row.vars"))), unclass(r))
     Age    Sex Class  X1  X2
1  Child   Male   1st   0   5
2  Adult   Male   1st 118  57
3  Child Female   1st   0   1

... snip ...

14 Adult   Male  Crew 670 192
15 Child Female  Crew   0   0
16 Adult Female  Crew   3  20
> 
> # using reshape2 library
> library(reshape2)
> dcast(as.data.frame(r), as.formula(paste(paste(names(attr(r, "row.vars")), collapse="+"), "~", paste(names(attr(r, "col.vars"))))))
Using Freq as value column: use value_var to override.
   Class    Sex   Age  No Yes
1    1st   Male Child   0   5
2    1st   Male Adult 118  57
3    1st Female Child   0   1

... snip ...

14  Crew   Male Adult 670 192
15  Crew Female Child   0   0
16  Crew Female Adult   3  20
高速公鹿 2024-11-23 20:14:00

我知道这个问题很老了,但如果它对某人有帮助:

ftable1 = ftable(Titanic, row.vars = 1:3)
library("metrumrg")
df1 = ftable2data.frame(ftable1)
df1
class(df1)

I know this question is old, but in case it helps someone:

ftable1 = ftable(Titanic, row.vars = 1:3)
library("metrumrg")
df1 = ftable2data.frame(ftable1)
df1
class(df1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文