2022-05-15

Placing Commas Between Names

I am trying to find out if certain patterns appear within a data frame.

Suppose I have the following "dictionary of patterns" (notice "james" vs "jamesj"):

patterns <- c("john", "jack", "james", "jamesj", "jason")

The actual data frame ("date_frame") I have looks like this:

  id                                              names
1  1                                     johnjack jameS
2  2                             john/james, jasonjames
3  3                                    peter_jackjason
4  4                                   jamesjasonj jack
5  5 jamesjjason, johnjasonjohn , jason-jack sam _ peter

The final result I am trying to produce should look like this:

  id                                                         names
1  1                                             john, jack, james
2  2                                     john, james, jason, james
3  3                                            peter, jack, jason
4  4                                          jamesj, asonj,  jack
5  5 jamesj, jason, john, jason, john , jason, jack,  sam ,  peter

I tried looking at this post here (R: insert comma after each element from the output) and tried the answer provided there:

> data_frame$parsed_names = dput(data_frame$names)



  id                                                         names                                                  parsed_names
1  1                                             john, jack, james                                             john, jack, james
2  2                                     john, james, jason, james                                     john, james, jason, james
3  3                                            peter, jack, jason                                            peter, jack, jason
4  4                                          jamesj, asonj,  jack                                          jamesj, asonj,  jack
5  5 jamesj, jason, john, jason, john , jason, jack,  sam ,  peter jamesj, jason, john, jason, john , jason, jack,  sam ,  peter

But this is not corresponding to what I wanted.

I then tried this post over here (insert commas in text string after certain words in r) and tried the answer provided there:

library(gsubfn)

data_frame$parsed_names = gsubfn("\\w+", as.list(setNames(paste0(patterns, ","), patterns)), 
  format(data_frame$names))

 data_frame
  id                                                         names                                                         parsed_names
1  1                                             john, jack, james     john,, jack,, james,                                            
2  2                                     john, james, jason, james    john,, james,, jason,, james,                                    
3  3                                            peter, jack, jason      peter, jack,, jason,                                           
4  4                                          jamesj, asonj,  jack      jamesj,, asonj,  jack,                                         
5  5 jamesj, jason, john, jason, john , jason, jack,  sam ,  peter jamesj,, jason,, john,, jason,, john, , jason,, jack,,  sam ,  peter
  • Can someone please show me how to fix this?

Thank you!



No comments:

Post a Comment