how to move specific observations starting with a pattern to a new column

I have the following data

charge <- c('UUW SHOOT AT MV PER BLDG','UNLAWFUL POSS FIREARM','lation Management Report','ARMED CRIMINAL ACTION'
,'ARMED CRIMINAL ACTION')
casecol1 <- c('1922-3','F','F','F','F')
casecol2 <- c('DETAIL','1822-3','','','1522-4')


df <- data.frame(charge,casecol1,casecol2)

                    charge casecol1 casecol2
1 UUW SHOOT AT MV PER BLDG   1922-3   DETAIL
2    UNLAWFUL POSS FIREARM        F   1822-3
3 lation Management Report        F         
4    ARMED CRIMINAL ACTION        F         
5    ARMED CRIMINAL ACTION        F   1522-4

I am trying to move the observations that start with the pattern with 4 digits and a dash e.g 1922- to a new column so that it looks like this in the newcol.

                     charge casecol1 casecol2 newcol
1 UUW SHOOT AT MV PER BLDG   1922-3   DETAIL  1922-3
2    UNLAWFUL POSS FIREARM        F   1822-3  1822-3
3 lation Management Report        F         
4    ARMED CRIMINAL ACTION        F         
5    ARMED CRIMINAL ACTION        F   1522-4  1522-4

How would I be able to do this?



Read more here: https://stackoverflow.com/questions/67480445/how-to-move-specific-observations-starting-with-a-pattern-to-a-new-column

Content Attribution

This content was originally published by Nathan123 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: