Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the column position

Using the iris dataframe, I want the position of all the columns with "Length" in the column name.

I can find them manually using the below code but I want a method that will allow me to just search for the word "Length" without having to specify the exact column name.

iris_df <- iris

which(colnames(iris_df) == "Sepal.Length") # returns 1
which(colnames(iris_df) == "Petal.Length") # returns 3

Can anyone suggest any code to achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use grep:

grep("Length", colnames(iris_df))
[1] 1 3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading