creating list of column with regular expression

I want to create a list of column with regular expression for example.

patt <- "col.1_"

now i want to create a list of column names which can fetch names from data frame like.

("col.1_1","col.1_2","col.1_3",.......)  but it should exclude column names like col.1_1id, col.1_2id etc


I am not sure exactly what names you want to include and exclude. In the following code, column names can have a single number after the underscore.

DF <- data.frame(col.1_1 = 1, col.1_1id = 3, 
                  col.1_2 = 2, col.1_2id = 5)
grep(pattern = "col.1_\\d$", colnames(DF), value = TRUE)
[1] "col.1_1" "col.1_2"

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.