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>
Hello,
Package stringr has functions str_sort and str_order, both with an
argument 'numeric' that will sort the numbers correctly.
Maybe that's what you are looking for, see the example below.
x <- sample(sprintf("ab%d", 1:20)) # shuffle the vector
stringr::str_sort(x, numeric = TRUE) # sort considering the numbers
Hope this helps,
Rui Barradas
?s 16:58 de 20/12/21, Stephen H. Dawson, DSL via R-help
escreveu:> 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,
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 at 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 at 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 at 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.
What is wrong with as.data.frame(lapply( Data, sort, decreasing = TRUE )) ? On December 20, 2021 8:58:48 AM PST, "Stephen H. Dawson, DSL via R-help" <r-help at r-project.org> wrote:>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.-- Sent from my phone. Please excuse my brevity.
Thanks. sort(unique(Data[[1]])) This syntax provides row numbers, not column values. *Stephen Dawson, DSL* /Executive Strategy Consultant/ Business & Technology +1 (865) 804-3454 http://www.shdawson.com <http://www.shdawson.com> On 12/20/21 11:58 AM, Stephen H. Dawson, DSL via R-help wrote:> 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.