I'm attempting to test if each of the column names in my dataframe contain a particular string (in this case "Fld". My attempt below is not compiling and I'm not sure where I'm going wrong. Any help would be appreciated.
varnames <-colnames(data)
for (i in 1:len(varnames)){
if grepl("Fld",varnames[i])==TRUE {
print varnames[i]
}
}
indx <- grepl('Fld', colnames(data))
would be enough to test. If you want to extract the columns,data[indx]
or the column namescolnames(data)[indx]
[0]
? That returns an empty vector every time. I think you wantvarnames[i]
. Also, itsprint(varnames[i])
with the parens. However, akrun's solution is superior to the loop.