How to sort a character vector where elements contain letters and numbers in R? The Next CEO of Stack OverflowOrder colnames by char + numberOrder data.frame after numbers within character column in ROrdering character values while preserving number orderSorting A Character Dataframe According to desired waysort strings by numbers inside themArrange column in data framehow to group rows without ordering?Sorting character vector containing semantic versionsHow to sort by Library of Congress Classification (LCC) number in RSort a dataframe based on a character column containing letters followed by numbers in RHow do I sort a list of dictionaries by a value of the dictionary?How do I sort a dictionary by value?How do I sort an NSMutableArray with custom objects in it?How to sort a dataframe by multiple column(s)?Java Array Sort descending?Sorting an array in descending order in RubyHow to Sort Multi-dimensional Array by Value?How to Sort a List<T> by a property in the objectGrouping functions (tapply, by, aggregate) and the *apply familySwift Beta performance: sorting arrays
Is it okay to store user locations?
Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis
Why here is plural "We went to the movies last night."
Implement the Thanos sorting algorithm
Why is there a PLL in CPU?
What do "high sea" and "carry" mean in this sentence?
How can I open an app using Terminal?
How to make a variable always equal to the result of some calculations?
How do scammers retract money, while you can’t?
Increase performance creating Mandelbrot set in python
Horror movie/show or scene where a horse creature opens its mouth really wide and devours a man in a stables
How do I get the green key off the shelf in the Dobby level of Lego Harry Potter 2?
Does the Brexit deal have to be agreed by both Houses?
Trouble understanding the speech of overseas colleagues
Apart from "berlinern", do any other German dialects have a corresponding verb?
Term for the "extreme-extension" version of a straw man fallacy?
Does it take more energy to get to Venus or to Mars?
India just shot down a satellite from the ground. At what altitude range is the resulting debris field?
How do I construct this japanese bowl?
Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?
Opposite of a diet
What can we do to stop prior company from asking us questions?
Why did we only see the N-1 starfighters in one film?
What does this shorthand mean?
How to sort a character vector where elements contain letters and numbers in R?
The Next CEO of Stack OverflowOrder colnames by char + numberOrder data.frame after numbers within character column in ROrdering character values while preserving number orderSorting A Character Dataframe According to desired waysort strings by numbers inside themArrange column in data framehow to group rows without ordering?Sorting character vector containing semantic versionsHow to sort by Library of Congress Classification (LCC) number in RSort a dataframe based on a character column containing letters followed by numbers in RHow do I sort a list of dictionaries by a value of the dictionary?How do I sort a dictionary by value?How do I sort an NSMutableArray with custom objects in it?How to sort a dataframe by multiple column(s)?Java Array Sort descending?Sorting an array in descending order in RubyHow to Sort Multi-dimensional Array by Value?How to Sort a List<T> by a property in the objectGrouping functions (tapply, by, aggregate) and the *apply familySwift Beta performance: sorting arrays
I have a character array
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
I would like to sort it in descending order so that I will have an output like this:
V51
V108
V116
V120
V155
V217
V327
V440
V446
V457
V477
I have tried sort.list()
like this
cf[sort.list(cf)]
and got this answer:
[1] "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477" "V51"
and also tried order()
and got same result.
Can someone help me please
r sorting
add a comment |
I have a character array
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
I would like to sort it in descending order so that I will have an output like this:
V51
V108
V116
V120
V155
V217
V327
V440
V446
V457
V477
I have tried sort.list()
like this
cf[sort.list(cf)]
and got this answer:
[1] "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477" "V51"
and also tried order()
and got same result.
Can someone help me please
r sorting
add a comment |
I have a character array
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
I would like to sort it in descending order so that I will have an output like this:
V51
V108
V116
V120
V155
V217
V327
V440
V446
V457
V477
I have tried sort.list()
like this
cf[sort.list(cf)]
and got this answer:
[1] "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477" "V51"
and also tried order()
and got same result.
Can someone help me please
r sorting
I have a character array
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
I would like to sort it in descending order so that I will have an output like this:
V51
V108
V116
V120
V155
V217
V327
V440
V446
V457
V477
I have tried sort.list()
like this
cf[sort.list(cf)]
and got this answer:
[1] "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477" "V51"
and also tried order()
and got same result.
Can someone help me please
r sorting
r sorting
edited Jan 25 at 7:47
Old Pro
15.3k23970
15.3k23970
asked Jul 8 '13 at 16:19
rinzy kutexrinzy kutex
5421512
5421512
add a comment |
add a comment |
6 Answers
6
active
oldest
votes
Try mixedsort
from the "gtools" package:
> # install.packages("gtools") ## Uncomment if not already installed
> library(gtools)
> mixedsort(cf)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
If you don't want to use mixedsort
(not sure why one wouldn't), and if your vector has a pretty consistent pattern (eg letters followed by numbers), you can also probably try something like this. (Note: Relatively untested.)
newvec <- c("V440", "V457", "V116", "V327", "V446", "V108", "V155",
"V217", "V120", "V51", "V477", "B22", "A10", "Z01")
newvec[order(gsub("([A-Z]+)([0-9]+)", "\1", newvec),
as.numeric(gsub("([A-Z]+)([0-9]+)", "\2", newvec)))]
# [1] "A10" "B22" "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440"
# [11] "V446" "V457" "V477" "Z01"
add a comment |
Plenty of right answers here, this is another way, just for fun.
cf[order(nchar(cf), cf)]
# [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
add a comment |
One more solution in a line of code using str_sort
function (from the stringr
packg.)
# install.packages("stringr") ## Uncomment if not already installed
library(stringr)
str_sort(cf, numeric = TRUE)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
1
this is great..
– FMM
Oct 1 '18 at 17:21
thanks! happy to help.
– JDie
Nov 15 '18 at 18:26
add a comment |
Just scrape off the preceding "V" character to build a sorting vector. No additional fancy tools required.
vals <- as.numeric(gsub("V","", cf))
cf[order(vals)]
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446"
[10] "V457" "V477"
add a comment |
R correctly orders strings alphabetically, that is why you get that result.
Aside from @Ananda very good answer, if you want to use base R you can use strsplit
to remove the "V" from each string and then use as.numeric
to cast the strings to integers:
vals <- as.numeric(sapply(cf, FUN=function(x)strsplit(x, "V")[[1]][2]))
Now you can sort your strings using vals
cf[order(vals)]
add a comment |
Here's a base approach utilizing names
and sort
(Ananda's was pretty slick):
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
cf2 <- as.numeric(gsub("[^[:digit:]]", "", cf))
names(cf2) <- seq_along(cf2)
cf[as.numeric(names(sort(cf2)))]
## > cf[as.numeric(names(sort(cf2)))]
## [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327"
## [8] "V440" "V446" "V457" "V477"
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17531403%2fhow-to-sort-a-character-vector-where-elements-contain-letters-and-numbers-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try mixedsort
from the "gtools" package:
> # install.packages("gtools") ## Uncomment if not already installed
> library(gtools)
> mixedsort(cf)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
If you don't want to use mixedsort
(not sure why one wouldn't), and if your vector has a pretty consistent pattern (eg letters followed by numbers), you can also probably try something like this. (Note: Relatively untested.)
newvec <- c("V440", "V457", "V116", "V327", "V446", "V108", "V155",
"V217", "V120", "V51", "V477", "B22", "A10", "Z01")
newvec[order(gsub("([A-Z]+)([0-9]+)", "\1", newvec),
as.numeric(gsub("([A-Z]+)([0-9]+)", "\2", newvec)))]
# [1] "A10" "B22" "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440"
# [11] "V446" "V457" "V477" "Z01"
add a comment |
Try mixedsort
from the "gtools" package:
> # install.packages("gtools") ## Uncomment if not already installed
> library(gtools)
> mixedsort(cf)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
If you don't want to use mixedsort
(not sure why one wouldn't), and if your vector has a pretty consistent pattern (eg letters followed by numbers), you can also probably try something like this. (Note: Relatively untested.)
newvec <- c("V440", "V457", "V116", "V327", "V446", "V108", "V155",
"V217", "V120", "V51", "V477", "B22", "A10", "Z01")
newvec[order(gsub("([A-Z]+)([0-9]+)", "\1", newvec),
as.numeric(gsub("([A-Z]+)([0-9]+)", "\2", newvec)))]
# [1] "A10" "B22" "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440"
# [11] "V446" "V457" "V477" "Z01"
add a comment |
Try mixedsort
from the "gtools" package:
> # install.packages("gtools") ## Uncomment if not already installed
> library(gtools)
> mixedsort(cf)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
If you don't want to use mixedsort
(not sure why one wouldn't), and if your vector has a pretty consistent pattern (eg letters followed by numbers), you can also probably try something like this. (Note: Relatively untested.)
newvec <- c("V440", "V457", "V116", "V327", "V446", "V108", "V155",
"V217", "V120", "V51", "V477", "B22", "A10", "Z01")
newvec[order(gsub("([A-Z]+)([0-9]+)", "\1", newvec),
as.numeric(gsub("([A-Z]+)([0-9]+)", "\2", newvec)))]
# [1] "A10" "B22" "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440"
# [11] "V446" "V457" "V477" "Z01"
Try mixedsort
from the "gtools" package:
> # install.packages("gtools") ## Uncomment if not already installed
> library(gtools)
> mixedsort(cf)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
If you don't want to use mixedsort
(not sure why one wouldn't), and if your vector has a pretty consistent pattern (eg letters followed by numbers), you can also probably try something like this. (Note: Relatively untested.)
newvec <- c("V440", "V457", "V116", "V327", "V446", "V108", "V155",
"V217", "V120", "V51", "V477", "B22", "A10", "Z01")
newvec[order(gsub("([A-Z]+)([0-9]+)", "\1", newvec),
as.numeric(gsub("([A-Z]+)([0-9]+)", "\2", newvec)))]
# [1] "A10" "B22" "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440"
# [11] "V446" "V457" "V477" "Z01"
edited Jul 8 '13 at 16:42
answered Jul 8 '13 at 16:21
A5C1D2H2I1M1N2O1R2T1A5C1D2H2I1M1N2O1R2T1
154k19291384
154k19291384
add a comment |
add a comment |
Plenty of right answers here, this is another way, just for fun.
cf[order(nchar(cf), cf)]
# [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
add a comment |
Plenty of right answers here, this is another way, just for fun.
cf[order(nchar(cf), cf)]
# [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
add a comment |
Plenty of right answers here, this is another way, just for fun.
cf[order(nchar(cf), cf)]
# [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
Plenty of right answers here, this is another way, just for fun.
cf[order(nchar(cf), cf)]
# [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
answered Jul 8 '13 at 16:35
Matthew PlourdeMatthew Plourde
35.5k469101
35.5k469101
add a comment |
add a comment |
One more solution in a line of code using str_sort
function (from the stringr
packg.)
# install.packages("stringr") ## Uncomment if not already installed
library(stringr)
str_sort(cf, numeric = TRUE)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
1
this is great..
– FMM
Oct 1 '18 at 17:21
thanks! happy to help.
– JDie
Nov 15 '18 at 18:26
add a comment |
One more solution in a line of code using str_sort
function (from the stringr
packg.)
# install.packages("stringr") ## Uncomment if not already installed
library(stringr)
str_sort(cf, numeric = TRUE)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
1
this is great..
– FMM
Oct 1 '18 at 17:21
thanks! happy to help.
– JDie
Nov 15 '18 at 18:26
add a comment |
One more solution in a line of code using str_sort
function (from the stringr
packg.)
# install.packages("stringr") ## Uncomment if not already installed
library(stringr)
str_sort(cf, numeric = TRUE)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
One more solution in a line of code using str_sort
function (from the stringr
packg.)
# install.packages("stringr") ## Uncomment if not already installed
library(stringr)
str_sort(cf, numeric = TRUE)
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446" "V457" "V477"
answered Jul 29 '18 at 9:48
JDieJDie
9612
9612
1
this is great..
– FMM
Oct 1 '18 at 17:21
thanks! happy to help.
– JDie
Nov 15 '18 at 18:26
add a comment |
1
this is great..
– FMM
Oct 1 '18 at 17:21
thanks! happy to help.
– JDie
Nov 15 '18 at 18:26
1
1
this is great..
– FMM
Oct 1 '18 at 17:21
this is great..
– FMM
Oct 1 '18 at 17:21
thanks! happy to help.
– JDie
Nov 15 '18 at 18:26
thanks! happy to help.
– JDie
Nov 15 '18 at 18:26
add a comment |
Just scrape off the preceding "V" character to build a sorting vector. No additional fancy tools required.
vals <- as.numeric(gsub("V","", cf))
cf[order(vals)]
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446"
[10] "V457" "V477"
add a comment |
Just scrape off the preceding "V" character to build a sorting vector. No additional fancy tools required.
vals <- as.numeric(gsub("V","", cf))
cf[order(vals)]
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446"
[10] "V457" "V477"
add a comment |
Just scrape off the preceding "V" character to build a sorting vector. No additional fancy tools required.
vals <- as.numeric(gsub("V","", cf))
cf[order(vals)]
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446"
[10] "V457" "V477"
Just scrape off the preceding "V" character to build a sorting vector. No additional fancy tools required.
vals <- as.numeric(gsub("V","", cf))
cf[order(vals)]
[1] "V51" "V108" "V116" "V120" "V155" "V217" "V327" "V440" "V446"
[10] "V457" "V477"
answered Jul 8 '13 at 16:24
David MarxDavid Marx
5,15012449
5,15012449
add a comment |
add a comment |
R correctly orders strings alphabetically, that is why you get that result.
Aside from @Ananda very good answer, if you want to use base R you can use strsplit
to remove the "V" from each string and then use as.numeric
to cast the strings to integers:
vals <- as.numeric(sapply(cf, FUN=function(x)strsplit(x, "V")[[1]][2]))
Now you can sort your strings using vals
cf[order(vals)]
add a comment |
R correctly orders strings alphabetically, that is why you get that result.
Aside from @Ananda very good answer, if you want to use base R you can use strsplit
to remove the "V" from each string and then use as.numeric
to cast the strings to integers:
vals <- as.numeric(sapply(cf, FUN=function(x)strsplit(x, "V")[[1]][2]))
Now you can sort your strings using vals
cf[order(vals)]
add a comment |
R correctly orders strings alphabetically, that is why you get that result.
Aside from @Ananda very good answer, if you want to use base R you can use strsplit
to remove the "V" from each string and then use as.numeric
to cast the strings to integers:
vals <- as.numeric(sapply(cf, FUN=function(x)strsplit(x, "V")[[1]][2]))
Now you can sort your strings using vals
cf[order(vals)]
R correctly orders strings alphabetically, that is why you get that result.
Aside from @Ananda very good answer, if you want to use base R you can use strsplit
to remove the "V" from each string and then use as.numeric
to cast the strings to integers:
vals <- as.numeric(sapply(cf, FUN=function(x)strsplit(x, "V")[[1]][2]))
Now you can sort your strings using vals
cf[order(vals)]
answered Jul 8 '13 at 16:25
niconico
39.9k1269102
39.9k1269102
add a comment |
add a comment |
Here's a base approach utilizing names
and sort
(Ananda's was pretty slick):
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
cf2 <- as.numeric(gsub("[^[:digit:]]", "", cf))
names(cf2) <- seq_along(cf2)
cf[as.numeric(names(sort(cf2)))]
## > cf[as.numeric(names(sort(cf2)))]
## [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327"
## [8] "V440" "V446" "V457" "V477"
add a comment |
Here's a base approach utilizing names
and sort
(Ananda's was pretty slick):
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
cf2 <- as.numeric(gsub("[^[:digit:]]", "", cf))
names(cf2) <- seq_along(cf2)
cf[as.numeric(names(sort(cf2)))]
## > cf[as.numeric(names(sort(cf2)))]
## [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327"
## [8] "V440" "V446" "V457" "V477"
add a comment |
Here's a base approach utilizing names
and sort
(Ananda's was pretty slick):
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
cf2 <- as.numeric(gsub("[^[:digit:]]", "", cf))
names(cf2) <- seq_along(cf2)
cf[as.numeric(names(sort(cf2)))]
## > cf[as.numeric(names(sort(cf2)))]
## [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327"
## [8] "V440" "V446" "V457" "V477"
Here's a base approach utilizing names
and sort
(Ananda's was pretty slick):
cf <- c("V440","V457","V116","V327","V446","V108",
"V155","V217","V120","V51","V477")
cf2 <- as.numeric(gsub("[^[:digit:]]", "", cf))
names(cf2) <- seq_along(cf2)
cf[as.numeric(names(sort(cf2)))]
## > cf[as.numeric(names(sort(cf2)))]
## [1] "V51" "V108" "V116" "V120" "V155" "V217" "V327"
## [8] "V440" "V446" "V457" "V477"
answered Jul 8 '13 at 16:29
Tyler RinkerTyler Rinker
65.2k42234412
65.2k42234412
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17531403%2fhow-to-sort-a-character-vector-where-elements-contain-letters-and-numbers-in-r%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown