我有一个包含Age_group(10到20,20到30,30到40,40到50)60,Tenure(1:5)和Response(True或False)列的数据框如何创建表这让我对每个团体和任期都是真实的。
例如
1 2 3 4 5
10-20
20-30 Count of True's
30-40
40-50
答案 0 :(得分:0)
require(reshape2)
df <- data.frame(age = sample(seq(10,50,5),100, replace = T),
tenure = sample(1:5, 100, replace = T),
response = sample(0:1, 100, replace = T))
head(df)
age tenure response
1 10 5 0
2 50 4 0
3 30 5 0
4 10 4 0
5 25 5 0
6 35 1 0
dcast(df, age ~ tenure,sum, value.var = 'response')
age 1 2 3 4 5
1 10 0 0 2 1 1
2 15 1 2 3 0 0
3 20 3 0 0 0 0
4 25 0 1 1 3 0
5 30 2 2 0 2 2
6 35 0 1 2 0 5
7 40 2 0 2 0 0
8 45 0 0 3 1 0
9 50 1 0 0 2 0