Home > Blockchain >  How to select all columns where one one column ends with '006'
How to select all columns where one one column ends with '006'

Time:08-02

I'm using dplyr and I want to select all the columns on the table but return only the rows where one specific column ends with '006'.

select(sample_id, ends_with("006"), everything())

The code above doesn't work. When I run it, it returns all rows (or more than I need -- it's a huge dataset).

I've tried using:

filter(sample_id == ends_with('006')) 

but ends_with() needs to be used within a select function.

CodePudding user response:

For a base R approach, we could use grepl here along with a data frame subset operation:

df_out <- df[grepl("006$", df$sample_id), ]

CodePudding user response:

ends_with() is for subseting columns. You should use endsWith() from base:

filter(endsWith(sample_id, "006"))

It's equivalent to

filter(grepl("006$", sample_id))
  • Related