Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting a specific string pattern

I have a database with a string column, this database looks like this:

structure(list(variables = c("data$Ageee[data$Beneficiary == 1] and data$Age[data$Beneficiary == 0]",
"data$var[data$Beneficiary == 1] and data$Age[data$Beneficiary == 0]",
"data$variable_test[data$Beneficiary == 1] and data$Age[data$Beneficiary == 0]"
), values = c(0, 0, 0)), class = "data.frame", row.names = c(NA,
-3L))

However, I would like to get a new column considering the text after the first $ and before the first [, so I get:

structure(list(variables = c("Ageee", "var", "variable_test"
), values = c(0, 0, 0)), class = "data.frame", row.names = c(NA,
-3L))

I appreciate any help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

We may use sub to capture the word ((\\w+) after the $$ is a metacharacter in regex that denotes the end of the string, so it is escaped (\\)

df1$variables <- sub("\\w+\\$(\\w+).*", "\\1", df1$variables)

-output

> df1
      variables values
1         Ageee      0
2           var      0
3 variable_test      0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading