计算然后总结特定值?

时间:2014-03-14 14:49:27

标签: r sum counting

我有如下数据(只是我数据的一部分)

month    NumberOfMonths
Jan       4
Jan       3
Feb       2
May       1
Jan       4
Feb       1
May       2
Mar       12
Feb       2
May       1

所以我想创建一个数据框,如下所示

Month  NumberOfMonths
       1  2  3  4  5  6  7  8  9  10  11  12
Jan    0  0  1  2  0  0  0  0  0  0   0   0
Feb    1  2  0  0  0  0  0  0  0  0   0   0
Mar    0  0  0  0  0  0  0  0  0  0   0   1
Apr    0  0  0  0  0  0  0  0  0  0   0   0
May    2  1  0  0  0  0  0  0  0  0   0   0

如上所示,该功能将计算相同的月份数,并将分配给相应的月份。例如,如果我在NumberOfMonths中有两个4,那么在我的数据框中,NumberOfMonths的1月将是2。

顺便说一下,月级是因素而不是日期。

有人可以帮我吗?


我尝试了你所提供的所有功能。但是,我无法得到相同的结果。我粘贴了我的数据输出。如果你不介意我可以再帮忙吗?

structure(list(Month = structure(c(4L, 5L, 5L, 12L, 5L, 5L, 2L, 
12L, 11L, 10L, 7L, 9L, 7L, 4L, 8L, 7L, 5L, 12L, 12L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 8L, 5L, 8L, 1L, 6L, 6L, 2L, 4L, 2L, 10L, 
3L, 5L, 5L, 4L, 1L, 12L, 7L, 7L, 3L, 5L, 6L, 2L, 10L, 1L, 2L, 
2L, 11L, 11L, 12L, 11L, 5L, 12L, 10L, 1L, 9L, 5L, 10L, 5L, 5L, 
9L, 5L, 5L, 5L, 5L, 5L, 1L, 5L, 10L, 4L, 1L, 5L, 5L, 5L, 3L, 
5L, 2L, 9L, 8L, 11L, 10L, 11L, 4L, 8L, 12L, 11L, 7L, 7L, 2L, 
5L, 3L, 8L, 1L, 9L, 9L, 5L, 11L, 10L, 5L, 4L, 4L, 7L, 6L, 2L, 
2L, 5L, 5L, 5L, 5L, 5L, 8L, 8L, 11L, 5L, 11L, 5L, 5L, 1L, 5L, 
5L, 5L, 12L, 5L, 5L, 5L, 4L, 8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OfMonth = c(1, 12.0000000000009, 
1, 1, 12.0000000000009, 12.0000000000009, 1, 1.99999999999909, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3.00000000000091, 
3.00000000000091, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10.9999999999991, 
1, 1, 1, 1, 1.99999999999909, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 
3.00000000000091, 1, 4, 12.0000000000009, 1, 1, 12.0000000000009, 
12.0000000000009, 12.0000000000009, 4, 1, 12.0000000000009, 12.0000000000009, 
1, 12.0000000000009, 1, 7.99999999999909, 4, 12.0000000000009, 
1, 1, 4, 4, 12.0000000000009, 1, 1.99999999999909, 1, 1, 1, 1.99999999999909, 
1, 1, 1, 1, 1, 1, 1, 1.99999999999909, 1, 1, 1, 1, 1, 1, 1, 12.0000000000009, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 4, 7, 12.0000000000009, 1, 1, 
1, 12.0000000000009, 1, 12.0000000000009, 12.0000000000009, 1, 
12.0000000000009, 4.99999999999909, 6.00000000000091, 1, 10.9999999999991, 
1, 1.99999999999909, 1, 2.99999999999818, 1, 1, 1, 1, 4, 1.99999999999909, 
12.0000000000009, 12.0000000000009, 12.0000000000009, 1.99999999999909, 
4, 4, 12.0000000000009, 1, 2.99999999999818, 1, 1, 2.00000000000182, 
4, 1, 7, 12.0000000000009, 1, 1, 6.00000000000091, 1, 7, 4, 3.00000000000091, 
1, 1, 1, 1, 1, 1, 1.99999999999909, 1, 1, 16, 12.0000000000009, 
1, 12.0000000000009, 12.0000000000009, 1, 12.0000000000009, 1, 
1, 12.0000000000009, 1, 12.0000000000009, 7, 3.00000000000091, 
1, 1, 1.99999999999909, 4.99999999999909, 12.0000000000009, 1.99999999999909, 
1.99999999999909, 6.00000000000091, 1, 7, 1, 1, 1.99999999999909, 
1, 1, 4, 3.00000000000091, 1.99999999999909, 1, 1, 3.00000000000091, 
1, 1, 1, 12.0000000000009, 1, 1, 1, 12.0000000000009, 10.9999999999991, 
1, 1, 2.00000000000182, 1, 1, 1, 12.0000000000009, 1, 1, 12.0000000000009, 
12.0000000000009, 24.0000000000009, 1, 1, 12.0000000000009, 1, 
1, 1, 10, 12.0000000000009, 1, 4, 12.0000000000009, 1.99999999999909, 
1, 7.99999999999909, 12.0000000000009, 12.0000000000009, 12.0000000000009, 
12.0000000000009, 12.0000000000009, 12.0000000000009, 6.00000000000091, 
12.0000000000009, 12.0000000000009, 4, 1, 1, 1, 12.0000000000009, 
1, 1, 2.00000000000182, 12.0000000000009, 12.0000000000009, 1, 
6.00000000000091, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.99999999999818, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 2.00000000000182, 12.0000000000009, 
12.0000000000009, 1, 1, 12.0000000000009, 1, 4.99999999999909, 
12.0000000000009, 7.99999999999909, 1, 1, 1, 21.0000000000009, 
7, 9.00000000000091, 2.00000000000182, 4, 4, 3.00000000000182, 
12.0000000000009, 1, 1, 12.0000000000009, 1, 1, 4.99999999999909, 
1, 1, 1.99999999999909, 1, 4.99999999999909, 4.99999999999909, 
4.99999999999909, 4.99999999999909, 4.99999999999909, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 4, 1, 3.00000000000091, 12.0000000000009, 1, 
1, 12.0000000000009, 12.0000000000009, 6.00000000000091, 3.00000000000091, 
4, 1, 12.0000000000009, 7, 3.00000000000091, 1.99999999999909, 
1, 1, 1, 1, 12.0000000000009, 1, 24.0000000000009, 12.0000000000009, 
1, 12.0000000000009, 12.0000000000009, 12.0000000000009, 1, 1, 
1, 1, 10.9999999999991, 1, 1.99999999999909, 1, 10.9999999999991, 
3.00000000000091, 6.00000000000091, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 7, 1, 1, 4, 12.0000000000009, 4, 4, 12.0000000000009, 12.0000000000009, 
12.0000000000009, 12.0000000000009, 6.00000000000091, 1.99999999999909, 
1, 1, 1, 4.99999999999909, 12.0000000000009, 1, 1, 1, 12.0000000000009, 
12.0000000000009, 12.0000000000009, 1, 1, 1, 1, 10.9999999999991, 
12.0000000000009, 1, 1, 1, 1, 1, 7.99999999999909, 4, 2.99999999999818, 
1.99999999999909, 1, 1, 4, 4, 1, 12.0000000000009, 12.0000000000009, 
1.99999999999909, 1, 1, 1, 1, 12.0000000000009, 1.99999999999909, 
4.99999999999909, 4.99999999999909, 3.00000000000091, 4, 1, 1, 
1, 1, 2.00000000000182, 1, 1.99999999999909, 1, 3.00000000000091, 
12.0000000000009, 4, 1.99999999999909, 9.00000000000091, 10, 
1, 1, 1, 1, 12.0000000000009, 4, 2.00000000000182, 3.00000000000091, 
4, 1, 1, 1, 1, 1, 4.99999999999909, 1.99999999999909, 1.99999999999909, 
7, 1, 4, 1, 7, 7.99999999999909, 12.0000000000009, 1, 10, 1, 
12.0000000000009, 7.99999999999909, 1, 1, 12.0000000000009, 4, 
7.99999999999909, 7, 2.99999999999818, 1, 3.00000000000091, 4.99999999999909, 
1, 1, 4, 4.99999999999909, 1, 6.00000000000091, 5.99999999999818, 
1, 9.00000000000091, 1, 7.99999999999909, 1, 1, 1, 4.99999999999909, 
1, 1, 1, 1, 1, 4.99999999999909, 1, 1, 1, 1, 1, 10.9999999999991, 
1, 1, 1, 1, 2.00000000000182, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 4.99999999999909, 1, 1, 1, 1, 9.00000000000091, 1.99999999999909, 
1, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 1, 1, 1.99999999999909, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7.99999999999909, 1, 1.99999999999909, 
1, 1, 1, 1, 1, 1, 1, 1, 12.0000000000009, 12.0000000000009, 1, 
12.0000000000009, 1, 1.99999999999909, 1, 1, 12.0000000000009, 
10, 12.0000000000009, 1, 1, 1, 1, 10, 12.0000000000009, 1, 1, 
1, 12.0000000000009, 12.0000000000009, 12.0000000000009, 1, 1, 
1, 1, 12.0000000000009, 12.0000000000009, 12.0000000000009, 1, 
1)), .Names = c("Month", "NumberOfMonth"), row.names = c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 
81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 
94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 
117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 
139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 
150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 
161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 
172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 
183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L, 
194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L, 
205L, 206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L, 
216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 224L, 225L, 226L, 
227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L, 
238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L, 248L, 
249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L, 259L, 
260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L, 270L, 
271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L, 281L, 
282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L, 292L, 
293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L, 303L, 
304L, 305L, 306L, 307L, 308L, 309L, 310L, 311L, 312L, 313L, 314L, 
315L, 316L, 317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L, 
326L, 327L, 328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 
337L, 338L, 339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L, 
348L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L, 
360L, 361L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 369L, 370L, 
371L, 372L, 373L, 374L, 375L, 376L, 377L, 378L, 379L, 380L, 381L, 
382L, 383L, 384L, 385L, 386L, 387L, 388L, 389L, 390L, 391L, 392L, 
393L, 394L, 395L, 396L, 397L, 398L, 399L, 400L, 401L, 402L, 403L, 
404L, 405L, 406L, 407L, 408L, 409L, 410L, 411L, 412L, 413L, 414L, 
415L, 416L, 417L, 418L, 419L, 420L, 421L, 422L, 423L, 424L, 425L, 
426L, 427L, 428L, 429L, 430L, 431L, 432L, 433L, 434L, 435L, 436L, 
437L, 438L, 439L, 440L, 441L, 444L, 445L, 446L, 447L, 448L, 449L, 
450L, 451L, 452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L, 460L, 
461L, 462L, 463L, 464L, 465L, 466L, 467L, 468L, 469L, 470L, 471L, 
472L, 473L, 474L, 475L, 476L, 477L, 478L, 479L, 480L, 481L, 482L, 
483L, 484L, 485L, 486L, 487L, 488L, 489L, 490L, 491L, 492L, 493L, 
494L, 495L, 496L, 497L, 498L, 499L, 500L, 501L, 502L, 503L, 504L, 
505L, 506L, 507L, 508L, 509L, 510L, 511L, 512L, 513L, 514L, 515L, 
516L, 517L, 518L, 519L, 520L, 521L, 522L, 523L, 524L, 525L, 526L, 
527L, 528L, 529L, 530L, 531L, 532L, 533L, 534L, 535L, 536L, 537L, 
538L, 539L, 540L, 541L, 542L, 543L, 544L, 545L, 546L, 547L, 548L, 
549L, 550L, 551L, 552L, 553L, 554L, 555L, 556L, 557L, 558L, 559L, 
560L, 561L, 562L, 563L, 564L, 565L, 566L, 567L, 568L, 569L, 570L, 
571L, 572L, 573L, 574L, 575L, 576L, 577L, 578L, 579L, 580L, 581L, 
582L, 583L, 584L, 585L, 586L, 587L, 588L, 589L, 590L, 591L, 592L, 
593L, 594L, 595L, 596L, 597L, 598L, 599L, 600L, 601L, 602L, 603L, 
604L, 605L, 606L, 607L, 608L, 609L, 610L, 611L, 612L, 613L, 614L, 
615L, 616L, 617L, 618L, 619L), class = "data.frame", na.action = structure(c(349L, 
442L, 443L), .Names = c("349", "442", "443"), class = "omit"))

3 个答案:

答案 0 :(得分:1)

读入数据

dd = read.table(textConnection("month    NumberOfMonths
Jan       4
Jan       3
Feb       2
May       1
Jan       4
Feb       1
May       2
Mar       12
Feb       2
May       1"), header=TRUE)

设置具有正确级别的因子

dd$month = factor(dd$month, levels=month.abb)
## I've made No. of months a factor to influence the table output
dd$NumberOfMonths = factor(dd$NumberOfMonths, levels=1:12)

现在制表

table(dd$month, dd$NumberOfMonths)

## Drop unused months
table(droplevels(dd$month), droplevels(dd$NumberOfMonths))

答案 1 :(得分:0)

你也有xtabs:

tab$NumberOfMonths <- factor(tab$NumberOfMonths, levels=1:12)
tab$month <- factor(tab$month, levels=c("Jan", "Feb", "Mar", "Apr", "May")) 
xtabs(~month+NumberOfMonths, data=tab)

答案 2 :(得分:0)

我也喜欢在dcast包中使用reshape2函数。

dat <- ...
dcast(dat, month ~ NumberOfMonths)

#  month 1 2 3 4 12
#1   Feb 1 2 0 0  0
#2   Jan 0 0 1 2  0
#3   Mar 0 0 0 0  1
#4   May 2 1 0 0  0