Home > Net >  R replace characters in a column based on a word in another column
R replace characters in a column based on a word in another column

Time:06-07

I have a dataset that has multiple columns. In the Date_Received column there are certain rows that have the word Abandonment in them. I would like to search the rows in this column that have this word, and then replace the characters AP with AB in the corresponding rows of the AP column.

How can I do this?

Sample data df:

structure(list(id = 1:6, Date_Received = c("Addition 1/2/2018", 
"Swimming Pool 1/8/2018", "Swimming Pool 1/8/2018", "Abandonment 1/9/2018", 
"Swimming Pool 1/12/2017", "Abandonment 2/5/2018"), Date_Approved = c("1/2/2018", 
"1/8/2018", "1/8/2018", "1/9/2018", "1/12/2017", "2/5/2018"), 
    AP= c("AP-18-001", "AP-18-002", "AP-18-003", "AP-18-004", 
    "AP-18-005", "AP-18-006"), Permit.. = c("06-SE-1812147", 
    "06-SS-1813516", "06-SS-1813699", "06-SE-1814032", "06-SE-1814924", 
    "06-SS-1820333"), Owner.Name.Agent = c("Tiny Tots Academy, Inc Mike Davis", 
    "Ernesto & Elizabeth Diaz Ensign Pools", "DSL Contruction & Investments LLC", 
    "BSD North Federal LLC EPOCA Plumbing Corp", "Maria Silva Parkwood Pools And Pavers LLC", 
    "HPA Borrower Westland Plumbing"), X = c("NA NA", "NA NA", 
    "NA NA", "NA NA", "NA NA", "NA NA"), Project.Address.City = c("61111 Washington Street Hollywood, 33024", 
    "1224 SW 170 Avenue SW Ranches, 33331", "1233 NW 6 Place Plantation, 33325", 
    "1231 N Federal Hwy Hollywood, 33020", "3223 Dawson Street", 
    "3691 SW 31 Avenue Fort Lauderdale")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -6L))

Code:

library(tidyverse)
library(dplyr)

df = df %>% grepl("Abandonement", df$Date_Received) %>% str_replace(df$AP) #.... stuck

CodePudding user response:

This should do it:

df %>% 
  mutate(AP = ifelse(grepl("Abandonment", Date_Received, fixed = TRUE), gsub("AP", "AB", AP), AP))

Which gives:

# A tibble: 6 × 8
     id Date_Received           Date_Approved AP        Permit..      Owner.Name.Agent                          X     Project.Address.City          
  <int> <chr>                   <chr>         <chr>     <chr>         <chr>                                     <chr> <chr>                         
1     1 Addition 1/2/2018       1/2/2018      AP-18-001 06-SE-1812147 Tiny Tots Academy, Inc Mike Davis         NA NA 61111 Washington Street Holly…
2     2 Swimming Pool 1/8/2018  1/8/2018      AP-18-002 06-SS-1813516 Ernesto & Elizabeth Diaz Ensign Pools     NA NA 1224 SW 170 Avenue SW Ranches…
3     3 Swimming Pool 1/8/2018  1/8/2018      AP-18-003 06-SS-1813699 DSL Contruction & Investments LLC         NA NA 1233 NW 6 Place Plantation, 3…
4     4 Abandonment 1/9/2018    1/9/2018      AB-18-004 06-SE-1814032 BSD North Federal LLC EPOCA Plumbing Corp NA NA 1231 N Federal Hwy Hollywood,…
5     5 Swimming Pool 1/12/2017 1/12/2017     AP-18-005 06-SE-1814924 Maria Silva Parkwood Pools And Pavers LLC NA NA 3223 Dawson Street            
6     6 Abandonment 2/5/2018    2/5/2018      AB-18-006 06-SS-1820333 HPA Borrower Westland Plumbing            NA NA 3691 SW 31 Avenue Fort Lauder…
  • Related