Reputation: 474
I have a dataset where the column names are survey questions and are extensive, sometimes containing different punctuation from panel to panel. As such, I would like to rename all column titles that are a partial string match to a more sensible var name.
My current attempt was:
df %>%
select_all(~str_replace(., "How would you describe your gender?", "cnt_gender"))
However, this only replaces the exact string match and leaves any others. For example:
Target string: "How would you describe your gender? (Select only one option)"
df %>%
select_all(~str_replace(., "How would you describe your gender?", "cnt_gender"))
Desired output string: "cnt_gender"
Actual output string: "cnt_gender (Select only one option)"
What I am looking for is to completely rename the columns based on a partial string match.
Upvotes: 0
Views: 525
Reputation: 21802
You can use .*
after the string you want to replace to capture anything else that might be lurking beyond (including nothing).
Target_strings <- c("How would you describe your gender? (Select only one option)",
"How would you describe your gender?")
str_replace(Target_strings, "How would you describe your gender?.*", "cnt_gender")
# [1] "cnt_gender" "cnt_gender"
Upvotes: 2