【问题标题】:Writing results from "desribe" into a matrix changed the Matrix in R-Studio from Data to Value将“desribe”中的结果写入矩阵将 R-Studio 中的矩阵从数据更改为值
【发布时间】:2017-05-23 14:06:52
【问题描述】:

我从 1 个月开始学习“r”并使用 r-Studio。现在我有以下问题:

我想用“describe”函数来描述一个数据集。之后,我想将结果写入一个用函数 Matrix 定义的表中。 我将此表写入变量“Result_table

如果使用“tapply”功能,我就没有这个问题了。我在定义的表格单元格中有定义的结果。

难道不能将“describe”函数的结果写入用 Matrix 函数描述的表中吗?

【问题讨论】:

  • 由于问题可能出在您的“描述”函数或您将函数结果写入矩阵的方式中,因此一个可重现的示例将很有帮助,因为在 R 中有多种方法可以做到这一点同样的事情。

标签: r matrix tapply describe


【解决方案1】:

感谢您的回答。代码如下:

#Package
library("psych")

#Table
Results <- Matrix(nrow = 2, ncol = 2)
colnames(Results) <- c("mean","sd")
rownames(Results) <- c("2010","2011")
View(Results)

#Calculate mean
mean2010 <- describe(sba$SalesNet[sba$Year=="2010")
mean2011 <- describe(sba$SalesNet[sba$Year=="2011")
(mean2010[3])
(mean2010[4])
(mean2011[3])
(mean2011[4])

#Writing Results
Results[1,1] <- mean2010[3]
Results[2,1] <- mean2011[3]
Results[1,2] <- mean2010[4]
Results[2,2] <- mean2011[4]
View(Results)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-02
    • 2011-06-01
    • 2017-03-29
    • 2017-05-09
    • 2021-05-24
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多