Get the column number in R given the column name

r

Solution

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

Should do it.

Problem

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!

Original source

Related problems