I have a large data set and I'm trying to format the addresses and move the apartment numbers to a different column. I want to extract that pattern from the address and move it into a new column, however, some rows already have the correct format I need. I've been using "str_extract(column, pattern)" but if the row doesn't match the pattern, it's replacing that row with NA. Please help.
An example of the data:
> data
name          add1              add2
John Doe      123 Main St       Apt 2
Jane Doe      2 S Main Apt 5
Jerry Day     77 N Main Unit 1  
Tom May       11 E Main         PO Box 7
I want it to look like this:
> data
name          add1               add2
John Doe      123 Main St        Apt 2
Jane Doe      2 S Main           Apt 5
Jerry Day     77 N Main          Unit 1
Tom May       11 E Main          PO Box 7
My code is as follows and my result:
> data$add2 <- str_extract(data$add1, "Apt [0-9]{1}|Unit [0-9]{1}")
> data
name          add1               add2
John Doe      123 Main St        NA
Jane Doe      2 S Main           Apt 5
Jerry Day     77 N Main          Unit 1
Tom May       11 E Main          NA
Can someone help with my problem? Thank you,
 
    