這是一個簡單的問題,但我不知道如何使用prop.table爲此,我非常需要此功能。在R中,如何計算數據框中列的百分比統計信息? (表函數擴展百分比)
我有這樣
> library(ggplot2)
> #sample data
> head(tips,3)
total_bill tip sex smoker day time size
1 17 1.0 Female No Sun Dinner 2
2 10 1.7 Male No Sun Dinner 3
3 21 3.5 Male No Sun Dinner 3
> #how often there is a non-smoker
> table(tips$smoker)
No Yes
151 93
> #how many subjects
> nrow(tips)
[1] 244
數據,我需要知道的吸菸者與非吸菸者的比例 像這樣的東西(醜陋的代碼):
> #percentage of smokers
> options(digits=2)
> transform(as.data.frame(table(tips$smoker)),percentage_column=Freq/nrow(tips)*100)
Var1 Freq percentage_column
1 No 151 62
2 Yes 93 38
>
是否有更好的辦法做這個?
(甚至更好它會做到這一點的一組列(我列舉的),並具有輸出有些很好的格式化) (如吸菸,日和時間)
祕訣是GGPLOT2包 – userJT 2012-03-08 22:07:35