在间隔中对年龄进行分组,以在r中创建另一个变量表

时间:2018-09-04 03:39:36

标签: r

this is the contingency table that I'm trying to make

数据:

  Edad  influye
1     31     Nada
2     72     Poco
3     34     Poco
4     47     Poco
5     74     Poco
6     27     Poco
7     43 Moderado
8     58     Poco
9     42     Nada
10    69 Moderado
11    73     Poco
12    48 Moderado
13    38     Poco
14    44     Poco
15    46     Poco

真实数据有247行

使用功能cut,我可以创建间隔,但是当我尝试创建列联表时,我做不到,有些建议

这是间隔

[16,24.5] (24.5,33] (33,41.5] (41.5,50] (50,58.5] (58.5,67] (67,75.5] (75.5,84] 
        1        10        38        59        59        44        27         9 

1 个答案:

答案 0 :(得分:0)

我认为您正在寻找table()

R> set.seed(1)
R> df = data.frame(
  Edad = sample(1:100, 15),
  Influye = c( "Nada", "Poco", "Poco", "Poco", "Poco", "Poco", "Moderado", "Poco", "Nada", "Moderado", "Poco", "Moderado", "Poco", "Poco", "Poco")
)
R> head(table(df))
    Influye
Edad Moderado Nada Poco
  6         1    0    0
  16        1    0    0
  19        0    0    1
  20        0    0    1
  27        0    1    0
  34        0    0    1
R> table(df[1])
 6 16 19 20 27 34 37 57 58 61 62 67 86 89 97
 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
R> table(df[2])
Moderado     Nada     Poco 
       3        2       10

有削减:

R> table(cut(df$Edad, breaks=seq(10,100,10)), df$Influye)
           Moderado Nada Poco
  (10,20]         1    0    2
  (20,30]         0    1    0
  (30,40]         0    0    2
  (40,50]         0    0    0
  (50,60]         0    1    1
  (60,70]         0    0    3
  (70,80]         0    0    0
  (80,90]         0    0    2
  (90,100]        1    0    0

我不确定图片中的多余数字应该代表什么。