Skip to content
Prev 169585 / 398502 Next

how to delete specific rows in a data frame where the first column matches any string from a list

Andrew Choens wrote:
(this is actually off-topic, but since it may be interesting for the
general public, i keep the response cc: to r-help)

yes, you can do this with sed.  suppose you have two files, one (say,
sample.txt) with the data to be filtered, record fields separated by,
e.g., a tab character, and another (say, filter.txt) with patterns to be
matched.  a row from the first is passed to output only of its second
field does not match any of the patterns -- this corresponds to (a
simplified version of) the original problem.

then, the following should do:

sed "$(sed 's/^/\/^[^\\t]\\+\\t/; s/$/\/d/' filter.txt)" sample.txt >
filtered-sample.txt

(unless the patterns contain characters that interfere with the shell or
sed's syntax, in which case they'd have to be appropriately escaped.)

vQ

Thread (12 messages)

Laura Rodriguez Murillo how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 Doran, Harold how to delete specific rows in a data frame where the firstcolumn matches any string from a list Feb 6 Wacek Kusnierczyk how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 Laura Rodriguez Murillo how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 Wacek Kusnierczyk how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 Laura Rodriguez Murillo how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 andy how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 Sebastien Bihorel how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 Laura Rodriguez Murillo how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 Wacek Kusnierczyk how to delete specific rows in a data frame where the first column matches any string from a list Feb 6 andy how to delete specific rows in a data frame where the first column matches any string from a list Feb 8 Wacek Kusnierczyk how to delete specific rows in a data frame where the first column matches any string from a list Feb 9