将 ftable 转换为矩阵

发布于 2024-08-13 05:10:44 字数 695 浏览 2 评论 0原文

以下面的ftable为例,

height <- c(rep('short', 7), rep('tall', 3))
girth <- c(rep('narrow', 4), rep('wide', 6))
measurement <- rnorm(10)
foo <- data.frame(height=height, girth=girth, measurement=measurement)
ftable.result <- ftable(foo$height, foo$girth)

我想将上面的ftable.result转换为具有行名称和列名称的矩阵。有没有有效的方法来做到这一点? as.matrix() 并不完全有效,因为它不会为您附加行名称和列名称。

您可以执行以下操作

ftable.matrix <- ftable.result
class(ftable.matrix) <- 'matrix'

rownames(ftable.matrix) <- unlist(attr(ftable.result, 'row.vars'))
colnames(ftable.matrix) <- unlist(attr(ftable.result, 'col.vars'))

,但是,这似乎有点严厉。有没有更有效的方法来做到这一点?

Take for example the following ftable

height <- c(rep('short', 7), rep('tall', 3))
girth <- c(rep('narrow', 4), rep('wide', 6))
measurement <- rnorm(10)
foo <- data.frame(height=height, girth=girth, measurement=measurement)
ftable.result <- ftable(foo$height, foo$girth)

I'd like to convert the above ftable.result into a matrix with row names and column names. Is there an efficient way of doing this? as.matrix() doesn't exactly work, since it won't attach the row names and column names for you.

You could do the following

ftable.matrix <- ftable.result
class(ftable.matrix) <- 'matrix'

rownames(ftable.matrix) <- unlist(attr(ftable.result, 'row.vars'))
colnames(ftable.matrix) <- unlist(attr(ftable.result, 'col.vars'))

However, it seems a bit heavy-handed. Is there a more efficient way of doing this?

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

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

发布评论

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

评论(2

疑心病 2024-08-20 05:10:44

事实证明,@Shane 最初发布了(但很快就删除了)最新版本 R 的正确答案。

在此过程中的某个地方,出现了一个 as.matrix 方法已为 ftable 添加(尽管我在阅读的新闻文件中没有找到它。

ftableas.matrix 方法可以让您很好地处理“嵌套”频率表(这是 ftable 很好地创建的)。考虑以下内容:

temp <- read.ftable(textConnection("breathless yes no
coughed yes no
age
20-24  9  7  95 1841
25-29 23  9 108 1654
30-34 54 19 177 1863"))

class(temp)
# [1] "ftable"

head(as.table(...), Inf) 技巧不适用于此类 ftables,因为 as.table 会将结果转换为多维数组

head(as.table(temp), Inf)
#  [1]    9   23   54   95  108  177    7    9   19 1841 1654 1863

出于同样的原因,第二个建议也不起作用。工作:

t <- as.table(temp)
class(t) <- "matrix"
# Error in class(t) <- "matrix" : 
#   invalid to set the class to matrix unless the dimension attribute is of length 2 (was 3)

但是,对于更新版本的 R,只需使用 as.matrix 就可以了:

as.matrix(temp)
#        breathless_coughed
# age     yes_yes yes_no no_yes no_no
#   20-24       9      7     95  1841
#   25-29      23      9    108  1654
#   30-34      54     19    177  1863

class(.Last.value)
# [1] "matrix"

如果您更喜欢 data.frame 而不是 matrix >,从我的 GitHub 上的 "mrdwabmisc" 包中查看 table2df

It turns out that @Shane had originally posted (but quickly deleted) what is a correct answer with more recent versions of R.

Somewhere along the way, an as.matrix method was added for ftable (I haven't found it in the NEWS files I read through though.

The as.matrix method for ftable lets you deal fairly nicely with "nested" frequency tables (which is what ftable creates quite nicely). Consider the following:

temp <- read.ftable(textConnection("breathless yes no
coughed yes no
age
20-24  9  7  95 1841
25-29 23  9 108 1654
30-34 54 19 177 1863"))

class(temp)
# [1] "ftable"

The head(as.table(...), Inf) trick doesn't work with such ftables because as.table would convert the result into a multi-dimensional array.

head(as.table(temp), Inf)
#  [1]    9   23   54   95  108  177    7    9   19 1841 1654 1863

For the same reason, the second suggestion also doesn't work:

t <- as.table(temp)
class(t) <- "matrix"
# Error in class(t) <- "matrix" : 
#   invalid to set the class to matrix unless the dimension attribute is of length 2 (was 3)

However, with more recent versions of R, simply using as.matrix would be fine:

as.matrix(temp)
#        breathless_coughed
# age     yes_yes yes_no no_yes no_no
#   20-24       9      7     95  1841
#   25-29      23      9    108  1654
#   30-34      54     19    177  1863

class(.Last.value)
# [1] "matrix"

If you prefer a data.frame to a matrix, check out table2df from my "mrdwabmisc" package on GitHub.

浪漫人生路 2024-08-20 05:10:44

我找到了2个解决方案在 R-Help 上:

head(as.table(ftable.result), Inf)

或者

t <- as.table(ftable.result)
class(t) <- "matrix"

I found 2 solutions on R-Help:

head(as.table(ftable.result), Inf)

Or

t <- as.table(ftable.result)
class(t) <- "matrix"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文