grep() to search column names of a dataframe

前端 未结 2 1061
灰色年华
灰色年华 2021-02-06 01:54

Is there a clearer, simpler, more direct, shorter way to do this:

Where df1 is a dataframe:

names(df1[grep(\"Yield\",names(df1))])

I wa

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-06 02:25

    You could easily define your own function to make it shorter. For instance,

     myfun <- function(x,y) names(y[grep(x, names(y))])
    

    Then, whenever you need it, you use

     myfun("Yield", df1)
    

    It hardly gets any shorter.

提交回复
热议问题