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 grepl to return TRUE only if there is a match with the full string

I have example data as follows:

library(data.table)
dat <- fread("q1   q2 ...1  ..2  q3..1 ..1
       NA   response other  else response other
       1    4        NA   NA   1    NA")

I wanted to filter out all columns that are automatically named when reading in an Excel file with missing column names, which have names like ..x. I thought that the following piece of code would work:

grepl("\\.+", names(dat))
[1] FALSE FALSE  TRUE  TRUE  TRUE  TRUE

But it also filters out columns which have a similar structure as column q3..1.

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

Although I do not know why the ..x part is added to such a column (because it was not empty), I would like to adapt the grepl code, so that the outcome is TRUE, unless the structure is ONLY ..x.

How should I do this?

Desired output:

grepl("\\.+", names(dat))
[1] FALSE FALSE  TRUE  TRUE  FALSE TRUE

>Solution :

Use an anchor ^ to state that the dots have to be in the start of the string:

grepl("^\\.+", names(dat))
#[1] FALSE FALSE  TRUE  TRUE FALSE  TRUE
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