Reputation: 97
I'm working on a data frame containing information about municipalities and its respective states like below:
Municipality <- c('Ariquemes', 'Borba', 'Bagre')
State <- c('RO', 'AM', 'PA')
df <- data.frame(Municipality, State)
I used the function paste
to create a new column containing both the municipality and state names:
df$Municipality_State<-paste(df$Municipality, df$State)
df>
Municipality State Municipality State
Ariquemes RO Ariquemes PA
Borba AM Borba AM
Bagre PA Bagre PA
Now, I'd like to add brackets around the State name to merge with another database i'm working on. The output I'm looking for is like:
Municipality State Municipality_State
Ariquemes RO Ariquemes (PA)
Borba AM Borba (AM)
Bagre PA Bagre (PA)
I would appreciate your comments Regards.
Upvotes: 0
Views: 218
Reputation: 887691
We can use sprintf
df$Municipality_State <- sprintf("%s (%s)", df$Municipality, df$State)
Upvotes: 0
Reputation: 389175
Without making it too complicated, we can add brackets while pasting the columns.
df$Municipality_State <- paste0(df$Municipality, " (", df$State, ")")
df
# Municipality State Municipality_State
#1 Ariquemes RO Ariquemes (RO)
#2 Borba AM Borba (AM)
#3 Bagre PA Bagre (PA)
Upvotes: 1