R:`rbind`,列表示绑定

时间:2016-03-29 07:08:35

标签: r dataframe data-manipulation rbind

我希望rbind两个数据框,在结果data.frame中有一个额外的列,表示哪一行来自data.frame。例如,

top <- mtcars[1:16, ]
bottom <- mtcars[17:32, ]

rbind(top, bottom)

这会给我,

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
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
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

但我需要,

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb which.df
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4      top
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4      top
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1      top
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1      top
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2      top
Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1      top
Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4      top
Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2      top
Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2      top
Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4      top
Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4      top
Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3      top
Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3      top
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3      top
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4      top
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4      top
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4   bottom
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1   bottom
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2   bottom
Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1   bottom
Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1   bottom
Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2   bottom
AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2   bottom
Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4   bottom
Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2   bottom
Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1   bottom
Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2   bottom
Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2   bottom
Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4   bottom
Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6   bottom
Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8   bottom
Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2   bottom

是否有任何现有的功能可以执行此操作,我有很多data.frame,重复计算行号的文本会很麻烦。

5 个答案:

答案 0 :(得分:5)

您可以为此编写一个简单的函数:

top <- mtcars[1:16, ]
bottom <- mtcars[17:32, ]

myBind <- function(df1, df2) {
    df1$which.df <- all.names(match.call())[2]
    df2$which.df <- all.names(match.call())[3]
    rbind(df1, df2)
}

result <- myBind(top, bottom)

结果如下:

result[14:19,]

                     mpg cyl  disp  hp drat    wt  qsec vs am gear carb which.df
Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3      top
Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4      top
Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4      top
Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4   bottom
Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1   bottom
Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2   bottom

为了容纳2个以上的数据帧,您可以使用...而不是df1, df2并迭代函数内的所有参数来设置which.df的值。

答案 1 :(得分:4)

我们可以使用Map

res <- do.call(rbind,Map(cbind, list(top, bottom),
                   which.df = c("top", "bottom")))
head(res)
#                   mpg cyl disp  hp drat    wt  qsec vs am gear carb which.df
#Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4      top
#Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4      top
#Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1      top
#Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1      top
#Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2      top
#Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1      top

答案 2 :(得分:2)

dfstack <- function(
    ...,
    indname='which.df',
    indfunc=function(...) {
        l <- list(...);
        nms <- names(l);
        nms <- (if (is.null(nms)) NA_character_ else nms)[seq_along(l)];
        ifelse(is.na(nms) | nms=='',as.character(substitute(list(...))[-1L]),nms);
    }
) do.call(rbind,do.call(Map,c(cbind,list(unname(list(...))),setNames(list(indfunc(...)),indname))));

特点:

  • 将data.frame操作数作为前导可变参数,因此添加新操作数就像在函数调用中再添加一个参数一样简单。
  • 将指标列名默认为'which.df',但可以使用indname参数覆盖。
  • 通过indfunc() lambda参数参数化用于选择指标值的逻辑。
  • 默认indfunc()会在可用和非空时自动更喜欢命名的可变参数的名称,否则会对参数解析树进行字符串化并使用它。

演示:

dfstack();
## NULL
dfstack(top);
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb which.df
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4      top
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4      top
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1      top
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1      top
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2      top
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1      top
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4      top
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2      top
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2      top
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4      top
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4      top
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3      top
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3      top
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3      top
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4      top
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4      top
dfstack(top,bottom);
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb which.df
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4      top
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4      top
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1      top
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1      top
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2      top
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1      top
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4      top
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2      top
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2      top
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4      top
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4      top
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3      top
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3      top
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3      top
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4      top
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4      top
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4   bottom
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1   bottom
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2   bottom
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1   bottom
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1   bottom
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2   bottom
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2   bottom
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4   bottom
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2   bottom
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1   bottom
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2   bottom
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2   bottom
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4   bottom
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6   bottom
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8   bottom
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2   bottom

这里有一个演示,说明如何有选择地指定命名的变量参数来控制指标值,以及如何为未命名的参数对参数解析树进行字符串化:

dfstack(top+2*top,BOTTOM=bottom);
##                      mpg cyl   disp  hp  drat     wt  qsec vs am gear carb      which.df
## Mazda RX4           63.0  18  480.0 330 11.70  7.860 49.38  0  3   12   12 top + 2 * top
## Mazda RX4 Wag       63.0  18  480.0 330 11.70  8.625 51.06  0  3   12   12 top + 2 * top
## Datsun 710          68.4  12  324.0 279 11.55  6.960 55.83  3  3   12    3 top + 2 * top
## Hornet 4 Drive      64.2  18  774.0 330  9.24  9.645 58.32  3  0    9    3 top + 2 * top
## Hornet Sportabout   56.1  24 1080.0 525  9.45 10.320 51.06  0  0    9    6 top + 2 * top
## Valiant             54.3  18  675.0 315  8.28 10.380 60.66  3  0    9    3 top + 2 * top
## Duster 360          42.9  24 1080.0 735  9.63 10.710 47.52  0  0    9   12 top + 2 * top
## Merc 240D           73.2  12  440.1 186 11.07  9.570 60.00  3  0   12    6 top + 2 * top
## Merc 230            68.4  12  422.4 285 11.76  9.450 68.70  3  0   12    6 top + 2 * top
## Merc 280            57.6  18  502.8 369 11.76 10.320 54.90  3  0   12   12 top + 2 * top
## Merc 280C           53.4  18  502.8 369 11.76 10.320 56.70  3  0   12   12 top + 2 * top
## Merc 450SE          49.2  24  827.4 540  9.21 12.210 52.20  0  0    9    9 top + 2 * top
## Merc 450SL          51.9  24  827.4 540  9.21 11.190 52.80  0  0    9    9 top + 2 * top
## Merc 450SLC         45.6  24  827.4 540  9.21 11.340 54.00  0  0    9    9 top + 2 * top
## Cadillac Fleetwood  31.2  24 1416.0 615  8.79 15.750 53.94  0  0    9   12 top + 2 * top
## Lincoln Continental 31.2  24 1380.0 645  9.00 16.272 53.46  0  0    9   12 top + 2 * top
## Chrysler Imperial   14.7   8  440.0 230  3.23  5.345 17.42  0  0    3    4        BOTTOM
## Fiat 128            32.4   4   78.7  66  4.08  2.200 19.47  1  1    4    1        BOTTOM
## Honda Civic         30.4   4   75.7  52  4.93  1.615 18.52  1  1    4    2        BOTTOM
## Toyota Corolla      33.9   4   71.1  65  4.22  1.835 19.90  1  1    4    1        BOTTOM
## Toyota Corona       21.5   4  120.1  97  3.70  2.465 20.01  1  0    3    1        BOTTOM
## Dodge Challenger    15.5   8  318.0 150  2.76  3.520 16.87  0  0    3    2        BOTTOM
## AMC Javelin         15.2   8  304.0 150  3.15  3.435 17.30  0  0    3    2        BOTTOM
## Camaro Z28          13.3   8  350.0 245  3.73  3.840 15.41  0  0    3    4        BOTTOM
## Pontiac Firebird    19.2   8  400.0 175  3.08  3.845 17.05  0  0    3    2        BOTTOM
## Fiat X1-9           27.3   4   79.0  66  4.08  1.935 18.90  1  1    4    1        BOTTOM
## Porsche 914-2       26.0   4  120.3  91  4.43  2.140 16.70  0  1    5    2        BOTTOM
## Lotus Europa        30.4   4   95.1 113  3.77  1.513 16.90  1  1    5    2        BOTTOM
## Ford Pantera L      15.8   8  351.0 264  4.22  3.170 14.50  0  1    5    4        BOTTOM
## Ferrari Dino        19.7   6  145.0 175  3.62  2.770 15.50  0  1    5    6        BOTTOM
## Maserati Bora       15.0   8  301.0 335  3.54  3.570 14.60  0  1    5    8        BOTTOM
## Volvo 142E          21.4   4  121.0 109  4.11  2.780 18.60  1  1    4    2        BOTTOM

以下是如何使用indnameindfunc参数自定义结果的演示:

dfstack(top,bottom,indname='ind',indfunc=function(...) paste0('[',as.character(substitute(list(...))[-1L]),']'));
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb      ind
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4    [top]
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4    [top]
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1    [top]
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1    [top]
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2    [top]
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1    [top]
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4    [top]
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2    [top]
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2    [top]
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4    [top]
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4    [top]
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3    [top]
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3    [top]
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3    [top]
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4    [top]
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4    [top]
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4 [bottom]
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1 [bottom]
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2 [bottom]
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1 [bottom]
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1 [bottom]
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2 [bottom]
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2 [bottom]
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4 [bottom]
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2 [bottom]
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1 [bottom]
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2 [bottom]
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2 [bottom]
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4 [bottom]
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6 [bottom]
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8 [bottom]
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2 [bottom]
dfstack(top,bottom,indfunc=function(...) seq_along(list(...)));
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb which.df
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4        1
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4        1
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1        1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1        1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2        1
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1        1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4        1
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2        1
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2        1
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4        1
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4        1
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3        1
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3        1
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3        1
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4        1
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4        1
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4        2
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1        2
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2        2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1        2
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1        2
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2        2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2        2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4        2
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2        2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1        2
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2        2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2        2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4        2
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6        2
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8        2
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2        2

答案 3 :(得分:1)

在使用rbind之前,可以在顶部和底部添加列。

which.df.top<-rep("top",dim(top)[1])
top[which.df]<-which.df.top
which.df.bot<-rep("botom",dim(bottom[1])
bottom[which.df]<-which.df.bot

答案 4 :(得分:1)

我也在尝试解决这个问题,并提出了这个解决方案,

stackDf <- function(..., add.to.col = TRUE){
    lst <- list(...)
    if(add.to.col) {
            nms <- sapply(substitute(list(...))[-1], deparse)
            lst <- lapply(seq_along(lst), 
                    function(x) cbind(lst[[x]], names = nms[x]))    
    }
    do.call(rbind, lst)
}

> head(stackDf(top, bottom))
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb names
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4   top
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4   top
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1   top
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1   top
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2   top
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1   top


> head(stackDf(top, bottom, add.to.col=FALSE))
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

我从Gavin Simpson

获得了一些帮助