Electrino
Electrino

Reputation: 2890

Get column index from data frame that matches numeric vector?

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?

Upvotes: 3

Views: 1151

Answers (4)

jay.sf
jay.sf

Reputation: 72593

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)

Upvotes: 1

akrun
akrun

Reputation: 886938

An option with select from dplyr

library(dplyr)
dat %>%
   select(where(~ all(testVec == .x))) %>% 
   names %>% 
   match(names(dat))
[1] 3

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 101024

You perhaps can try this

> which(colSums(dat == testVec) == nrow(dat))
z
3

Upvotes: 1

benson23
benson23

Reputation: 19097

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 

Upvotes: 4

Related Questions