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)
I would like to rename column names, so that they would consist of only the biggest number in between. I thought of doing it with gsub/grep and a loop, for example: This returns me the column names
grep(pattern = "[50-100]", x = colnames(df), value= T )
Now, I would want the column name to be equal to the pattern, by which they were matched, which is the number from 50-100 and not smaller numbers. Is this possible? If not, do you know other generic way to rename the columns as described? Thanks in advance.