创建一个包含计数值的表

时间:2015-02-16 07:37:40

标签: r

我有一个大型的不同位置的标本观察数据集 标本具有单独的名称(标本)和位置(位置)。 目前,每一行都是不同的目击。

我想要一张包含所有样本的表格,并且知道它们在哪个位置被发现(每个位置都有一个辛辛那里的数量)。每行都是一个标本,列就是位置。

可以管理吗?

以下是我的数据集示例:

   Stops duration_m Specimen     Month      Location
     1        483   Adonia       May    Copenhagen
     2        385   Adonia       May      Saaremaa
     3        494   Adonia       May       Tallinn
     4       1955   Adonia       May St Petersburg
     5        488   Adonia       May     Stockholm
    55        480   Adonia      June      Sassnitz
    64       1330   Adonia      June          Kiel
    65        492   Adonia      June      Saaremaa
     1        482   Adonia      July       Tallinn
     2       2166   Adonia      July St Petersburg
     2        505   Adonia    August    Copenhagen
     3        486   Adonia    August      Saaremaa
     5        464   Adonia    August       Tallinn
     6       2160   Adonia    August St Petersburg
     7        491   Adonia    August     Stockholm
     8       1120   Adonia    August     Stockholm
    15        654   Adonia    August      Sassnitz
    21        361   Adonia September          Kiel
     3        495   Adonia September      Saaremaa
     4        513   Adonia September       Tallinn
     5       2001   Adonia September St Petersburg
     6        507   Adonia September     Stockholm
     8       1470   Adonia September    Copenhagen
    73        433  killuia      June    Copenhagen
    74        408  killuia      June       Tallinn
    75       2114  killuia      June St Petersburg
    76        550  killuia      June      Helsinki
    77        384      abc      June       Tallinn
    78       1116      abc      June St Petersburg

1 个答案:

答案 0 :(得分:2)

您可以使用table函数:

table(df$Specimen,df$Location)

          Copenhagen Helsinki Kiel Saaremaa Sassnitz St Petersburg Stockholm Tallinn
  abc              0        0    0        0        0             1         0       1
  Adonia           3        0    2        4        2             4         4       4
  killuia          1        1    0        0        0             1         0       1

数据

 df<-structure(list(Stops = c(1L, 2L, 3L, 4L, 5L, 55L, 64L, 65L, 1L, 
    2L, 2L, 3L, 5L, 6L, 7L, 8L, 15L, 21L, 3L, 4L, 5L, 6L, 8L, 73L, 
    74L, 75L, 76L, 77L, 78L), duration_m = c(483L, 385L, 494L, 1955L, 
    488L, 480L, 1330L, 492L, 482L, 2166L, 505L, 486L, 464L, 2160L, 
    491L, 1120L, 654L, 361L, 495L, 513L, 2001L, 507L, 1470L, 433L, 
    408L, 2114L, 550L, 384L, 1116L), Specimen = c("Adonia", "Adonia", 
    "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", 
    "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", 
    "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", "Adonia", 
    "killuia", "killuia", "killuia", "killuia", "abc", "abc"), Month = c("May", 
    "May", "May", "May", "May", "June", "June", "June", "July", "July", 
    "August", "August", "August", "August", "August", "August", "August", 
    "September", "September", "September", "September", "September", 
    "September", "June", "June", "June", "June", "June", "June"), 
        Location = c("Copenhagen", "Saaremaa", "Tallinn", "St Petersburg", 
        "Stockholm", "Sassnitz", "Kiel", "Saaremaa", "Tallinn", "St Petersburg", 
        "Copenhagen", "Saaremaa", "Tallinn", "St Petersburg", "Stockholm", 
        "Stockholm", "Sassnitz", "Kiel", "Saaremaa", "Tallinn", "St Petersburg", 
        "Stockholm", "Copenhagen", "Copenhagen", "Tallinn", "St Petersburg", 
        "Helsinki", "Tallinn", "St Petersburg")), .Names = c("Stops", 
    "duration_m", "Specimen", "Month", "Location"), class = "data.frame", row.names = c(NA, 
    -29L))