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?
>Solution :
You can use grep:
grep("Length", colnames(iris_df))
[1] 1 3