69

Possible Duplicate:
Get column index from label in a data frame

I need to get the column number of a column given its name.

Supose we have the following dataframe:

df <- data.frame(a=rnorm(100),b=rnorm(100),c=rnorm(100))

I need a function that would work like the following:

getColumnNumber(df,"b")

And it would return

[1] 2

Is there a function like that?

Thanks!

Community
  • 1
  • 1
João Daniel
  • 8,696
  • 11
  • 41
  • 65

4 Answers4

136
which( colnames(df)=="b" )

Should do it.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
74

One fast and neat method is :

> match("b",names(df))
[1] 2

That avoids the vector scan that == and which do. If you have a lot of columns, and you do this a lot, then you might like the fastmatch package.

> require(fastmatch)
> fmatch("b",names(df))
[1] 2

fmatch is faster than match, but on subsequent calls it's not just faster, it's instant.

Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
  • 2
    If I'm using just one variable (one column), "match" and "which" both work equally as well for me. However, if I'm using more than one variable, "which" throws errors, while "match" doesn't give me errors. So for my situation, "match" is a superior choice. Thanks! – jeramy townsley Mar 16 '16 at 19:32
14

Another method which generalizes better to non-exact matching tasks is to use grep which returns a vector of numbers for matches with patterns within character vectors :

grep("^b$", colnames(df) )

If you wanted to remove by position number all of the columns whose names begin with "b", you would write:

df[ , - grep("^b", colnames(df) )]

That neatly finesses the issue that you cannot use negative indexing with character vectors.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
14

..especially, if you need to get several column indices the below approach applies:

> df <- data.frame(a=rnorm(100),b=rnorm(100),c=rnorm(100))
> which(names(df)%in%c("b", "c"))
[1] 2 3

if you use this for subsetting df you don't need which()

> df_sub <- df[, names(df)%in%c("b", "c")]
> head(df_sub)
           b          c
1  0.1712754  0.3119079
2 -1.3656995  0.7111664
3 -0.2176488  0.7714348
4 -0.6599826 -0.3528118
5  0.4510227 -1.6438053
6  0.2451216  2.5305453
Kay
  • 2,702
  • 6
  • 32
  • 48