[R] Adding SORT to UNIQUE

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Mon Dec 20 18:15:06 CET 2021


Stephen,

You can sort using sort() either before or after doing a unique. Unique
removes all duplicates in any order so sorting before may be wasteful. in
your data shown below, do this:

sort(unique(Data[1]))
sort(unique(Data[2]))
sort(unique(Data[3]))
sort(unique(Data[4]))

Even simpler is to define a function like this:

unisort <- function(vec) sort(unique(vec))

and use it like this:

unisort(Data[1])


And since you seem to want all of the first four columns of Data you may
want to do them all at once using something like:

lapply(Data[1:4], unisort)

As you note, the sort ordering depends on the data and perhaps on options
you specify.

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Stephen H. Dawson,
DSL via R-help
Sent: Monday, December 20, 2021 11:59 AM
To: Stephen H. Dawson, DSL via R-help <r-help using r-project.org>
Subject: [R] Adding SORT to UNIQUE

Hi,


Running a simple syntax set to review entries in dataframe columns. Here is
the working code.

Data <- read.csv("./input/Source.csv", header=T)
describe(Data)
summary(Data)
unique(Data[1])
unique(Data[2])
unique(Data[3])
unique(Data[4])

I would like to add sort the unique entries. The data in the various columns
are not defined as numbers, but also text. I realize 1 and 10 will not sort
properly, as the column is not defined as a number, but want to see what I
have in the columns viewed as sorted.

QUESTION
What is the best process to sort unique output, please?


Thanks.
--
*Stephen Dawson, DSL*
/Executive Strategy Consultant/
Business & Technology
+1 (865) 804-3454
http://www.shdawson.com <http://www.shdawson.com>

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list