Home > Back-end >  Get column index from data frame that matches numeric vector?
Get column index from data frame that matches numeric vector?

Time:02-18

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?

CodePudding user response:

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 

CodePudding user response:

You perhaps can try this

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

CodePudding user response:

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)

CodePudding user response:

An option with select from dplyr

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