Un-select column which start with number

I have a big data frame which have column names like "2. city name", "44. country"
now I am trying to create a subset having column names not have which start with any number.

I am trying this way but not working

dat<-newdat1[,[,-grepl("^[[:digit:]]+",colnames(newdat1))]

do we have any other simple solution for that

Here is one solution using dplyr.

DF <- data.frame(Name = 1:3, `2.Value` = 1:3,`44.star`=1:3,Band=1:3,check.names = FALSE)
DF
#>   Name 2.Value 44.star Band
#> 1    1       1       1    1
#> 2    2       2       2    2
#> 3    3       3       3    3
library(dplyr,warn.conflicts = FALSE)
select(DF,matches("^[A-Z]"))
#>   Name Band
#> 1    1    1
#> 2    2    2
#> 3    3    3

Created on 2020-10-27 by the reprex package (v0.3.0)

And here is a solution using base R only.

DF <- data.frame(Name = 1:3, `2.Value` = 1:3, `44.star` = 1:3, Band = 1:3, check.names = FALSE)

DF[, grepl("^\\D", colnames(DF))]
#>   Name Band
#> 1    1    1
#> 2    2    2
#> 3    3    3

Created on 2020-10-28 by the reprex package (v0.3.0)

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.