菜鳥 - 對不起。我一直在申請prop.table()資料,我有一個非常基本的問題。我想從分類資料的資料框中自動輸出合并的偶爾和常規吸煙者(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
uj5u.com熱心網友回復:
您可以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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/375154.html
上一篇:如何以編程方式洗掉字串后的空格?
