0

Let say that I have 10 objects in R environment named a.1, a.2, ..., a.10

for(i in 1:10) assign(paste("a", i, sep="."), iris)
rm(i)

If I were to call ls(), it would return as:

 "a.1"  "a.10" "a.2"  "a.3"  "a.4"  "a.5"  "a.6"  "a.7"  "a.8"  "a.9" 

instead of

"a.1"  "a.2"  "a.3"  "a.4"  "a.5"  "a.6"  "a.7"  "a.8"  "a.9" "a.10"

May I know if there's any method to force ls() to return in the latter sequence, instead of the former?

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • look at `?mixedsort()` from `gtools` package. – mtoto Jun 15 '16 at 08:42
  • Nope. `mixedsort` doesn't work... It will work if the character and numerals are not separated by `[.]` – Adam Quek Jun 15 '16 at 08:51
  • Your actual problem is that you "have 10 objects in R environment named a.1, a.2, ..., a.10". Those probably should be in a list. Then this would be a non-issue. – Roland Jun 15 '16 at 08:53
  • @Roland Thanks and I completely agree with your point. However, I did made a mistake of creating the bizarrely named objects in my answer at http://stackoverflow.com/questions/37802597/how-to-combine-vectors-in-r/37803178?noredirect=1#comment63119668_37803178 Was just wondering if there's a way to call for natural order... – Adam Quek Jun 15 '16 at 09:07
  • `x <- ls(); x[order(as.numeric(gsub("[^[:digit:]]*", "", x)))]` – Roland Jun 15 '16 at 09:16
  • And please don't write answers promoting bad practices. – Roland Jun 15 '16 at 09:18
  • Thanks for the help. And yes, I'd screwed up big time... – Adam Quek Jun 15 '16 at 09:21

0 Answers0