Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 表%来自不同子集的分类变量_R - Fatal编程技术网

R 表%来自不同子集的分类变量

R 表%来自不同子集的分类变量,r,R,我有一个数据集,其中包含来自csv的15个指标(列)。1个指标称为癌症 这就是数据集中的列的外观 Cancer: yes no yes no 我想创建一个带有百分比的表。是否 但我正在使用dplyr创建不同的子集(例如过滤数据集1:agegroup 50-54和numberrelatives=1,过滤数据集2:agebirtfirstchild),您可以执行以下操作: df %>% group_by(agegroup, numberrelatives, agefirstchild

我有一个数据集,其中包含来自csv的15个指标(列)。1个指标称为癌症

这就是数据集中的列的外观

Cancer:  yes no yes no
我想创建一个带有百分比的表。是否
但我正在使用
dplyr创建不同的子集(例如过滤数据集1:agegroup 50-54和numberrelatives=1,过滤数据集2:agebirtfirstchild),您可以执行以下操作:

df %>%
   group_by(agegroup, numberrelatives, agefirstchild) %>%
   summarize(prop_cancer = mean(cancer == 'yes'))

请注意,表格的格式很长(但也有一些方法可以使其更宽)。

以下是一些使用base R的方法。但首先,我们需要一些可复制的数据:

set.seed(42)
cancer <- sample(c("yes", "no"), 200, replace=TRUE) 
agegroup <- sample(c("35-39", "40-44", "45-49"), 200, replace=TRUE)  
numberrelatives <- sample(c("zero", "one", "2 or more"), 200, replace=TRUE)  
agefirstchild <- sample(c("Age < 30", "Age 30 or greater", "nullipareous"), 200, replace=TRUE) 
dat <- data.frame(cancer, agegroup, numberrelatives, agefirstchild)

是的,谢谢,它在没有分组依据的情况下部分工作,它给了我1个数据帧/数据集的摘要。但是我想绘制不同的数据 表1中的数据帧/过滤数据集-> 已筛选数据集1否%
过滤数据集2 no%

您为示例数据集提供的向量具有不同的行数-无法从中生成数据帧。可以使用
rep()
函数将每个向量重复几次,以便它们都具有相同的长度(例如6、4、4和4次)阅读以下功能:
表格
xtabs
道具表格
添加边距
边距.表格
。您可以提出问题更具体地说,通过使用
dput
包含一个数据样本,或者通过指示R中包含的一个数据集,该数据集的数据与您的数据相似。如果您在示例中实际运行代码,您将看到它没有运行。是的,感谢它在没有group by的情况下部分工作,它为我提供了来自1个数据帧/数据集的摘要。但是我将I’我想在1表->过滤数据集1否%过滤数据集2否%中绘制不同的过滤数据帧/过滤数据集。您还可以给出部分分数:)您可以对任意数量的变量使用相同的方法。例如,您可以分别对
agegroup
numberrelatives的“主要效果”执行
groupby(agegroup)
groupby(numberrelatives)
)。按照这种逻辑,当你同时对更多的变量进行分组时,你正在检查你分组所依据的分类变量之间的相互作用。是的,这确实也是它需要做的。谢谢我投票认为你的答案是一个解决方案,但有人关闭了这个问题,因为它已经得到了回答:(因此认为它再次变灰非常漂亮!它让我们对亚组中的问题有了很好的了解。谢谢!是否可以将agegroup+cancer(例如agefirstchild列)添加到表中。因此您可以查看组合?agefirstchild agegroup no是的。例如,尝试
xtabs(agegroup+agefirstchild+cancer,dat)
。这将创建一个三维表格。要自定义哪些变量是行,哪些是列,请使用
ftable()
。我投票认为你的答案是一个解决方案,但有人关闭了这个问题,因为它已经得到了回答:(我想它因此又变成了灰色
set.seed(42)
cancer <- sample(c("yes", "no"), 200, replace=TRUE) 
agegroup <- sample(c("35-39", "40-44", "45-49"), 200, replace=TRUE)  
numberrelatives <- sample(c("zero", "one", "2 or more"), 200, replace=TRUE)  
agefirstchild <- sample(c("Age < 30", "Age 30 or greater", "nullipareous"), 200, replace=TRUE) 
dat <- data.frame(cancer, agegroup, numberrelatives, agefirstchild)
(tbl <- xtabs(~agegroup+cancer, dat))
#         cancer
# agegroup no yes
#    35-39 38  31
#    40-44 38  32
#    45-49 35  26
addmargins(tbl)
#         cancer
# agegroup  no yes Sum
#    35-39  38  31  69
#    40-44  38  32  70
#    45-49  35  26  61
#    Sum   111  89 200
options(digits=3)
prop.table(tbl, 1) * 100
#         cancer
# agegroup   no  yes
#    35-39 55.1 44.9
#    40-44 54.3 45.7
#    45-49 57.4 42.6
prop.table(tbl, 2) * 100
#         cancer
# agegroup   no  yes
#    35-39 34.2 34.8
#    40-44 34.2 36.0
#    45-49 31.5 29.2