Here's my code
library(dplyr)
df <- data.frame(
Compound = c("methyl-", "ethylidene")
)
merged_string_df <- df %>%
mutate(Compound = ifelse(str_detect(Compound, "\\ $(and|,|with)$"),
str_c(Compound, " "),
Compound)) %>%
pull(Compound) %>%
paste(collapse=" ")
print(merged_string_df)
My desired output is methyl-ethylidene but it generates methyl- ethylidene, with a space in between the two strings.
But when my dataframe is
df <- data.frame(
Compound = c("methyl,", "ethylidene")
)
My desired output is methyl, ethylidene. Also I would like to extend this to strings that end with and and with. Can anyone help please? Thanks.
I think it's easier to check your condition if each part is in its own column:
Created on 2023-11-04 with reprex v2.0.2