dplyr filter on a vector rather than a dataframe in R

后端 未结 3 642
傲寒
傲寒 2021-02-08 14:57

This seems like a simple question, but I have not come across a clean solution for it yet. I have a vector in R and I want to remove certain elements from the vector, however I

3条回答
  •  攒了一身酷
    2021-02-08 15:22

    Pretty sure dplyr only really operates on data.frames. Here's a two line example coercing the vector to a data.frame and back.

    myDf = data.frame(states = gsub(" ", "-", tolower(state.name))) %>% filter(states != "alaska")
    all_states = myDf$states
    

    or a gross one liner:

    all_states = (data.frame(states = gsub(" ", "-", tolower(state.name))) %>% filter(states != "alaska"))$states
    

提交回复
热议问题