汇总总计数

时间:2016-06-02 10:39:52

标签: r

我有以下数据框:

df2 <- structure(list(year = c(1986L, 1987L, 1991L, 1991L, 1991L, 1991L, 
1992L, 1992L, 1992L, 1992L, 1992L, 1992L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 1993L, 
1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 
1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L, 1994L), 
knmilocatie = structure(c(4L, 16L, 10L, 12L, 9L, 20L, 12L, 12L, 25L, 9L, 30L, 
26L, 22L, 18L, 15L, 24L, 13L, 31L, 27L, 5L, 3L, 19L, 21L, 23L, 20L, 26L, 26L, 
31L, 35L, 25L, 11L, 28L, 8L, 29L, 36L, 34L, 7L, 28L, 17L, 14L, 33L, 1L, 11L, 
6L, 32L, 27L, 29L, 20L, 20L, 2L), .Label = c("Achterdiep", "Annen", 
"Appingedam", "Assen", "Bedum", "De Klip", "Delfzijl", "Eenrum", "Eleveld", 
"Emmen", "Garsthuizen", "Geelbroek", "Haren", "Hellum", "Hoogezand", 
"Hooghalen", "Kolham", "Langelo", "Loppersum", "Middelstum", "Nijenklooster", 
"Noordbroek", "Oldenzijl", "Overschild", "Roswinkel", "Slochteren", "Stedum",
 "Steendam", "t-Zandt", "Ten Boer", "Ten Post", "Uithuizermeeden", "Weiwerd", 
"Westerbroek", "Winneweer", "Zandeweer"), class = "factor"), baglocatie = 
structure(c(2L, 12L, 5L, 4L, 2L, 17L, 11L, 2L, 21L, 2L, 16L, 35L, 27L, 14L, 
22L, 19L, 33L, 34L, 26L, 17L, 1L, 18L, 1L, 28L, 6L, 25L, 25L, 29L, 9L, 21L, 
10L, 19L, 34L, 15L, 36L, 13L, 7L, 19L, 8L, 23L, 7L, 31L, 17L, 1L, 20L, 3L, 
10L, 32L, 30L, 24L), .Label = c("Appingedam", "Assen", "Bedum", "Ekehaar", 
"Emmen", "Eppenhuizen", "Farmsum", "Froombosch", "Garrelsweer", 
"Garsthuizen", "Geelbroek", "Hooghalen", "Kolham", "Langelo", "Leermens", 
"Loppersum", "Middelstum", "Oosterwijtwerd", "Overschild", "Roodeschool", 
"Roswinkel", "Sappemeer", "Schildwolde", "Schipborg", "Slochteren", "Stedum",
 "Steendam", "t-Zandt", "Ten Post", "Toornwerd", "Tripscompagnie", "Warffum",
 "Westerbroek", "Wirdum", "Woudbloem", "Zandeweer"), class = "factor"), 
lllocatie = structure(c(3L, 13L, 5L, 10L, 4L, 32L, 10L, 10L, 22L, 4L, 36L, 
37L, 31L, 15L, 23L, 20L, 34L, 8L, 24L, 35L, 19L, 19L, 2L, 29L, 26L, 25L, 25L,
 30L, 8L, 22L, 9L, 20L, 19L, 16L, 38L, 12L, 6L, 27L, 7L, 11L, 17L, 33L, 14L, 
2L, 21L, 18L, 9L, 28L, 32L, 1L), .Label = c("Annen", "Appingedam", "Assen", 
"Eleveld", "Emmen", "Farmsum", "Froombosch", "Garrelsweer", "Garsthuizen", 
"Geelbroek", "Hellum", "Hoogezand", "Hooghalen", "Huizinge", "Langelo", 
"Leermens", "Meedhuizen", "Onderdendam", "Oosterwijtwerd", "Overschild", 
"Roodeschool", "Roswinkel", "Sappemeer", "Sint Annen", "Slochteren", 
"Startenhuizen", "Steendam", "Stitswerd", "t-Zandt", "Ten Post", "Tjuchem", 
"Toornwerd", "Tripscompagnie", "Westerbroek", "Westerwijtwerd", "Winneweer", 
"Woudbloem", "Zandeweer"), class = "factor")), .Names = c("year", 
"knmilocatie", "baglocatie", "lllocatie"), class = "data.frame", row.names = 
c(NA, -50L))

DF2:

   year     knmilocatie     baglocatie      lllocatie
1  1986           Assen          Assen          Assen
2  1987       Hooghalen      Hooghalen      Hooghalen
3  1991           Emmen          Emmen          Emmen
4  1991       Geelbroek        Ekehaar      Geelbroek
5  1991         Eleveld          Assen        Eleveld
6  1991      Middelstum     Middelstum      Toornwerd
7  1992       Geelbroek      Geelbroek      Geelbroek
8  1992       Geelbroek          Assen      Geelbroek
9  1992       Roswinkel      Roswinkel      Roswinkel
10 1992         Eleveld          Assen        Eleveld
11 1992        Ten Boer      Loppersum      Winneweer
12 1992      Slochteren      Woudbloem      Woudbloem
13 1993      Noordbroek       Steendam        Tjuchem
14 1993         Langelo        Langelo        Langelo
15 1993       Hoogezand      Sappemeer      Sappemeer
16 1993      Overschild     Overschild     Overschild
17 1993           Haren    Westerbroek    Westerbroek
18 1993        Ten Post         Wirdum    Garrelsweer
19 1993          Stedum         Stedum     Sint Annen
20 1993           Bedum     Middelstum Westerwijtwerd
21 1993      Appingedam     Appingedam Oosterwijtwerd
22 1993       Loppersum Oosterwijtwerd Oosterwijtwerd
23 1993   Nijenklooster     Appingedam     Appingedam
24 1993       Oldenzijl        t-Zandt        t-Zandt
25 1993      Middelstum    Eppenhuizen  Startenhuizen
26 1993      Slochteren     Slochteren     Slochteren
27 1993      Slochteren     Slochteren     Slochteren
28 1993        Ten Post       Ten Post       Ten Post
29 1994       Winneweer    Garrelsweer    Garrelsweer
30 1994       Roswinkel      Roswinkel      Roswinkel
31 1994     Garsthuizen    Garsthuizen    Garsthuizen
32 1994        Steendam     Overschild     Overschild
33 1994          Eenrum         Wirdum Oosterwijtwerd
34 1994         t-Zandt       Leermens       Leermens
35 1994       Zandeweer      Zandeweer      Zandeweer
36 1994     Westerbroek         Kolham      Hoogezand
37 1994        Delfzijl        Farmsum        Farmsum
38 1994        Steendam     Overschild       Steendam
39 1994          Kolham     Froombosch     Froombosch
40 1994          Hellum    Schildwolde         Hellum
41 1994         Weiwerd        Farmsum     Meedhuizen
42 1994      Achterdiep Tripscompagnie Tripscompagnie
43 1994     Garsthuizen     Middelstum       Huizinge
44 1994         De Klip     Appingedam     Appingedam
45 1994 Uithuizermeeden    Roodeschool    Roodeschool
46 1994          Stedum          Bedum    Onderdendam
47 1994         t-Zandt    Garsthuizen    Garsthuizen
48 1994      Middelstum        Warffum      Stitswerd
49 1994      Middelstum      Toornwerd      Toornwerd
50 1994           Annen      Schipborg          Annen

我想按年总结一下。对于每年我需要的实例数量是baglocatie

1 个答案:

答案 0 :(得分:-2)

您可以使用cbind.data.frame()table()功能。对于每种类型使用colSums()

,请使用rowSums()每年计算一次
 df2 <- as.data.frame(df2)
 table(cbind.data.frame(df2$baglocatie,df2$year))
 colSums(a)
 #   1986 1987 1991 1992 1993 1994 
 #     1     1    4    6   16   22 
 rowSums(a)
  Appingedam          Assen          Bedum        Ekehaar          Emmen    Eppenhuizen        Farmsum     Froombosch 
         3              4              1              1              1              1              2              1 
  Garrelsweer    Garsthuizen      Geelbroek      Hooghalen         Kolham        Langelo       Leermens      Loppersum 
         1              2              1              1              1              1              1              1 
  Middelstum Oosterwijtwerd     Overschild    Roodeschool      Roswinkel      Sappemeer    Schildwolde      Schipborg 
         3              1              3              1              2              1              1              1 
  Slochteren         Stedum       Steendam        t-Zandt       Ten Post      Toornwerd Tripscompagnie        Warffum 
         2              1              1              1              1              1              1              1 
  Westerbroek         Wirdum      Woudbloem      Zandeweer 
         1              2              1              1