1

When splitting a dataframe and reassemble again with do.call(rbind, x) the rownames become messy. It's less a problem with the prefixed numbers, because they are easy to handle with a regex, but list objects with just one row completely loose the previous row name.

Example

l1 <- split(mtcars, mtcars$disp)
# ... doing some stuff with l1
d <- do.call(rbind, l1)
d[10:15, ]
> d[10:15, ]
                   mpg cyl  disp  hp drat    wt  qsec vs am gear carb
140.8             22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
145               19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
146.7             24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
160.Mazda RX4     21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
160.Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
167.6.Merc 280    19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4

Expected output: In this example exactly similar to mtcars with original rownames, at least with no missing car names as in the issue above.

Might there be a remedy?

Community
  • 1
  • 1
jay.sf
  • 46,523
  • 6
  • 46
  • 87

1 Answers1

2

You can use unname for this:

> d <- do.call(rbind, unname(l1))
> d[10:15, ]
               mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Merc 230      22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Ferrari Dino  19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Merc 240D     24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Mazda RX4     21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Merc 280      19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
jay.sf
  • 46,523
  • 6
  • 46
  • 87
Sonny
  • 3,023
  • 1
  • 9
  • 19