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
.
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
CodePudding user response:
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
CodePudding user response:
We may do
library(dplyr)
dat %>%
select(matches('^[^.] $'))
q1 q2
<int> <char>
1: NA response
2: 1 4