looping through a data.frame in R to create variables based on logic
<p>I have a data.frame in R with ten columns but only three columns of interest. I need to create two new columns based on the data from the 'ID', 'test' and the 'result' columns. The logic is fairly simple...</p>

<ul>
<li>First there are going to be two new columns created, Positive and Negative.</li>
<li>If the result column for a specific ID has the value of Positive than the test field should appear in the new Positive column that was just created and the opposite for Negative </li>
<li>If an ID is found in multiple rows (ie repeating ID's) an array should be created of the 'test' value for each of those rows. </li>
<li>Than that array should be put in the new Positive or Negative row depending on the 'result' column for that ID. </li>
</ul>

<p>this is what the data looks like ..<a href="https://i.stack.imgur.com/x5RV9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x5RV9.png" alt="enter image description here"></a></p>

<pre><code>dput(data)
structure(list(id = c(145, 146, 147, 148, 149, 150, 151, 151, 
152, 153, 153, 154, 154, 155, 156, 156, 157, 158, 159, 160), 
    age = structure(c(59L, 11L, 23L, 60L, 4L, 52L, 21L, 21L, 
    44L, 1L, 1L, 3L, 3L, 1L, 1L, 1L, 50L, 72L, 48L, 1L), .Label = c("0", 
    "1", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
    "19", "2", "20", "21", "22", "23", "24", "25", "262", "27", 
    "28", "29", "3", "31", "34", "35", "36", "37", "38", "39", 
    "4", "40", "41", "45", "48", "49", "5", "50", "51", "53", 
    "54", "55", "56", "57", "58", "59", "6", "60", "62", "63", 
    "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", 
    "75", "76", "77", "78", "79", "8", "80", "81", "84", "9", 
    "Unknown"), class = "factor"), sex = structure(c(2L, 2L, 
    2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    2L, 1L, 1L), .Label = c("F", "M", "U", "Unknown"), class = "factor"), 
    comments = structure(c(NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
    NA_integer_, NA_integer_), .Label = c("&gt;10,000 cfu/mL Beta Hemolytic Streptococci, Group B \nSusceptibility testing of penicillin and other beta-lactams \nis not necessary for beta-hemolytic streptococci since \nresistant strains have not been recognized. \n(CLSI M100). Susceptibility testing is \navailable upon request for penicillin-allergic patients. \n1,000 cfu/mL Gram Positive Cocci \nIdentification to follow.", 
    "1+ Gram Variable Rods \nNo polymorphonuclear leukocytes (PMNs) seen", 
    "2+ Achromobacter species ", "2+ Mixed gram positive flora \nResembles normal flora", 
    "2+ Mixed gram positive flora \nResembles skin flora", "2+ Staphylococcus aureus \nSusceptibility to follow.", 
    "3+ Coagulase negative Staphylococcus species 2 colony types", 
    "3+ Enteric Gram Negative Rods", "3+ Enterobacter cloacae complex \nIdentification by MALDI-TOF \nTest developed and characteristics determined by \nARUP Laboratories. See Compliance Statement B: \naruplab.com/CS \nNo other flora isolated", 
    "3+ Gram Positive Cocci \n1+ Gram Negative Rods \n2+ polymorphonuclear leukocytes (PMNs)", 
    "3+ Gram Positive Cocci \nNo polymorphonuclear leukocytes (PMNs) seen", 
    "3+ Isolates consistent with microorganisms normally \nencountered in the upper respiratory tract. \n3+ Streptococcus pneumoniae \n3+ Haemophilus species \nPlease contact ARUP within 24 hours if \nsusceptibilities are required. \n(801) 583-2787 Ext. 2169", 
    "3+ Pseudomonas aeruginosa 3+Gram Negative Rods 3+Enteric Gram Negative Rods", 
    "Culture negative for Fungi", "Culture overgrown with contaminants/flora. Unable to complete standard incubation time. Unable to rule out presence of indicated pathogen(s)", 
    "Culture POSITIVE for Beta Hemolytic Streptococci, Group A \nSusceptibility testing of penicillin and other beta-lactams \nis not necessary for beta-hemolytic streptococci since \nresistant strains have not been recognized. \n(CLSI M100). Susceptibility testing is \navailable upon request for penicillin-allergic patients. \nSome isolates may be resistant to erythromycin and/or \nclindamycin.", 
    "Culture POSITIVE for Beta Hemolytic Streptococci, Group C \nBeta hemolytic streptococci other than Group A have \nbeen implicated as pathogens in pharyngitis. \nSusceptibility testing of penicillin and other beta-lactams \nis not necessary for beta-hemolytic streptococci since \nresistant strains have not been recognized. \n(CLSI M100). Susceptibility testing is \navailable upon request for penicillin-allergic patients. \nSome isolates may be resistant to erythromycin and/or \nclindamycin.", 
    "Culture POSITIVE for Cytomegalovirus (CMV) \nby early antigen test", 
    "Culture POSITIVE for Legionella bozemanii \nIdentification by MALDI-TOF \nTest developed and characteristics determined by \nARUP Laboratories. See Compliance Statement B: \naruplab.com/CS", 
    "Culture POSITIVE for Staphylococcus aureus \nPresumptive methicillin susceptible by screening agar", 
    "Specimen received and in progress."), class = "factor"), 
    test = structure(c(60L, 190L, 15L, 190L, 35L, 190L, 35L, 
    36L, 15L, 35L, 36L, 35L, 36L, 3L, 35L, 36L, 18L, 10L, 35L, 
    35L), .Label = c("test.", "source", "Adenovirus", "Coronavirus", 
    "Human.bocavirus", "X...HBoV.1", "Enterovirus", "X...Coxsackievirus.A..with.types.", 
    "X...Coxsackievirus.B..with.types.", "Human.Rhinovirus", 
    "X...Human.Rhinovirus.A..with.types.", "X...Human.Rhinovirus.B..with.types.", 
    "X...Human.Rhinovirus.C..with.types.", "Cytomegalovirus..CMV.", 
    "Herpes.Simplex.Virus", "Varicella.Zoster.Virus", "Human.metapneumovirus", 
    "Influenza.Virus.Type.A", "X...Influenza.Virus.Type.A..H1", 
    "X...Influenza.Virus.Type.A..2009.H1N1", "X...Influenza.Virus.Type.A..H3", 
    "Influenza.Virus.Type.B", "Influenza.Virus.Type.C", "Parainfluenza.Virus.Type.1", 
    "Parainfluenza.Virus.Type.2", "Parainfluenza.Virus.Type.3", 
    "Parainfluenza.Virus.Type.4", "Parechovirus", "Respiratory.Syncytial.Virus..RSV.", 
    "X..RSVA", "X..RSVB", "Hantavirus", "Measles.virus", "X...Measeles.virus.typing..including.wild.type.vs..vaccine.strain.", 
    "Bordetella.pertussis", "Bordetella.parapertussis", "Bordetella.bronchiseptica", 
    "Bordetella.holmsii", "Bordetella.petrii", "Bordetella.avium", 
    "Pseudomonas.aeruginosa", "Pseudomonas.fluorescens.group", 
    "Pseudomonas.putida.group", "Pseudomonas.putida", "Pseudomonas.stutzeri", 
    "Pseudomonas.fulva", "Pseudomonas.luteola", "Mycoplasma.pneumoniae", 
    "Mycoplasma.hominis", "Haemophilus.influenzae", "Haemophilus.parainfluenzae", 
    "Stenotrophomonas.maltophilia", "Burkholderia.cepacia.complex", 
    "Burkholderia.cenocepacia", "Burkholderia.multivorans", "Burkholderia.vietnamiensis", 
    "Burkholderia.dolosa", "Burkholderia.cepacia", "Burkholderia.gladioli", 
    "Legionella.pneumophila", "Legionella.feeleii", "Legionella.micdadei", 
    "Legionella.bozemanii", "Legionella.dumoffii", "Legionella.longbeachae", 
    "Staphylococcus.aureus", "Streptococcus.pneumoniae", "Streptococcus.pyogenes", 
    "Streptococcus.agalactiae", "Streptococcus.dysgalactiae.subsp..Equisimilis", 
    "Streptococcus.anginosus", "Streptococcus.constellatus", 
    "X...Streptococcus.constellatus.subsp..Constellatus", "X...Streptococcus.constellatus.subsp..Pharyngis", 
    "Streptococcus.intermedius", "Chlamydophila.pneumoniae", 
    "Chlamydia.trachomatis", "Ureaplasma.urealyticum", "Moraxella.catarrhalis", 
    "Citrobacter.freundii", "Citrobacter.koseri", "Morganella.morganii", 
    "Proteus.mirabilis", "Klebsiella.pneumoniae", "Klebsiella.oxytoca", 
    "Raoultella.planticola", "Raoultella.ornithinolytica", "Escherichia.coli", 
    "Enterobacter.aerogenes", "Enterobacter.cloacae", "Serratia.marcescens", 
    "Acinetobacter.baumanii", "Neisseria.meningitidis", "Eikenella.corrodens", 
    "Achromobacter.xylosoxidans", "Nocardia.cyriacigeorgica", 
    "Nocardia.nova.complex", "X...Nocardia.africana", "X...Nocardia.elegans", 
    "X...Nocardia.kruczakiae", "X...Nocardia.mikamii", "X...Nocardia.nova", 
    "X...Nocardia.veterana", "Nocardia.farcinica", "Nocardia.brasiliensis", 
    "Nocardia.abscessus.complex", "X...Nocardia.abscessus", "X...Nocardia.arthritidis", 
    "X...Nocardia.asiatica", "Nocardia.transvalensis.complex", 
    "X...Nocardia.transvalensis", "X...Nocardia.wallacei", "Nocardia.beijingensis.complex", 
    "X...Nocardia.beijingensis", "X...Nocardia.pneumoniae", "Nocardia.otitidiscaviarum", 
    "Nocardia.paucivorans", "Actinomyces.pyogenes", "Actinomyces.cardiffensis", 
    "Actinomyces.funkei", "Actinomyces.gerencseriae", "Actinomyces.graevenitzii", 
    "Actinomyces.israelii", "Actinomyces.meyeri", "Actinomyces.naeslundii", 
    "Actinomyces.odontolyticus", "Actinomyces.turicensis", "Actinomyces.viscosus", 
    "Mycobacterium.tuberculosis", "Mycobacterium.arupense..SGM.", 
    "Mycobacterium.avium..SGM.", "Mycobacterium.asiaticum..SGM.", 
    "Mycobacterium.branderi..SGM.", "Mycobacterium.chimaera..SGM.", 
    "Mycobacterium.celatum..SGM.", "Mycobacterium.florentinum..SGM.", 
    "Mycobacterium.heckeshornense..SGM.", "Mycobacterium.intermedium..SGM.", 
    "Mycobacterium.interjectum..SGM.", "Mycobacterium.intracellulare..SGM.", 
    "Mycobacterium.iranicum..SGM.", "Mycobacterium.kansasii..SGM.", 
    "Mycobacterium.kubicae..SGM.", "Mycobacterium.lentiflavum..SGM.", 
    "Mycobacterium.malmoense..SGM.", "Mycobacterium.nebraskense..SGM.", 
    "Mycobacterium.nonchromogenicum..SGM.", "Mycobacterium.palustre..SGM.", 
    "Mycobacterium.parascrofulaceum..SGM.", "Mycobacterium.phlei..SGM.", 
    "Mycobacterium.riyadhense..SGM.", "Mycobacterium.saskatchewanse..SGM.", 
    "Mycobacterium.scrofulaceum..SGM.", "Mycobacterium.senuense..SGM.", 
    "Mycobacterium.shimodei..SGM.", "Mycobacterium.simiae..SGM.", 
    "Mycobacterium.szulgai..SGM.", "Mycobacterium.triviale..SGM.", 
    "Mycobacterium.triplex..SGM.", "Mycobacterium.xenopi..SGM.", 
    "Mycobacterium.abscessus..RGM.", "X...Mycobacterium.bolletii....RGM.", 
    "X...Mycobacterium.massiliense....RGM.", "Mycobacterium.alvei..RGM.", 
    "Mycobacterium.boenickei..RGM.", "Mycobacterium.brumae..RGM.", 
    "Mycobacterium.chelonae..RGM.", "Mycobacterium.confluentis..RGM.", 
    "Mycobacterium.elephantis..RGM.", "Mycobacterium.fortuitum..RGM.", 
    "Mycobacterium.goodii..RGM.", "Mycobacterium.holsaticum..RGM.", 
    "Mycobacterium.houstonense..RGM.", "Mycobacterium.mageritense..RGM.", 
    "Mycobacterium.mucogenicum..RGM.", "Mycobacterium.peregrinum..RGM.", 
    "Mycobacterium.phocaicum..RGM.", "Mycobacterium.septicum..RGM.", 
    "Mycobacterium.thermoresistible..RGM.", "Treponema.pallidum", 
    "Listeria.monocytogenes", "Aspergillus.fumigatus", "Aspergillus.flavus", 
    "Aspergillus.niger", "Aspergillus.terreus", "Mucor.spp", 
    "Rhizopus.spp", "Fusarium.spp.", "Scedosporium.spp.", "Pneumocystis.jiroveci", 
    "Cryptococcus.neoformans", "Candida.albicans", "Candida.glabrata", 
    "NA.", "NA..1", "NA..2", "NA..3", "NA..4", "NA..5", "NA..6"
    ), class = "factor"), result = c("Positive", "Positive", 
    "Positive", "Positive", "Positive", "Positive", "Positive", 
    "Negative", "Positive", "Positive", "Negative", "Negative", 
    "Positive", "Positive", "Positive", "Negative", "Positive", 
    "Positive", "Positive", "Positive"), source = structure(c(2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L), .Label = c("test.", "source", "Adenovirus", 
    "Coronavirus", "Human.bocavirus", "X...HBoV.1", "Enterovirus", 
    "X...Coxsackievirus.A..with.types.", "X...Coxsackievirus.B..with.types.", 
    "Human.Rhinovirus", "X...Human.Rhinovirus.A..with.types.", 
    "X...Human.Rhinovirus.B..with.types.", "X...Human.Rhinovirus.C..with.types.", 
    "Cytomegalovirus..CMV.", "Herpes.Simplex.Virus", "Varicella.Zoster.Virus", 
    "Human.metapneumovirus", "Influenza.Virus.Type.A", "X...Influenza.Virus.Type.A..H1", 
    "X...Influenza.Virus.Type.A..2009.H1N1", "X...Influenza.Virus.Type.A..H3", 
    "Influenza.Virus.Type.B", "Influenza.Virus.Type.C", "Parainfluenza.Virus.Type.1", 
    "Parainfluenza.Virus.Type.2", "Parainfluenza.Virus.Type.3", 
    "Parainfluenza.Virus.Type.4", "Parechovirus", "Respiratory.Syncytial.Virus..RSV.", 
    "X..RSVA", "X..RSVB", "Hantavirus", "Measles.virus", "X...Measeles.virus.typing..including.wild.type.vs..vaccine.strain.", 
    "Bordetella.pertussis", "Bordetella.parapertussis", "Bordetella.bronchiseptica", 
    "Bordetella.holmsii", "Bordetella.petrii", "Bordetella.avium", 
    "Pseudomonas.aeruginosa", "Pseudomonas.fluorescens.group", 
    "Pseudomonas.putida.group", "Pseudomonas.putida", "Pseudomonas.stutzeri", 
    "Pseudomonas.fulva", "Pseudomonas.luteola", "Mycoplasma.pneumoniae", 
    "Mycoplasma.hominis", "Haemophilus.influenzae", "Haemophilus.parainfluenzae", 
    "Stenotrophomonas.maltophilia", "Burkholderia.cepacia.complex", 
    "Burkholderia.cenocepacia", "Burkholderia.multivorans", "Burkholderia.vietnamiensis", 
    "Burkholderia.dolosa", "Burkholderia.cepacia", "Burkholderia.gladioli", 
    "Legionella.pneumophila", "Legionella.feeleii", "Legionella.micdadei", 
    "Legionella.bozemanii", "Legionella.dumoffii", "Legionella.longbeachae", 
    "Staphylococcus.aureus", "Streptococcus.pneumoniae", "Streptococcus.pyogenes", 
    "Streptococcus.agalactiae", "Streptococcus.dysgalactiae.subsp..Equisimilis", 
    "Streptococcus.anginosus", "Streptococcus.constellatus", 
    "X...Streptococcus.constellatus.subsp..Constellatus", "X...Streptococcus.constellatus.subsp..Pharyngis", 
    "Streptococcus.intermedius", "Chlamydophila.pneumoniae", 
    "Chlamydia.trachomatis", "Ureaplasma.urealyticum", "Moraxella.catarrhalis", 
    "Citrobacter.freundii", "Citrobacter.koseri", "Morganella.morganii", 
    "Proteus.mirabilis", "Klebsiella.pneumoniae", "Klebsiella.oxytoca", 
    "Raoultella.planticola", "Raoultella.ornithinolytica", "Escherichia.coli", 
    "Enterobacter.aerogenes", "Enterobacter.cloacae", "Serratia.marcescens", 
    "Acinetobacter.baumanii", "Neisseria.meningitidis", "Eikenella.corrodens", 
    "Achromobacter.xylosoxidans", "Nocardia.cyriacigeorgica", 
    "Nocardia.nova.complex", "X...Nocardia.africana", "X...Nocardia.elegans", 
    "X...Nocardia.kruczakiae", "X...Nocardia.mikamii", "X...Nocardia.nova", 
    "X...Nocardia.veterana", "Nocardia.farcinica", "Nocardia.brasiliensis", 
    "Nocardia.abscessus.complex", "X...Nocardia.abscessus", "X...Nocardia.arthritidis", 
    "X...Nocardia.asiatica", "Nocardia.transvalensis.complex", 
    "X...Nocardia.transvalensis", "X...Nocardia.wallacei", "Nocardia.beijingensis.complex", 
    "X...Nocardia.beijingensis", "X...Nocardia.pneumoniae", "Nocardia.otitidiscaviarum", 
    "Nocardia.paucivorans", "Actinomyces.pyogenes", "Actinomyces.cardiffensis", 
    "Actinomyces.funkei", "Actinomyces.gerencseriae", "Actinomyces.graevenitzii", 
    "Actinomyces.israelii", "Actinomyces.meyeri", "Actinomyces.naeslundii", 
    "Actinomyces.odontolyticus", "Actinomyces.turicensis", "Actinomyces.viscosus", 
    "Mycobacterium.tuberculosis", "Mycobacterium.arupense..SGM.", 
    "Mycobacterium.avium..SGM.", "Mycobacterium.asiaticum..SGM.", 
    "Mycobacterium.branderi..SGM.", "Mycobacterium.chimaera..SGM.", 
    "Mycobacterium.celatum..SGM.", "Mycobacterium.florentinum..SGM.", 
    "Mycobacterium.heckeshornense..SGM.", "Mycobacterium.intermedium..SGM.", 
    "Mycobacterium.interjectum..SGM.", "Mycobacterium.intracellulare..SGM.", 
    "Mycobacterium.iranicum..SGM.", "Mycobacterium.kansasii..SGM.", 
    "Mycobacterium.kubicae..SGM.", "Mycobacterium.lentiflavum..SGM.", 
    "Mycobacterium.malmoense..SGM.", "Mycobacterium.nebraskense..SGM.", 
    "Mycobacterium.nonchromogenicum..SGM.", "Mycobacterium.palustre..SGM.", 
    "Mycobacterium.parascrofulaceum..SGM.", "Mycobacterium.phlei..SGM.", 
    "Mycobacterium.riyadhense..SGM.", "Mycobacterium.saskatchewanse..SGM.", 
    "Mycobacterium.scrofulaceum..SGM.", "Mycobacterium.senuense..SGM.", 
    "Mycobacterium.shimodei..SGM.", "Mycobacterium.simiae..SGM.", 
    "Mycobacterium.szulgai..SGM.", "Mycobacterium.triviale..SGM.", 
    "Mycobacterium.triplex..SGM.", "Mycobacterium.xenopi..SGM.", 
    "Mycobacterium.abscessus..RGM.", "X...Mycobacterium.bolletii....RGM.", 
    "X...Mycobacterium.massiliense....RGM.", "Mycobacterium.alvei..RGM.", 
    "Mycobacterium.boenickei..RGM.", "Mycobacterium.brumae..RGM.", 
    "Mycobacterium.chelonae..RGM.", "Mycobacterium.confluentis..RGM.", 
    "Mycobacterium.elephantis..RGM.", "Mycobacterium.fortuitum..RGM.", 
    "Mycobacterium.goodii..RGM.", "Mycobacterium.holsaticum..RGM.", 
    "Mycobacterium.houstonense..RGM.", "Mycobacterium.mageritense..RGM.", 
    "Mycobacterium.mucogenicum..RGM.", "Mycobacterium.peregrinum..RGM.", 
    "Mycobacterium.phocaicum..RGM.", "Mycobacterium.septicum..RGM.", 
    "Mycobacterium.thermoresistible..RGM.", "Treponema.pallidum", 
    "Listeria.monocytogenes", "Aspergillus.fumigatus", "Aspergillus.flavus", 
    "Aspergillus.niger", "Aspergillus.terreus", "Mucor.spp", 
    "Rhizopus.spp", "Fusarium.spp.", "Scedosporium.spp.", "Pneumocystis.jiroveci", 
    "Cryptococcus.neoformans", "Candida.albicans", "Candida.glabrata", 
    "NA.", "NA..1", "NA..2", "NA..3", "NA..4", "NA..5", "NA..6"
    ), class = "factor"), source_value = c("BAL", "BAL", "Bronch Wash", 
    "BAL", "Nasopharyngeal", "BAL", "Nasopharyngeal", "Nasopharyngeal", 
    "BAL", "Nasal", "Nasal", "Nasal", "Nasal", "Nasal Wash", 
    "Nasal", "Nasal", "Nasopharyngeal", "Nasal", "Nasopharyngeal", 
    "Nasal")), .Names = c("id", "age", "sex", "comments", "test", 
"result", "source", "source_value"), row.names = c(NA, 20L), class = "data.frame")  
</code></pre>