I have a dataframe AData that of which I have extracted a certain subset of its column names say SpecialNames. I would like to know how to reference these columns in a for loop.
My current code looks like this:
SpecialNames <- setdiff(colnames(AData), colnames(BData))
for ( i in SpecialNames ) {
AData$i <- NULL # Do something to AData$i such as delete it or something else
}
Alas, AData$i does not seem to reference the column of dataframe AData with name i. Is there a different syntax that would give me that?
I read in this post here that: "the $ is for interactive usage. Instead, when programming, i.e. when the column name is to be interpreted, you need to use [ or [[, hence I replaced sample$i.imp with sample[[paste0(i, '.impt')]]".
Based on this comment, I guessed that perhaps the syntax I have been looking for is AData$[i] or AData$[[i]] or AData$[[paste0(i)]] but none of these seem to work either.
Any ideas?