Very similar questions have been asked here, here, and here. However, they all seem to rely on knowing the column names of the data.
I am trying to get the column index of a data frame that matches a numeric vector. For example, if I have some data and a vector like so:
dat <- data.frame(
x = c(1,2,3,4,5),
y = c(10,9,8,7,6),
z = c(2,4,6,8,10)
)
testVec <- c(2,4,6,8,10)
I would just like to return the column index of dat
that matches testVec
. We can see that dat$z
matches testVec
... so in this situation I would just like to return 3
.
Any suggestions as to how I could do this?