[R] elimination of multiple rows

John Day jday at csihq.com
Thu Jul 18 15:00:48 CEST 2002


Sounds like you want something that can run as a preprocess step from the 
command line. If so (and if you're in Unix) you can use 'uniq', which will 
remove any adjacent dupe lines, but otherwise leave the file in the 
original order:

 > uniq myfile > umyfile

If you really want to get rid of _all_ dupes then sort first:

 > sort myfile | uniq > umyfile
 > sort -u myfile > umyfile

BTW, 'uniq' has a nifty -c  option which counts the number of dupes and can 
be used to create a useful 'histogram', sorted by frequency. Must sort 
first to make this work and then sort again after the uniq to arrange in 
descending order:

sort myfile | uniq -c | sort -r -n


John Day
Staff Scientist
Computer Science Innovations
Melbourne, FL

At 08:32 AM 7/18/02 +0200, you wrote:
>Hi all,
>Excuse me for posting a question which may not be true R.
>I want to process a text file so that the resulting file contains only one
>of possibly multiple consecutive rows. Example (the row numbers do not
>belong to the file):
>(1) a b c d
>(2) a b c d
>(3) A b c d
>(4) A b c d
>(5) A b c d
>(6) a b c d
>(7) a b c D
>resulting in:
>(1) a b c d
>(3) A b c d
>(6) a b c d
>(7) a b c D
>(6) could be disposed of also by first sorting the original file.
>Does anybody have a script ready, preferably in Pearl? I do not know Pearl
>well enough to write it myself.
>Thanks for your help.
>Dr.sc.math.Christian W. Hoffmann
>Mathematics and Statistical Computing
>Landscape Dynamics and Spatial Development
>Swiss Federal Research Institute WSL
>Zuercherstrasse 111
>CH-8903 Birmensdorf, Switzerland
>phone: ++41-1-739 22 77    fax: ++41-1-739 22 15
>e-mail: christian.hoffmann at wsl.ch
>www: http://www.wsl.ch/staff/christian.hoffmann/
>r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
>Send "info", "help", or "[un]subscribe"
>(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list