separate function

Hi, I would like to use the separate function and extract information that is inside brackets in a character into another column.

This is my code so far, however, I just cannot figure out the correct code for the sep to only extract the information from inside the brackets.

SurveyMonthly_2010_10 <- SurveyMonthly_2010_10 %>%
separate(
col = ClosefriendsWestgate,
into = c("ClosefriendsWestgate_label", "ClosefriendsWestgate_percentage"),
sep = "([\.\?\:])"
)

Can someone help me out with this one? Thanks!

If I understand correctly what you want, I think you should use a function like str_extract from the stringr package. Can you post a small example of the data you have and the result you want to get?

Hi! Thanks for your reply.

The data looks like this:
About half (41-60%) ()

I would like to end up with two columns
'About a half' | '41-60%'

Thanks!

I'm sure there are more elegant ways to do this.

DF <- data.frame(TRG= "About half (41-60%) ()")
DF
                     TRG
1 About half (41-60%) ()
library(stringr)
DF <- DF %>% mutate(New = str_extract(TRG, "\\([^\\)]+"),
               New = str_remove(New, "\\("),
               TRG = str_extract(TRG,"^[^\\(]+"),
               TRG = str_trim(TRG))
 DF
         TRG    New
1 About half 41-60%

This worked perfectly, thank you very much for your help @FJCC!

This topic was automatically closed 21 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.