在 prop.table() 中将分类输出相加
Adding categorical outputs together in prop.table()
新手 - 抱歉。我已经将 prop.table()
应用于数据,我有一个非常基本的问题。我想从分类数据的数据框中自动输出合并的偶尔 AND 经常吸烟者 (0.04 + 0.34 = 0.38),我该怎么做?
我真的不介意让我了解这里发生了什么的循序渐进的过程。我可以把它应用到其他地方 - 谢谢
编辑:我能想到的唯一过程是尝试移除 not
吸烟者并计算剩余的分类变量,并使用该计数通过基础数学得出比例。但我认为我与这种方法相去甚远
编辑 - 添加 prop.table 代码
OccSmoker <- table(Student1995$smoke)
prop.table(OccSmoker)
$smoke
Not Occasional Regular
0.62 0.04 0.34
数据帧在这里
> dput(head(Student1995,5))
structure(list(alcohol = structure(c(3L, 2L, 2L, 2L, 3L), .Label = c("Not",
"Once or Twice a week", "Once a month", "Once a week", "More than once a week"
), class = "factor"), drugs = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("Not",
"Tried once", "Occasional", "Regular"), class = "factor"), smoke = structure(c(2L,
3L, 1L, 1L, 1L), .Label = c("Not", "Occasional", "Regular"), class = "factor"),
sport = structure(c(2L, 1L, 1L, 2L, 2L), .Label = c("Not regular",
"Regular"), class = "factor")), row.names = c(NA, 5L), class = "data.frame")
汇总数据(如果有帮助)- 编辑
> summary(Student1995)
alcohol drugs smoke sport
Not : 5 Not :36 Not :38 Not regular:13
Once or Twice a week :16 Tried once: 6 Occasional: 5 Regular :37
Once a month :12 Occasional: 7 Regular : 7
Once a week :14 Regular : 1
More than once a week: 3
您可以像这样定义一个新变量 smoke_class
,然后将其插入 prop.table()
Student1995$smoke_class <- ifelse(
Student1995$smoke %in% c("Occasional", "Regular"), # condition
"Occasional + Regular", # value if condition == TRUE
Student1995$smoke) # value if condition == FALSE
OccSmoker <- table(Student1995$smoke_class)
prop.table(OccSmoker)
# Not Occasional + Regular
# 0.6 0.4
新手 - 抱歉。我已经将 prop.table()
应用于数据,我有一个非常基本的问题。我想从分类数据的数据框中自动输出合并的偶尔 AND 经常吸烟者 (0.04 + 0.34 = 0.38),我该怎么做?
我真的不介意让我了解这里发生了什么的循序渐进的过程。我可以把它应用到其他地方 - 谢谢
编辑:我能想到的唯一过程是尝试移除 not
吸烟者并计算剩余的分类变量,并使用该计数通过基础数学得出比例。但我认为我与这种方法相去甚远
编辑 - 添加 prop.table 代码
OccSmoker <- table(Student1995$smoke)
prop.table(OccSmoker)
$smoke
Not Occasional Regular
0.62 0.04 0.34
数据帧在这里
> dput(head(Student1995,5))
structure(list(alcohol = structure(c(3L, 2L, 2L, 2L, 3L), .Label = c("Not",
"Once or Twice a week", "Once a month", "Once a week", "More than once a week"
), class = "factor"), drugs = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("Not",
"Tried once", "Occasional", "Regular"), class = "factor"), smoke = structure(c(2L,
3L, 1L, 1L, 1L), .Label = c("Not", "Occasional", "Regular"), class = "factor"),
sport = structure(c(2L, 1L, 1L, 2L, 2L), .Label = c("Not regular",
"Regular"), class = "factor")), row.names = c(NA, 5L), class = "data.frame")
汇总数据(如果有帮助)- 编辑
> summary(Student1995)
alcohol drugs smoke sport
Not : 5 Not :36 Not :38 Not regular:13
Once or Twice a week :16 Tried once: 6 Occasional: 5 Regular :37
Once a month :12 Occasional: 7 Regular : 7
Once a week :14 Regular : 1
More than once a week: 3
您可以像这样定义一个新变量 smoke_class
,然后将其插入 prop.table()
Student1995$smoke_class <- ifelse(
Student1995$smoke %in% c("Occasional", "Regular"), # condition
"Occasional + Regular", # value if condition == TRUE
Student1995$smoke) # value if condition == FALSE
OccSmoker <- table(Student1995$smoke_class)
prop.table(OccSmoker)
# Not Occasional + Regular
# 0.6 0.4