mercredi 15 juillet 2020

Revise content of string factor using 'ifelse' isn't working

Kia ora data science community, I'm struggling to get an ifelse statement to work when trying to revise the contents of a data frame factor. I'm working with Trap Types of 5 different types, but two of the trap types aren't being summarized correctly. Here's the summary table of the trap types and number of observations associated with each type:

 DOC150 Double (Fiordland)        DOC150 Single (ATBT) 
                     107748                       20260 
      DOC150 Single (ATBT)  DOC200 Double (Run Through) 
                        456                        2324 
     DOC200 Double (Takaka)         DOC200 Double (ZIP) 
                      23748                        2472 
     DOC200 Single (Takaka)     DOC200 Single (Takaka)  
                      11258                       23668

I need DOC150 Single (ATBT) traps to be recognized as the same and summarized as such, with the same being true for DOC200 Single (Takaka). For whatever reason, the trap types are being summarized into individual categories; I suspect that when the information was pulled from the larger dataset that there was something wrong with the spacing of the names.

I've tried using the following code to reclassify one of the errant Trap Types, but to no avail: the categories remain, but the code changes all of the Trap Types from a character factor into a numeric factor and the final tally for each category remains unchanged.

Records2$TrapName<- as.character(ifelse(grepl("Single (Takaka)", Records2$TrapTypeTe), "DOC200 Single (Takaka)", Records2$TrapTypeTe))

Here's the resulting summary table:

     1      2      3      4      5      6      7      8 
107748  20260    456   2324  23748   2472  11258  23668

I thought I finally understood how to use grepl in ifelse statements, but now I'm stuck. I know how to do this in SAS, but R has thrown me for a loop. Any help would be greatly appreciated. Kia pai to ra, Doug

Aucun commentaire:

Enregistrer un commentaire