I have a larger dataset following the same order, a unique date column, data, unique date column, date, etc. I am trying to subset not just the data column by name but the uniqu
How about
NameList <- c("Fire","Earth") idx <- match(NameList, names(MAINDF)) idx <- sort(c(idx-1, idx)) NewDF <- MAINDF[,idx]
Here we use match() to find the index of the desired column, and then we can use index subtraction to grab the column before it
match()