df <- data.frame(value=abs(rnorm(100, 25, 5)), status=sample(0:1,100,replace=T))
df$value[sample(1:100,5)] <- NA
我需要獲得一個頻率(百分比)表(更好的回報矩陣)像下面這樣:
br <- seq(0, 50, 25)
with(df, summary(cut(value[status==0], br, labels=br[-1],
include.lowest=T, ordered_result=T)))
with(df, summary(cut(value[status==1], br, labels=br[-1],
include.lowest=T, ordered_result=T)))
:
value | status(0) status(1)
----------------------------
<=25 | 23 (23%) 20 (20%)
>25 | 27 (27%) 25 (25%)
NA | 3 (3%) 2 (2%)
我可以利用這樣做
但是會有一種一次性返回上述矩陣的方法嗎?謝謝!
而且,如果您不想指定左和右截斷點,請設置'breaks = c(-Inf,25,Inf)'。 –
好的提示。我總是忘記那個,我自己。 –
謝謝!應該已經知道table()更好。 :) – Rock