1

I am trying to get the name of a data.frame column based on its contents in R. For instance, in

dd= data.frame(col1=c(1,2,3), col2=c(4,5,6), col3=c(4,4,4))

I am looking for a something that returns col1 when I feed it c(1,2,3).

I found this advice elsewhere but it does not work for me; when, as suggested, I try this

colnames(dd)[which(dd == c(1,2,3), arr.ind = TRUE)] 
colnames(dd)[which(dd == c(1,2,3), arr.ind = TRUE)[2]] 

it returns all kinds of things, here the result for the 1st:

[1] "col1" "col2" "col3" "col1" "col1" "col1"
  • What is the best way to do this?

  • Is the above approach right; can you explain what exactly it does?

  • I was surprised not to find anything on this here; if it's a duplicate, I'd be glad for links / search terms.

10 Rep
  • 2,217
  • 7
  • 19
  • 33
patrick
  • 4,455
  • 6
  • 44
  • 61

1 Answers1

3

One way using identical,

names(dd)[sapply(dd, function(i)identical(i, c(1, 2, 3)))]
#[1] "col1"

names(dd)[sapply(dd, function(i)identical(i, c(4, 5, 6)))]
#[1] "col2"
Sotos
  • 51,121
  • 6
  • 32
  • 66
  • 1
    Also `setequal` if OP wants same contents, but to allow for different order / count of each unique element. – lmo Mar 21 '17 at 14:46
  • Nice, works beautifully, thanks for the help! Quick follow-up question: is there any advantage/difference to using `names` over `colnames`? They seem to be giving me the same result. – patrick Mar 21 '17 at 14:53
  • 1
    Well for data frames those 2 are the same. However, `names` does not work on matrices. That is as far as I know – Sotos Mar 21 '17 at 14:56
  • 1
    ahhh...here. [This will clarify it](http://stackoverflow.com/questions/24799153/what-is-the-difference-between-names-and-colnames) for you – Sotos Mar 21 '17 at 14:57