Across and stringr together with mutate

I'm sorry, but I couldn't get reprex.

I have many character columns with numbers and characters. I want to remove characters and leave numbers but to do this with many variables at the same time.

I am using this code:

pandemic_siordia %>%
mutate(across(anxiety_1:lifesat_5), str_extract("^\d+"))

with different modifications (changing variables, or putting ".," in str_extract) but still get various errors.

What can I possibly be doing wrong?

I cannot test this at the moment but try putting str_extract() inside of the across function.

pandemic_siordia %>%
mutate(across(.cols = anxiety_1:lifesat_5,  .fns = str_extract, pattern = "^\d+"))
1 Like

Thank you very much. It worked! You have helped me on the last three questions, I don't know how to thank you.

you can try gsub() for removing characters from a given string.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.