Let's assume I have a following data frame:
xx2xx30x4xx <- rep(5,30)
yyyy3yy50y5yyy <- rep(4,30)
zz12zzzz70z8zz <- rep(7,30)
df <- data.frame(xx2xx30x4xx,yyyy3yy50y5yyy,zz12zzzz70z8zz)
grep(pattern = "[50-100]", x = colnames(df), value= T )
xxxxxx30xxxx <- rep(5,30)
yyyyyyy50yyyyy <- rep(4,30)
zzzzzzz70zzzz <- rep(7,30)
df <- data.frame(zzzzzzz70zzzz,yyyyyyy50yyyyy,xxxxxx30xxxx)
grep(pattern = "[0-100]", x = colnames(df), value= T )
new_colnames <- gsub("\\D", "", colnames(df))
colnames(df) <- new_colnames
I hope i understood you correctly. The gsub command erases everything that is not a digit from the column names, so you're left with the numbers inbetween.
EDIT:
This code matches a two-digit number in your string between 30 and 70, and extracts it.
xxxxxx30xxxx <- rep(5,30)
yyyyyyy50yyyyy <- rep(4,30)
zzzzzzz70zzzz <- rep(7,30)
df <- data.frame(zzzzzzz70zzzz,yyyyyyy50yyyyy,xxxxxx30xxxx)
grep(pattern = "[0-100]", x = colnames(df), value= T )
# new_colnames <- gsub("\\D", "", colnames(df))
new_colnames <- regmatches(colnames(df), regexpr("([3-6][0-9])|([7][0])",colnames(df)))
colnames(df) <- new_colnames
Here's some information on regular expressions and string operations:
https://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html