0

I have a simple data.frame in R with two columns, organisms and frequency. I need to combine rows with similar organisms and add the frequency of these rows together at the same time. This should be easy enough to do with a for loop but I'm new with R so any help is greatly appreciated.

> dput(dat)
structure(list(organism = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 8L, 9L, 9L, 9L, 9L, 10L, 11L, 11L, 11L, 12L, 13L, 
13L, 14L, 14L, 14L, 14L, 15L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 
17L, 18L, 18L, 18L, 19L, 19L, 20L, 21L, 21L, 22L, 22L, 23L, 23L, 
23L, 23L, 24L, 24L, 25L, 25L, 25L, 26L, 27L, 28L, 29L, 29L, 30L, 
31L, 32L, 33L, 33L, 34L, 35L, 35L, 36L, 36L, 37L, 38L, 38L, 39L, 
40L, 40L, 40L, 41L, 41L, 42L, 43L, 44L, 44L, 44L, 45L, 45L, 46L, 
46L, 47L, 47L, 48L, 48L, 48L, 49L, 50L, 50L, 51L, 51L, 52L, 53L, 
55L, 57L, 60L, 62L, 63L, 64L, 65L, 67L, 70L, 71L, 72L, 73L, 74L, 
77L, 78L, 79L, 80L, 82L, 84L, 85L, 86L, 94L, 96L, 97L, 99L, 100L, 
101L, 118L, 119L, 120L, 121L, 124L, 125L, 128L, 131L, 132L), .Label = c("(1,3)-Beta-D-Glucan (Fungitell)", 
"HPIV-1", "Rhino", "Adeno C", "Pneumst", "Aspergillus Galactomannan", 
"Mycobacterium avium-intracellulare complex", "Aspergillus niger Group", 
"yeast", "Aspergillus versicolor", "Penicillium", "Candida glabrata Candida dubliniensis", 
"Pseudomonas aeruginosa", "CMV", "Enterovirus,", "HMPV", "RSV", 
"InfA", "Yeast", "Achromobacter ", "Klebsiella pneumoniae", "Aspergillus fumigatus", 
"Aspergillus fumigatus Group", "CWP-KOH", "Pnuemocystis jirovecii", 
"Mycobacterium gordonae", "Human Herpesvirus 6(?)", "Nocardia farcinica", 
"Parainfluenza Virus Type 1", "Pflu,1", "Pflu,2", "Pflu,3", "Aspergillus", 
"Cryptococcus", "Legionella pneumophila", "Mycobacterium avium-intracellulare complex ", 
"Trametes versicolor / Tricholoma robustum Basidiomyceteous moulds", 
"Pnuemocystis jirovecii ", "Acinetobacter baumannii", "Pseudomonas aeruginosa ", 
"Adeno", "Adeno C. Pnuemocystis", "AdenoB/E", "RSV-A", "RSV-B", 
"RSVA", "RSVB", "InfB", " Pantoea species", "Staphylococcus aureus", 
"Streptococcus pneumoniae", " Herpes Simplex Virus", " Legionella pneumophila", 
"(1,3)-Beta-D-Glucan (Fungitell), Rhino, HPIV-1", "Adeno B/E", 
"Adeno C, Pneumst", "Aspergillus Gal", "Aspergillus Galactomannan, Mycobacterium avium-intracellulare complex", 
"Aspergillus niger Group, yeast", "Aspergillus versicolor Group", 
"Aspergillus versicolor, Penicillium", "BPert", "Candida albicans", 
"Candida albicans/dubliniensis", "Candida albicans/dubliniensis ", 
"Candida glabrata Candida dubliniensis, Pseudomonas aeruginosa", 
"Candida lipolytica", "CMV, Pneumst", "CMV, Rhino, Pneumst", 
"Coccidioides immitis", "Filamentous fungus", "FluA", "Haemophilus influenzae", 
"Herpes Simplex Virus", "HMPV, Rhino", "HMPV, RSV", "HPIV-2", 
"HPIV1", "Inf A&B", "infA", "InfA, Yeast", "Klebsiella pneumoniae 4+ Staphylococcus aureus 3+ Enterobacter cloacae", 
"Klebsiella pneumoniae, Achromobacter ", "Legionella maceachernii", 
"Legionella micdadei", "Mucoraceae", "Mycobacterium avium-intracellulare complex, Aspergillus fumigatus", 
"Mycobacterium avium-intracellulare complex, Aspergillus fumigatus Group", 
"Mycobacterium avium-intracellulare complex, CWP-KOH", "Mycobacterium avium-intracellulare complex, Penicillium", 
"Mycobacterium avium-intracellulare complex, yeast", "Mycobacterium avium-intracellulare complex, yeast, Aspergillus fumigatus Group, Pnuemocystis jirovecii", 
"Mycobacterium gordonae, Aspergillus fumigatus Group", "Mycoplasma pneumoniae", 
"Nocardia farcinica, Aspergillus fumigatus, Human Herpesvirus 6(?)", 
"Nocardia wallacei ", "Parainfluenza 1", "Parainfluenza Virus Type 1, Rhino", 
"Parainfluenza Virus Type 3", "Pflu 1", "Pflu 2", "Pneumst, Aspergillus", 
"Pneumst, Cryptococcus", "Pneumst, Legionella pneumophila, Mycobacterium avium-intracellulare complex ", 
"Pneumst, Trametes versicolor / Tricholoma robustum Basidiomyceteous moulds", 
"Pnuemocystis jirovecii , Aspergillus Galactomannan", "Pnuemocystis jirovecii, CMV, Aspergillus", 
"Pseudomonas aeruginosa , Acinetobacter baumannii", "Rhino, Adeno", 
"Rhino, Adeno C", "Rhino, Adeno C. Pnuemocystis", "Rhino, AdenoB/E", 
"Rhino, HMPV", "Rhino, RSV-A", "Rhino, RSV-B", "Rhino, RSVA", 
"Rhino, RSVB", "RS V", "RSV A", "RSV B", "RSV-A Rhino", "RSV, HPIV-1", 
"RSVA, HMPV, HPIV-1, InfB", "saprophytic Neisseria species", 
"Scedosporium apiospermum/ Pseudallescheria boydii complex", 
"Staphylococcus aureus,  Pantoea species", "Staphylococcus aureus, Pseudomonas aeruginosa ", 
"Streptococcus mitis", "Streptococcus pneumoniae, InfA", "Streptococcus pneumoniae, InfB, RSV-A", 
"Trichoderma species", "Varicella Zoster Virus"), class = "factor"), 
    freq = c(6L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 2L, 1L, 1L, 1L, 1L, 1L, 40L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 29L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 
    7L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 3L, 1L, 1L, 
    1L, 1L, 2L, 1L, 1L, 1L, 1L, 21L, 1L, 1L, 41L, 1L, 1L, 22L, 
    1L, 3L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 7L, 1L, 1L, 
    7L, 1L, 1L, 1L, 1L, 5L, 10L, 2L, 1L, 1L, 1L, 1L, 1L, 8L, 
    1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 9L, 1L, 2L, 1L, 1L, 
    1L, 1L, 2L, 1L, 1L, 1L, 4L, 1L, 1L, 17L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 37L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 11L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 11L, 1L, 2L, 1L, 
    4L, 1L, 6L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("organism", 
"freq"), row.names = c("1", "2", "4", "88", "91", "341", "3", 
"18", "22", "49", "69", "71", "73", "75", "77", "79", "81", "83", 
"85", "831", "5", "72", "710", "6", "16", "19", "53", "55", "57", 
"60", "711", "7", "63", "8", "29", "31", "33", "35", "37", "39", 
"491", "9", "10", "38", "40", "112", "11", "12", "36", "651", 
"13", "14", "801", "15", "17", "65", "211", "20", "21", "23", 
"78", "90", "311", "24", "87", "941", "25", "98", "391", "26", 
"113", "28", "27", "421", "30", "46", "32", "41", "44", "910", 
"34", "251", "42", "64", "761", "43", "47", "45", "48", "621", 
"50", "51", "52", "54", "66", "56", "58", "471", "59", "501", 
"61", "62", "771", "68", "67", "96", "811", "70", "510", "74", 
"76", "80", "101", "971", "82", "991", "84", "89", "86", "102", 
"92", "100", "411", "94", "93", "95", "97", "99", "114", "210", 
"610", "105", "131", "151", "161", "171", "181", "201", "241", 
"271", "281", "291", "301", "351", "361", "371", "381", "431", 
"451", "461", "481", "581", "601", "611", "641", "661", "671", 
"931", "951", "961", "981", "103", "104", "107", "110", "111"
), class = "data.frame")
Khashaa
  • 7,293
  • 2
  • 21
  • 37
webDevleoper101
  • 69
  • 3
  • 14

0 Answers0