[R] Adding SORT to UNIQUE

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


Stephen,

 

Sorry about that. I tried modifying what you had and realize the use of [] returned a data.frame and you need [[]] to return a vector.

 

Try this:

 

sort(unique(Data[[1]]))

 

 

From: Stephen H. Dawson, DSL <service using shdawson.com> 
Sent: Monday, December 20, 2021 12:32 PM
To: Avi Gross <avigross using verizon.net>; r-help using r-project.org
Subject: Re: [R] Adding SORT to UNIQUE

 

Thanks for the reply.

sort(unique(Data[1]))
Error in `[.data.frame`(x, order(x, na.last = na.last, decreasing = decreasing)) : 
  undefined columns selected

The recommended syntax did not work, as listed above.



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



On 12/20/21 12:15 PM, Avi Gross via R-help wrote:

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  <mailto:r-help-bounces using r-project.org> <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  <mailto:r-help using r-project.org> <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> <http://www.shdawson.com>
 
______________________________________________
R-help using r-project.org <mailto: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.

______________________________________________
R-help using r-project.org <mailto: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.





	[[alternative HTML version deleted]]



More information about the R-help mailing list