I have to join 60 dataframes named: coord1, coord2, coord3, ... , coord60, in a numerical order, that is: I need to create a new_df with coord1 on the top and coord60 on the bottom.
To do this I do the following:
new_df <- do.call("rbind",mget(ls(pattern(pattern = "^coord"))))
the problem is that it joins the data frames but instead of doing it by the logical order of the numbers from 1 to 60 it does it in the following order:
coord1, coord10, coord11, coord12..., coord19, coord2,coord20,....
how could I solve this and have it order them coord1, coord2, coord3,.... coord60?
Thank you very much
CodePudding user response:
coord1 <- coord2 <- coord11 <- mtcars[1:2,1:3]
alldat <- mget(ls(pattern="^coord"))
alldat
# $coord1
# mpg cyl disp
# Mazda RX4 21 6 160
# Mazda RX4 Wag 21 6 160
# $coord11
# mpg cyl disp
# Mazda RX4 21 6 160
# Mazda RX4 Wag 21 6 160
# $coord2
# mpg cyl disp
# Mazda RX4 21 6 160
# Mazda RX4 Wag 21 6 160
alldat <- alldat[ order(as.integer(gsub("\\D", "", names(alldat)))) ]
alldat
# $coord1
# mpg cyl disp
# Mazda RX4 21 6 160
# Mazda RX4 Wag 21 6 160
# $coord2
# mpg cyl disp
# Mazda RX4 21 6 160
# Mazda RX4 Wag 21 6 160
# $coord11
# mpg cyl disp
# Mazda RX4 21 6 160
# Mazda RX4 Wag 21 6 160
do.call(rbind, alldat)
# mpg cyl disp
# coord1.Mazda RX4 21 6 160
# coord1.Mazda RX4 Wag 21 6 160
# coord2.Mazda RX4 21 6 160
# coord2.Mazda RX4 Wag 21 6 160
# coord11.Mazda RX4 21 6 160
# coord11.Mazda RX4 Wag 21 6 160