3

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?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Electrino
  • 2,636
  • 3
  • 18
  • 40

4 Answers4

4

Here's a base R approach, which compares every column in dat with testVec to see if they are identical. Use which to output the column index if they're identical.

which(sapply(1:ncol(dat), function(x) identical(dat[,x], testVec)))
[1] 3

UPDATE @nicola has provided a better syntax to my original code (you can see it in the comment under this answer):

which(sapply(dat, identical, y = testVec))
z 
3 
benson23
  • 16,369
  • 9
  • 19
  • 38
2

You perhaps can try this

> which(colSums(dat == testVec) == nrow(dat))
z
3
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
1

An option with select from dplyr

library(dplyr)
dat %>%
   select(where(~ all(testVec == .x))) %>% 
   names %>% 
   match(names(dat))
[1] 3
akrun
  • 874,273
  • 37
  • 540
  • 662
1

Subtract the testVec.

which(colSums(dat - testVec) == 0)
# z 
# 3 

Without name:

unname(which(colSums(dat - testVec) == 0))
# [1] 3

Data:

dat <- structure(list(x = c(1, 2, 3, 4, 5), y = c(10, 9, 8, 7, 6), z = c(2, 
4, 6, 8, 10)), class = "data.frame", row.names = c(NA, -5L))
testVec <- c(2, 4, 6, 8, 10)
jay.sf
  • 60,139
  • 8
  • 53
  • 110