Iterate over pattern grep function in R

Viewed 166

I am novice in R. I want to write the loop for the below code in R.

day1<-XYZ[, -grep("_0", colnames(XYZ))]
day2<-XYZ[, -grep("_0|_1", colnames(XYZ))]
day3<-XYZ[, -grep("_0|_1|_2", colnames(XYZ))]
day4<-XYZ[, -grep("_0|_1|_2|_3", colnames(XYZ))]
day5<-XYZ[, -grep("_0|_1|_2|_3|_4", colnames(XYZ))]
day6<-XYZ[, -grep("_0|_1|_2|_3|_4|_5", colnames(XYZ))]
day7<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6", colnames(XYZ))]
day8<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7", colnames(XYZ))]
day9<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8", colnames(XYZ))]
day10<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8|_9", colnames(XYZ))]
day11<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8|_9|_10", colnames(XYZ))]
day12<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8|_9|_10|_11", colnames(XYZ))]
day13<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8|_9|_10|_11|_12", colnames(XYZ))]
day14<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8|_9|_10|_11|_12|_13", colnames(XYZ))]
day15<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8|_9|_10|_11|_12|_13|_14", colnames(XYZ))]
day16<-XYZ[, -grep("_0|_1|_2|_3|_4|_5|_6|_7|_8|_9|_10|_11|_12|_13|_14|_15", colnames(XYZ))]

Thanks in advance!!

1 Answers
Related