Get the column number in R given the column name

R

R Problem Overview


> 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!

R Solutions


Solution 1 - R

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

Should do it.

Solution 2 - R

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.

Solution 3 - R

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.

Solution 4 - R

..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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJo&#227;o DanielView Question on Stackoverflow
Solution 1 - RAri B. FriedmanView Answer on Stackoverflow
Solution 2 - RMatt DowleView Answer on Stackoverflow
Solution 3 - RIRTFMView Answer on Stackoverflow
Solution 4 - RKayView Answer on Stackoverflow