- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图在 中获得一个 2 路表R 类似于Stata的这个。我试图使用 CrossTable
来自 gmodels
包,但表不一样。您知道如何在 中完成此操作吗? R ?
我希望至少能从
当 cursmoke1 == "Yes"& cursmoke2 == "No"并反转时
在 R 我只从 yes、no 和 NA 中获得总数。
这是输出:
斯塔塔
. tabulate cursmoke1 cursmoke2, cell column miss row
+-------------------+
| Key |
|-------------------|
| frequency |
| row percentage |
| column percentage |
| cell percentage |
+-------------------+
Current |
smoker, | Current smoker, exam 2
exam 1 | No Yes . | Total
-----------+---------------------------------+----------
No | 1,898 131 224 | 2,253
| 84.24 5.81 9.94 | 100.00
| 86.16 7.59 44.44 | 50.81
| 42.81 2.95 5.05 | 50.81
-----------+---------------------------------+----------
Yes | 305 1,596 280 | 2,181
| 13.98 73.18 12.84 | 100.00
| 13.84 92.41 55.56 | 49.19
| 6.88 35.99 6.31 | 49.19
-----------+---------------------------------+----------
Total | 2,203 1,727 504 | 4,434
| 49.68 38.95 11.37 | 100.00
| 100.00 100.00 100.00 | 100.00
| 49.68 38.95 11.37 | 100.00
> CrossTable(cursmoke2, cursmoke1, missing.include = T, format="SAS")
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 4434
| cursmoke1
cursmoke2 | No | Yes | NA | Row Total |
-------------|-----------|-----------|-----------|-----------|
No | 2203 | 0 | 0 | 2203 |
| 1122.544 | 858.047 | 250.409 | |
| 1.000 | 0.000 | 0.000 | 0.497 |
| 1.000 | 0.000 | 0.000 | |
| 0.497 | 0.000 | 0.000 | |
-------------|-----------|-----------|-----------|-----------|
Yes | 0 | 1727 | 0 | 1727 |
| 858.047 | 1652.650 | 196.303 | |
| 0.000 | 1.000 | 0.000 | 0.389 |
| 0.000 | 1.000 | 0.000 | |
| 0.000 | 0.389 | 0.000 | |
-------------|-----------|-----------|-----------|-----------|
NA | 0 | 0 | 504 | 504 |
| 250.409 | 196.303 | 3483.288 | |
| 0.000 | 0.000 | 1.000 | 0.114 |
| 0.000 | 0.000 | 1.000 | |
| 0.000 | 0.000 | 0.114 | |
-------------|-----------|-----------|-----------|-----------|
Column Total | 2203 | 1727 | 504 | 4434 |
| 0.497 | 0.389 | 0.114 | |
-------------|-----------|-----------|-----------|-----------|
最佳答案
也许我在这里遗漏了一些东西。 CrossTable
的默认设置似乎基本上提供了您正在寻找的内容。
这是CrossTable
用最少的参数。 (我已将数据集加载为“temp”。)请注意,结果与您从 Stata 输出发布的结果相同(如果您希望结果为百分比,则只需乘以 100)。
library(gmodels)
with(temp, CrossTable(cursmoke1, cursmoke2, missing.include=TRUE))
Cell Contents
|-------------------------|
| N |
| Chi-square contribution |
| N / Row Total |
| N / Col Total |
| N / Table Total |
|-------------------------|
Total Observations in Table: 4434
| cursmoke2
cursmoke1 | No | Yes | NA | Row Total |
-------------|-----------|-----------|-----------|-----------|
No | 1898 | 131 | 224 | 2253 |
| 541.582 | 635.078 | 4.022 | |
| 0.842 | 0.058 | 0.099 | 0.508 |
| 0.862 | 0.076 | 0.444 | |
| 0.428 | 0.030 | 0.051 | |
-------------|-----------|-----------|-----------|-----------|
Yes | 305 | 1596 | 280 | 2181 |
| 559.461 | 656.043 | 4.154 | |
| 0.140 | 0.732 | 0.128 | 0.492 |
| 0.138 | 0.924 | 0.556 | |
| 0.069 | 0.360 | 0.063 | |
-------------|-----------|-----------|-----------|-----------|
Column Total | 2203 | 1727 | 504 | 4434 |
| 0.497 | 0.389 | 0.114 | |
-------------|-----------|-----------|-----------|-----------|
format="SPSS"
如果您希望数字显示为百分比。
with(temp, CrossTable(cursmoke1, cursmoke2, missing.include=TRUE, format="SPSS"))
Cell Contents
|-------------------------|
| Count |
| Chi-square contribution |
| Row Percent |
| Column Percent |
| Total Percent |
|-------------------------|
Total Observations in Table: 4434
| cursmoke2
cursmoke1 | No | Yes | NA | Row Total |
-------------|-----------|-----------|-----------|-----------|
No | 1898 | 131 | 224 | 2253 |
| 541.582 | 635.078 | 4.022 | |
| 84.243% | 5.814% | 9.942% | 50.812% |
| 86.155% | 7.585% | 44.444% | |
| 42.806% | 2.954% | 5.052% | |
-------------|-----------|-----------|-----------|-----------|
Yes | 305 | 1596 | 280 | 2181 |
| 559.461 | 656.043 | 4.154 | |
| 13.984% | 73.177% | 12.838% | 49.188% |
| 13.845% | 92.415% | 55.556% | |
| 6.879% | 35.995% | 6.315% | |
-------------|-----------|-----------|-----------|-----------|
Column Total | 2203 | 1727 | 504 | 4434 |
| 49.684% | 38.949% | 11.367% | |
-------------|-----------|-----------|-----------|-----------|
prop.table()
data.frame
时所做的繁琐工作),您可能也对
prop.table()
感兴趣。功能。
data.frame
.您可能还对查看函数感兴趣
margin.table()
或
addmargins()
:
## Your basic table
CurSmoke <- with(temp, table(cursmoke1, cursmoke2, useNA = "ifany"))
CurSmoke
# cursmoke2
# cursmoke1 No Yes <NA>
# No 1898 131 224
# Yes 305 1596 280
## Row proportions
prop.table(CurSmoke, 1) # * 100 # If you so desire
# cursmoke2
# cursmoke1 No Yes <NA>
# No 0.84243231 0.05814470 0.09942299
# Yes 0.13984411 0.73177442 0.12838148
## Column proportions
prop.table(CurSmoke, 2) # * 100 # If you so desire
# cursmoke2
# cursmoke1 No Yes <NA>
# No 0.86155243 0.07585408 0.44444444
# Yes 0.13844757 0.92414592 0.55555556
## Cell proportions
prop.table(CurSmoke) # * 100 # If you so desire
# cursmoke2
# cursmoke1 No Yes <NA>
# No 0.42805593 0.02954443 0.05051872
# Yes 0.06878665 0.35994587 0.06314840
关于r - 在 R 中模仿来自 Stata 的制表命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13043817/
为什么有两个不同的命令来生成新变量的原因? 有什么简单的方法可以记住何时使用gen和何时使用egen? 最佳答案 它们都创建一个新变量,但是使用不同的函数集。当您对数据集中的其他变量进行简单转换时,通
给我谜语: scalar bah = .1*1000.01 display bah 1 scalar list bah = 100.001 display 似乎是故意显示错误的数字。
假设我有一个包含一些变量名的本地宏 local indep "cat dog pig" 如果我希望宏只包含 cat 和 dog,如何从宏中删除 pig? 最佳答案 上下文是您想要从列出它们的字符串中删
一些数据集带有全小写的值标签,我最终得到的图表和表格显示了“埃及”、“约旦”和“沙特阿拉伯”的结果,而不是大写的国家名称。 我猜是proper() string 函数可以为我做一些事情,但我没有找到正
我最近购买了在 Linux 服务器上运行的 Stata MP12(8 核)许可证。 有没有人写过 Stata 程序,比如说模拟研究来测试 Stata MP 的性能?我想监视在作业处理过程中实际使用的内
我不想用这个问题来打扰你们,但我收到错误“{ required”,即使我的所有循环似乎都正确打开(和关闭),不幸的是 Stata 没有告诉你错误在哪里,所以我不明白为什么会这样。顺便说一句,如果我用
编辑:下面提供了一个工作示例 原文:后估计命令可用于预测因变量的值。这是一个示例,您可以在其中键入 _b[_cons] + _b[x1]*1 + _b[x2] 以获得 Y 的实际值。对于 Stata
我正在绘制时间序列并根据 Windows 7 上的 Stata 13.1 中的 bool 指标变量对某些时间段进行着色。如果指标变量等于 1,则对时间段进行着色,如果缺少则不着色。我想在 y 轴上时间
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
熟悉 R,我想知道向量的 Stata 等价物是什么(例如 var<-c(1,2,3,4) )? 我知道 Stata 使用宏,但我不知道如何在一个宏中存储多个值(创建宏的数组类型)。 我想遍历一些字符串
我在综合辨别力改善 (IDI) 方面得到了两个截然不同的结果。 如果我使用 nriidi.pkg 包并运行 idi 我会得到此输出 . idi totaloutcome grace_prob,
我正在使用一个基本上是 URL 的变量。因此,观察结果包括诸如此类的值 www.google.com https://www.google.com https://yahoo.movies.com
我目前有 2 个变量,state 和 year,我希望将它们转换成 1 个变量,stateyear。 我希望 stateyear 变量具有以下形式的值:state_year(例如 Texas_1962
我正在使用一个基本上是 URL 的变量。所以观察包括例如值 www.google.com https://www.google.com https://yahoo.movies.com 我正在尝试
我在工作中遇到了一个问题:我合并了两个数据集,并且有许多变量具有相同的内容,但是在数据集 1 的变量中具有值的观察在数据集中具有缺失值2. 所以我需要将值从一个变量转移到另一个变量。 这是我迄今为止最
我有一个格式如下所示的日期,我使用 R 将它从字符串转换为日期 date <- "20 Nov 2010 21:44:00:000" strptime(date,"%d %b %Y %H:%M:%S"
我有面板数据(时间: 日期,名称: 代码)。我想为变量 x 和 y 创建 10 个滞后。现在,我使用以下代码一个一个地创建每个滞后变量: by ticker: gen lag1 = x[_n-1] 但
在 Stata ,我想运行一个回归并直观地显示每个变量的系数及其相对于零的置信区间,如下所示的代码和图: sysuse auto regress price mpg weight length for
我有一个带有变量 x 的文件 aa,该变量标有值标签 x_lab。我想在Stata文件bb的变量x上使用这个值标签: use bb, clear label value x x_lab 如何导入值标签
假设我的当前工作文件夹有一个很长的文件路径(80 多个字符): use .\random_folders_name\project1\secret_data\survey_data\big_const
我是一名优秀的程序员,十分优秀!