R:提高复杂循环的性能

时间:2018-07-12 02:46:56

标签: r performance loops data.table

我在数据框df$moves中有一列看起来像这样:

[1] "W1.d4 B1.d5 W2.c4 B2.e6 W3.Nc3 B3.Nf6 W4.cxd5 B4.exd5 W5.Bg5 B5.Be7 W6.e3 B6.Ne4 W7.Bxe7 B7.Nxc3 W8.Bxd8 B8.Nxd1 W9.Bxc7 B9.Nxb2 W10.Rb1 B10.Nc4 W11.Bxc4 B11.dxc4 W12.Ne2 B12.O-O W13.Nc3 B13.b6 W14.d5 B14.Na6 W15.Bd6 B15.Rd8 W16.Ba3 B16.Bb7 W17.e4 B17.f6 W18.Ke2 B18.Nc7 W19.Rhd1 B19.Ba6 W20.Ke3 B20.Kf7 W21.g4 B21.g5 W22.h4 B22.h6 W23.Rh1 B23.Re8 W24.f3 B24.Bb7 W25.hxg5 B25.fxg5 W26.d6 B26.Nd5+ W27.Nxd5 B27.Bxd5 W28.Rxh6 B28.c3 W29.d7 B29.Re6 W30.Rh7+ B30.Kg8 W31.Rbh1 B31.Bc6 W32.Rh8+ B32.Kf7 W33.Rxa8 B33.Bxd7 W34.Rh7+ "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
[2] "W1.e4 B1.d5 W2.exd5 B2.Qxd5 W3.Nc3 B3.Qa5 W4.d4 B4.Nf6 W5.Nf3 B5.c6 W6.Ne5 B6.Bf5 W7.g4 B7.Be4 W8.f3 B8.Bd5 W9.a3 B9.Nbd7 W10.Be3 B10.Nxe5 W11.dxe5 B11.Nxg4 W12.Bd4 B12.e6 W13.b4 B13.Qd8 W14.Nxd5 B14.Qxd5 W15.c4 B15.Ne3 W16.cxd5 B16.Nxd1 W17.dxc6 B17.bxc6 W18.Rxd1 B18.Be7 W19.Ba6 B19.O-O W20.Ke2 B20.Rab8 W21.Rc1 B21.Rfd8 W22.Rhd1 B22.c5 W23.Bxc5 B23.Rxd1 W24.Rxd1 B24.Bxc5 W25.bxc5 B25.g6 W26.c6 B26.Rb2+ W27.Rd2 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
[3] "W1.e4 B1.e5 W2.Nf3 B2.Nc6 W3.Bc4 B3.Bc5 W4.c3 B4.Nf6 W5.d3 B5.d6 W6.Bb3 B6.O-O W7.Nbd2 B7.Be6 W8.O-O B8.Qd7 W9.Re1 B9.Rfe8 W10.Nf1 B10.Ne7 W11.Ng3 B11.Bg4 W12.h3 B12.Be6 W13.Bg5 B13.Kh8 W14.Bxf6 B14.gxf6 W15.d4 B15.exd4 W16.cxd4 B16.Bb4 W17.Re3 B17.Rg8 W18.d5 B18.Bxh3 W19.Qd4 B19.Rg6 W20.Qxb4 B20.c5 W21.Qc3 B21.Bg4 W22.Bc2 B22.Rh6 W23.Nh2 B23.b5 W24.b4 B24.Rc8 W25.Bd3 B25.c4 W26.Bc2 B26.Bh5 W27.Nxh5 B27.Rxh5 W28.Qxf6+ B28.Kg8 W29.Bd1 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
[4] "W1.e4 B1.d5 W2.exd5 B2.Qxd5 W3.Nc3 B3.Qa5 W4.d4 B4.e6 W5.Nf3 B5.c6 W6.Bd3 B6.Nf6 W7.O-O B7.Be7 W8.Re1 B8.Nbd7 W9.Ne5 B9.O-O W10.Bg5 B10.Qd8 W11.Qf3 B11.Re8 W12.Rad1 B12.Nf8 W13.Ne4 B13.Ng6 W14.h4 B14.Nxe5 W15.dxe5 B15.Nxe4 W16.Bxe4 B16.Qc7 W17.Bxe7 B17.Qxe7 W18.h5 B18.Bd7 W19.h6 B19.gxh6 W20.Qf4 B20.h5 W21.Qh6 B21.f5 W22.exf6 B22.Qf7 W23.Re3 B23.Kh8 W24.Rg3 B24.Rg8 W25.Rg7 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[5] "W1.e4 B1.e5 W2.Nf3 B2.Nc6 W3.Bb5 B3.a6 W4.Ba4 B4.Nf6 W5.O-O B5.Be7 W6.Re1 B6.b5 W7.Bb3 B7.d6 W8.c3 B8.O-O W9.h3 B9.Na5 W10.Bc2 B10.c5 W11.d4 B11.Qc7 W12.Nbd2 B12.Bd7 W13.Nf1 B13.cxd4 W14.cxd4 B14.Rac8 W15.Ne3 B15.Nc6 W16.d5 B16.Nb4 W17.Bb1 B17.a5 W18.a3 B18.Na6 W19.b4 B19.Ra8 W20.Bd2 B20.Rfc8 W21.Bd3 B21.Qb7 W22.g4 B22.g6 W23.Nf1 B23.axb4 W24.axb4 B24.Bd8 W25.Ng3 B25.Nc7 W26.Qe2 B26.Rxa1 W27.Rxa1 B27.Ra8 W28.Qe1 B28.Nfe8 W29.Qc1 B29.Ng7 W30.Rxa8 B30.Qxa8 W31.Bh6 B31.Nce8 W32.Qb2 B32.Qa4 W33.Kg2 B33.Bb6 W34.Bc2 B34.Qa7 W35.Bd3 B35.Qa4 W36.Ne2 B36.Nc7 W37.Nxe5 B37.dxe5 W38.Qxe5 B38.Nce8 W39.Bxg7 B39.Qd1 W40.Bh6 B40.Qxd3 W41.Qe7 B41.Ng7 W42.Ng3 B42.Qc2 W43.Qf6 B43.Nf5 W44.Qxb6 B44.Nh4+ W45.Kh2 B45.Nf3+ W46.Kg2 B46.Nh4+ W47.Kh2 B47.Nf3+ W48.Kg2 B48.Nh4+ W49.Kh2 "                                                                                                                                                                                                                                                                                                                                                                                                           
[6] "W1.d4 B1.e6 W2.Nf3 B2.Nf6 W3.c4 B3.d5 W4.Nc3 B4.dxc4 W5.e4 B5.Bb4 W6.Bg5 B6.c5 W7.Bxc4 B7.cxd4 W8.Nxd4 B8.Qa5 W9.Bd2 B9.O-O W10.Nc2 B10.Bxc3 W11.Bxc3 B11.Qg5 W12.Qe2 B12.Qxg2 W13.O-O-O B13.Qxe4 W14.Rhg1 B14.g6 W15.Ne3 B15.e5 W16.f4 B16.Be6 W17.Bd3 B17.Qxf4 W18.Rgf1 B18.Qh4 W19.Be1 B19.Qa4 W20.Rxf6 B20.Nc6 W21.Rxe6 B21.Nd4 W22.Qg4 B22.Qxa2 W23.Bxg6 B23.hxg6 W24.Rxg6+ B24.fxg6 W25.Qxg6+ B25.Kh8 W26.Qh5+ B26.Kg8 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
[7] "W1.e4 B1.c5 W2.Nf3 B2.Nc6 W3.Bb5 B3.g6 W4.Bxc6 B4.dxc6 W5.d3 B5.Bg7 W6.h3 B6.Nf6 W7.Nc3 B7.O-O W8.Be3 B8.Qa5 W9.Qd2 B9.Rd8 W10.O-O B10.Bd7 W11.Bh6 B11.Qc7 W12.Bxg7 B12.Kxg7 W13.Qe3 B13.b6 W14.Nh2 B14.Rf8 W15.f4 B15.Rad8 W16.Rae1 B16.Bc8 W17.f5 B17.e5 W18.Rf2 B18.Qd6 W19.Ref1 B19.h6 W20.b3 B20.Qd4 W21.Qe1 B21.b5 W22.Ne2 B22.Qd6 W23.Ng3 B23.c4 W24.dxc4 B24.bxc4 W25.Qa5 B25.cxb3 W26.axb3 B26.Rd7 W27.Nf3 B27.c5 W28.Qc3 B28.Re7 W29.Ra1 B29.Rfe8 W30.Ra5 B30.Rc7 W31.Qe3 B31.Qb6 W32.Ra4 B32.Bb7 W33.Qc3 B33.Nd7 W34.Ra1 B34.c4 W35.b4 B35.f6 W36.fxg6 B36.Kxg6 W37.Nh4+ B37.Kh7 W38.Qf3 B38.Qe6 W39.Rxa7 B39.Rg8 W40.Nhf5 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
[8] "W1.d4 B1.Nf6 W2.c4 B2.g6 W3.g3 B3.Bg7 W4.Bg2 B4.O-O W5.Nc3 B5.d6 W6.Nf3 B6.Nbd7 W7.O-O B7.e5 W8.e4 B8.exd4 W9.Nxd4 B9.Ne5 W10.h3 B10.Re8 W11.Re1 B11.Nfd7 W12.Be3 B12.Nf8 W13.b3 B13.Bd7 W14.Qc2 B14.Nc6 W15.Qd2 B15.a6 W16.Rad1 B16.Rc8 W17.Bg5 B17.Bf6 W18.Bxf6 B18.Qxf6 W19.Nde2 B19.Ne5 W20.Nd5 B20.Qg7 W21.Nef4 B21.Ne6 W22.Nxe6 B22.Bxe6 W23.f4 B23.Nc6 W24.Ne3 B24.Rcd8 W25.Qf2 B25.Bc8 W26.Nd5 B26.f5 W27.exf5 B27.Bxf5 W28.g4 B28.Bc8 W29.Rxe8+ B29.Rxe8 W30.g5 B30.Kh8 W31.Nf6 B31.Re6 W32.c5 B32.Ne7 W33.cxd6 B33.cxd6 W34.Qb6 B34.Ng8 W35.Qd8 B35.Rxf6 W36.gxf6 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
[9] "W1.c4 B1.e6 W2.g3 B2.d5 W3.Bg2 B3.Nf6 W4.Nf3 B4.Be7 W5.b3 B5.O-O W6.O-O B6.c5 W7.Bb2 B7.Nc6 W8.e3 B8.b6 W9.Nc3 B9.Bb7 W10.cxd5 B10.Nxd5 W11.Nxd5 B11.Qxd5 W12.d4 B12.Rad8 W13.Ne5 B13.Qd6 W14.dxc5 B14.Qxc5 W15.Qe2 B15.Nxe5 W16.Bxb7 B16.Qc7 W17.Bg2 B17.Bc5 W18.Rfd1 B18.a5 W19.Rxd8 B19.Rxd8 W20.Rd1 B20.Rxd1+ W21.Qxd1 B21.Qd7 W22.Qxd7 B22.Nxd7 W23.Bc6 B23.Nf6 W24.Kg2 B24.Kf8 W25.Kf3 B25.Ke7 W26.g4 B26.h6 W27.h4 B27.g6 W28.Bb5 B28.h5 W29.g5 B29.Nd5 W30.Ke2 B30.Nc7 W31.Bd3 B31.Nd5 W32.Bc4 B32.Kd6 W33.a3 B33.Ne7 W34.e4 B34.Nc6 W35.f4 B35.Nd4+ W36.Bxd4 B36.Bxd4 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
[10] "W1.e4 B1.c5 W2.Nf3 B2.d6 W3.d4 B3.cxd4 W4.Nxd4 B4.Nf6 W5.Nc3 B5.a6 W6.Be3 B6.e6 W7.f3 B7.b5 W8.g4 B8.h6 W9.Qd2 B9.Nbd7 W10.O-O-O B10.Bb7 W11.h4 B11.b4 W12.Na4 B12.d5 W13.Bh3 B13.g5 W14.Bg2 B14.gxh4 W15.Rxh4 B15.dxe4 W16.g5 B16.Nd5 W17.Rxe4 B17.hxg5 W18.Bxg5 B18.Qa5 W19.f4 B19.Rh2 W20.Nxe6 B20.fxe6 W21.Rxe6+ B21.Kf7 W22.Qd3 B22.Bg7 W23.Qf5+ B23.Kg8 W24.Rxd5 B24.Qxa4 W25.Re7 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[11] "W1.e4 B1.c5 W2.Nf3 B2.Nc6 W3.d4 B3.cxd4 W4.Nxd4 B4.Nf6 W5.Nc3 B5.e5 W6.Ndb5 B6.d6 W7.Bg5 B7.a6 W8.Na3 B8.b5 W9.Bxf6 B9.gxf6 W10.Nd5 B10.f5 W11.c3 B11.Bg7 W12.exf5 B12.Bxf5 W13.Nc2 B13.Be6 W14.a4 B14.O-O W15.axb5 B15.axb5 W16.Rxa8 B16.Qxa8 W17.Nce3 B17.Qb7 W18.g4 B18.Ne7 W19.Bg2 B19.Nxd5 W20.Bxd5 B20.Qe7 W21.h4 B21.Rc8 W22.g5 B22.Rc5 W23.Be4 B23.d5 W24.Nxd5 B24.Bxd5 W25.Bxd5 B25.e4 W26.Ke2 B26.Qe5 W27.Bb3 B27.Qc7 W28.Qg1 B28.b4 W29.g6 B29.hxg6 W30.Qxg6 B30.Kf8 W31.Qg3 B31.Qxg3 W32.fxg3 B32.bxc3 W33.bxc3 B33.Rxc3 W34.Bd5 B34.Rxg3 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
[12] "W1.d4 B1.d5 W2.c4 B2.e6 W3.Nc3 B3.Nf6 W4.cxd5 B4.exd5 W5.Bg5 B5.c6 W6.Qc2 B6.Be7 W7.e3 B7.Nbd7 W8.Bd3 B8.O-O W9.Nge2 B9.Re8 W10.O-O B10.Nf8 W11.f3 B11.Ng6 W12.Rad1 B12.h6 W13.Bxf6 B13.Bxf6 W14.Bxg6 B14.fxg6 W15.e4 B15.g5 W16.e5 B16.Be7 W17.f4 B17.gxf4 W18.Nxf4 B18.Rf8 W19.Ng6 B19.Rxf1+ W20.Rxf1 B20.Be6 W21.Ne2 B21.Qd7 W22.h4 B22.Re8 W23.Ng3 B23.Bf7 W24.Nxe7+ B24.Rxe7 W25.Nf5 B25.Re6 W26.Nd6 B26.Bg6 W27.Qc3 B27.Rxd6 W28.exd6 B28.Qxd6 W29.Qa3 B29.Qb8 W30.Qe7 B30.Qg3 W31.Rf3 B31.Qg4 W32.Qf8+ B32.Kh7 W33.Qf4 B33.Qd7 W34.Rg3 B34.Be4 W35.Qe5 B35.Qf7 W36.h5 B36.Qd7 W37.b4 B37.a6 W38.a4 B38.Kg8 W39.a5 B39.Kh7 W40.Kf2 B40.Kg8 W41.Qb8+ B41.Kh7 W42.Qf8 B42.Bc2 W43.Qf4 B43.Be4 W44.Qe5 B44.Kg8 W45.Ke3 B45.Kh7 W46.Ke2 B46.Kg8 W47.Qb8+ B47.Kh7 W48.Qf8 B48.Bc2 W49.Kd2 B49.Be4 W50.Kc1 B50.Qc7 W51.Rg4 B51.Qd7 W52.Rf4 B52.Bxg2 W53.Qf5+ B53.Qxf5 W54.Rxf5 B54.Kg8 W55.Kd2 B55.Bh3 W56.Rf4 B56.Be6 W57.Ke3 B57.Bf7 W58.Rf5 B58.Kf8 W59.Re5 B59.Be8 W60.Kf4 B60.Bf7 W61.Kg4 B61.Be8 W62.Kg3 B62.Bf7 W63.Kf4 B63.Be8 W64.Kf5 B64.Bxh5 W65.Ke6 B65.Bg6 W66.Re3 B66.Bc2 W67.Rf3+ B67.Kg8 W68.b5 B68.cxb5 W69.Kxd5 B69.b4 W70.Kc4 B70.b3 W71.Kc3 B71.g5 W72.d5 B72.g4 W73.Rf4 B73.h5 W74.d6 "
[13] "W1.b3 B1.c5 W2.Bb2 B2.Nc6 W3.g3 B3.d6 W4.Bg2 B4.Nf6 W5.c4 B5.a6 W6.Nc3 B6.e5 W7.d3 B7.Nd4 W8.e3 B8.Bg4 W9.Qd2 B9.Nf5 W10.Nge2 B10.Bxe2 W11.Qxe2 B11.g6 W12.Bxb7 B12.Rb8 W13.Bc6+ B13.Nd7 W14.O-O B14.Bg7 W15.Bg2 B15.O-O W16.Nd5 B16.Nb6 W17.Nxb6 B17.Rxb6 W18.Bh3 B18.Qf6 W19.f4 B19.Rb4 W20.fxe5 B20.dxe5 W21.e4 B21.Qe7 W22.exf5 B22.Kh8 W23.Rae1 B23.Rbb8 W24.f6 B24.Bxf6 W25.Rxf6 B25.Qxf6 W26.Bxe5 B26.Qxe5 W27.Qxe5+ B27.Kg8 W28.Bg2 B28.Rbe8 W29.Qxe8 B29.Rxe8 W30.Rxe8+ B30.Kg7 W31.Bd5 B31.h5 W32.b4 B32.cxb4 W33.c5 B33.b3 W34.Bxb3 B34.f5 W35.c6 B35.f4 W36.c7 B36.fxg3 W37.c8=Q B37.gxh2+ W38.Kxh2 B38.h4 W39.Qe6 B39.Kh6 W40.Rg8 B40.Kg5 W41.Rxg6+ B41.Kh5 W42.Qg4# "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
[14] "W1.b3 B1.e5 W2.Bb2 B2.Nc6 W3.c4 B3.Bc5 W4.e3 B4.d6 W5.a3 B5.Bf5 W6.Nc3 B6.Be6 W7.Nf3 B7.h6 W8.Be2 B8.Nf6 W9.O-O B9.d5 W10.cxd5 B10.Bxd5 W11.b4 B11.e4 W12.Nxd5 B12.Nxd5 W13.bxc5 B13.exf3 W14.Bxf3 B14.O-O W15.Qb3 B15.Nde7 W16.d4 B16.Na5 W17.Qc3 B17.c6 W18.d5 B18.f6 W19.d6 B19.Ng6 W20.Be4 B20.Ne5 W21.f4 B21.Nec4 W22.Rfe1 B22.Nxb2 W23.Qxb2 B23.Nc4 W24.Qc3 B24.Na5 W25.Bb1 B25.b6 W26.e4 B26.bxc5 W27.e5 B27.c4 W28.e6 B28.Qb6+ W29.Kh1 B29.c5 W30.e7 B30.Rfb8 W31.Bg6 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
[15] "W1.d4 B1.e6 W2.c4 B2.b6 W3.a3 B3.Bb7 W4.Nc3 B4.f5 W5.d5 B5.Nf6 W6.g3 B6.Na6 W7.Bg2 B7.Nc5 W8.Nh3 B8.Bd6 W9.O-O B9.Be5 W10.Qc2 B10.O-O W11.Rd1 B11.Qe7 W12.Be3 B12.Rab8 W13.Rac1 B13.Nce4 W14.Nxe4 B14.Nxe4 W15.Nf4 B15.c5 W16.dxc6 B16.Bxc6 W17.Nd3 B17.Bf6 W18.f3 B18.Nc5 W19.b4 B19.Nxd3 W20.Rxd3 B20.d5 W21.f4 B21.dxc4 W22.Qxc4 B22.Bxg2 W23.Kxg2 B23.Rf7 W24.b5 B24.Re8 W25.Rcd1 B25.e5 W26.Rd7 B26.Qe6 W27.Qxe6 B27.Rxe6 W28.Kf3 B28.exf4 W29.gxf4 B29.Rxd7 W30.Rxd7 B30.Re7 W31.Rxe7 B31.Bxe7 W32.a4 B32.Kf7 W33.Bd4 B33.Bd6 W34.e4 B34.g6 W35.h3 B35.Ke6 W36.Bc3 B36.Bc7 W37.Bb4 B37.Bd8 W38.e5 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
[16] "W1.e4 B1.e5 W2.Nf3 B2.Nc6 W3.Bc4 B3.Nf6 W4.d3 B4.Bc5 W5.c3 B5.d6 W6.Bb3 B6.Be6 W7.Bc2 B7.O-O W8.O-O B8.Qd7 W9.d4 B9.exd4 W10.cxd4 B10.Bxd4 W11.Nxd4 B11.Nxd4 W12.Qxd4 B12.c5 W13.Qd3 B13.b5 W14.Rd1 B14.Rfd8 W15.Nc3 B15.Bc4 W16.Qg3 B16.Qg4 W17.Bf4 B17.Qe6 W18.b3 B18.Be2 W19.Nxe2 B19.d5 W20.Bg5 B20.dxe4 W21.Nf4 B21.Qe5 W22.Bxf6 B22.Qxf6 W23.Bxe4 B23.Re8 W24.Bxa8 B24.Rxa8 W25.Nd5 B25.Qe6 W26.Nc7 B26.Qc8 W27.Nxa8 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
[17] "W1.e4 B1.c6 W2.d4 B2.d5 W3.exd5 B3.cxd5 W4.Bd3 B4.Nc6 W5.c3 B5.Nf6 W6.Bf4 B6.Bg4 W7.Qb3 B7.Qd7 W8.Nd2 B8.e6 W9.Ngf3 B9.Bd6 W10.Ne5 B10.Bxe5 W11.dxe5 B11.Nh5 W12.Be3 B12.a6 W13.h3 B13.Nxe5 W14.Bf1 B14.Bf5 W15.g4 B15.Nd3+ W16.Bxd3 B16.Bxd3 W17.gxh5 B17.Rc8 W18.Rg1 B18.f6 W19.h6 B19.g6 W20.O-O-O B20.d4 W21.Bxd4 B21.Ke7 W22.Nf3 B22.e5 W23.Rxd3 B23.exd4 W24.Re1+ B24.Kf8 W25.Rxd4 B25.Qc6 W26.Qb4+ "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
[18] "W1.e4 B1.c5 W2.Nf3 B2.d6 W3.c3 B3.Nf6 W4.Be2 B4.Nc6 W5.d4 B5.cxd4 W6.cxd4 B6.e6 W7.Nc3 B7.Be7 W8.O-O B8.O-O W9.Bd3 B9.a6 W10.a3 B10.b5 W11.e5 B11.dxe5 W12.dxe5 B12.Nd7 W13.Qc2 B13.g6 W14.Bh6 B14.Re8 W15.Rad1 B15.Bb7 W16.Rfe1 B16.Rc8 W17.Qe2 B17.Qc7 W18.Bb1 B18.Red8 W19.h4 B19.Nc5 W20.h5 B20.Rxd1 W21.Rxd1 B21.Rd8 W22.Rxd8+ B22.Qxd8 W23.Be3 B23.Qc7 W24.Bf4 B24.Nb3 W25.Qd1 B25.Nc5 W26.Ne4 B26.Nxe4 W27.Bxe4 B27.Na5 W28.Bxb7 B28.Nxb7 W29.h6 B29.Nc5 W30.Bg5 B30.Ne4 W31.Bxe7 B31.Qxe7 W32.Qc2 B32.Nc5 W33.b4 B33.Nd7 W34.Qc7 B34.Qe8 W35.Ng5 B35.Nf8 W36.Ne4 B36.Nd7 W37.Qxd7 "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
[19] "W1.d4 B1.Nf6 W2.Nf3 B2.g6 W3.c4 B3.Bg7 W4.Nc3 B4.d5 W5.Qb3 B5.dxc4 W6.Qxc4 B6.O-O W7.e4 B7.a6 W8.Qb3 B8.c5 W9.dxc5 B9.Qa5 W10.Qb6 B10.Qxb6 W11.cxb6 B11.Nbd7 W12.Be2 B12.Nxb6 W13.Be3 B13.Nbd7 W14.Nd4 B14.Nc5 W15.f3 B15.e5 W16.Nc6 B16.bxc6 W17.Bxc5 B17.Rd8 W18.Kf2 B18.Be6 W19.Rhd1 B19.Nd7 W20.Be3 B20.Bf8 W21.Rd2 B21.f5 W22.Rad1 B22.Be7 W23.g3 B23.Kf7 W24.b3 B24.a5 W25.Rc2 B25.Nf6 W26.Rxd8 B26.Rxd8 W27.exf5 B27.gxf5 W28.Na4 B28.Bd5 W29.Bb6 B29.Ra8 W30.Bc5 B30.Nd7 W31.Bxe7 B31.Kxe7 W32.Ke3 B32.Kd6 W33.Bd3 B33.f4+ W34.gxf4 B34.exf4+ W35.Kxf4 B35.Rf8+ W36.Kg5 B36.Ne5 W37.Bxh7 B37.Nxf3+ W38.Kh6 B38.Rf4 W39.Re2 B39.Rh4+ W40.Kg7 B40.Nxh2 W41.Nc3 B41.Nf3 W42.Ne4+ B42.Kc7 W43.Nf6 B43.Nd4 W44.Nxd5+ B44.cxd5 W45.Rd2 B45.Kd6 W46.Bd3 B46.Ne6+ W47.Kf6 B47.Rf4+ "                                                                                                                                                                                                                                                                                                                                                                                                                        
[20] "W1.e4 B1.c5 W2.Nf3 B2.d6 W3.d4 B3.cxd4 W4.Nxd4 B4.Nf6 W5.Nc3 B5.a6 W6.Be3 B6.Ng4 W7.Bg5 B7.h6 W8.Bh4 B8.g5 W9.Bg3 B9.Bg7 W10.h3 B10.Nf6 W11.Bc4 B11.Qb6 W12.O-O B12.O-O W13.Nde2 B13.Qxb2 W14.Bb3 B14.Qa3 W15.f4 B15.Nc6 W16.Kh1 B16.Be6 W17.Qd3 B17.Rac8 W18.fxg5 B18.hxg5 W19.Nd5 B19.Rfe8 W20.Rad1 B20.Nb4 W21.Qf3 B21.Nbxd5 W22.exd5 B22.Bd7 W23.c3 B23.a5 W24.Qd3 B24.a4 W25.Bc2 B25.Qc5 W26.Rxf6 B26.exf6 W27.Qh7+ B27.Kf8 W28.Nd4 B28.Re5 W29.Bxe5 B29.fxe5 W30.Ne6+ B30.Bxe6 W31.dxe6 B31.Rc7 W32.Bxa4 B32.d5 W33.Qf5 B33.Qc4 W34.Bd7 B34.Qf4 W35.Qb1 B35.fxe6 W36.Bxe6 B36.Ke7 W37.Bxd5 B37.Rd7 W38.c4 B38.Qe3 W39.Qh7 B39.Kd8 W40.Rb1 B40.Qf4 W41.Be6 B41.Re7 W42.Bg4 B42.Rf7 W43.Qd3+ B43.Qd4 W44.Qg6 " 

这些是国际象棋的动作,我想针对每一行确定女王/王后的最后位置以及是否被捕获。我已按照以下步骤进行操作(以白人玩家为例):

  1. 在最后一个皇后移动所在的字符串中找到索引-对于White,这将使用正则表达式"W\\d+\\.Qx?"
  2. 如果进行了皇后移动,请提取"W\\d+\\.Qx?"正则表达式后的下两个字符以获取平方(例如f3)-否则,如果找不到与正则表达式匹配的字符,皇后位置作为默认的d1(开始位置)
  3. 在最后一个皇后移动之后搜索其余移动,以查看是否再次出现了标记最后确定女王所在的正方形的2个字符-如果是,那么敌人的棋子一定已经移动到这个正方形并俘获了女王。

这是我做的上述功能:

df$white_queen_last_position <- 'd1'
df$white_queen_captured <- FALSE
df$black_queen_last_position <- 'd8'
df$black_queen_captured <- FALSE
add_queen <- function() { 
    for (i in 1:nrow(df)) {
        #white
        white_last_queen_position_index <- stri_locate_last_regex(df$moves[i], "W\\d+\\.Qx?")[2]
        if (is.na(white_last_queen_position_index)) {
            white_last_queen_position <- 'd1'
            white_remainder_of_game <- df$moves[i]
        } else {
            white_last_queen_position <- str_sub(df$moves[i], white_last_queen_position_index + 1, white_last_queen_position_index + 2)
            white_remainder_of_game <- str_sub(df$moves[i], white_last_queen_position_index + 3, nchar(df$moves[i]))
        }
        white_is_captured <- grepl(white_last_queen_position, white_remainder_of_game)
        #black
        black_last_queen_position_index <- stri_locate_last_regex(df$moves[i], "B\\d+\\.Qx?")[2]
        if (is.na(black_last_queen_position_index)) {
            black_last_queen_position <- 'd8'
            black_remainder_of_game <- df$moves[i]
        } else {
            black_last_queen_position <- str_sub(df$moves[i], black_last_queen_position_index + 1, black_last_queen_position_index + 2)
            black_remainder_of_game <- str_sub(df$moves[i], black_last_queen_position_index + 3, nchar(df$moves[i]))
        }
        black_is_captured <- grepl(black_last_queen_position, black_remainder_of_game)
        #add to df
        df$white_queen_last_position[i] <- white_last_queen_position
        df$white_queen_captured[i] <- white_is_captured
        df$black_queen_last_position[i] <- black_last_queen_position
        df$black_queen_captured[i] <- black_is_captured
    }
}

这将为前20行生成如下输出:

   white_queen_last_position white_queen_captured black_queen_last_position black_queen_captured
1                         d1                 TRUE                        d8                 TRUE
2                         d1                 TRUE                        d5                 TRUE
3                         f6                FALSE                        d7                FALSE
4                         h6                FALSE                        f7                FALSE
5                         b6                FALSE                        c2                FALSE
6                         h5                FALSE                        a2                FALSE
7                         f3                FALSE                        e6                FALSE
8                         d8                FALSE                        g7                FALSE
9                         d7                 TRUE                        d7                 TRUE
10                        f5                FALSE                        a4                FALSE
11                        g3                 TRUE                        g3                 TRUE
12                        f5                 TRUE                        f5                 TRUE
13                        g4                FALSE                        e5                 TRUE
14                        c3                FALSE                        b6                FALSE
15                        e6                 TRUE                        e6                 TRUE
16                        g3                FALSE                        c8                FALSE
17                        b4                FALSE                        c6                FALSE
18                        d7                FALSE                        e8                FALSE
19                        b6                 TRUE                        b6                 TRUE
20                        g6                FALSE                        d4                FALSE

这是可行的,但在350万行的数据集上需要花费很长的时间。我发现行越多,每次迭代的速度就越慢(例如,如果只有10000行,则在5秒钟之内在我的计算机上完成-如果有20000行,则需要18秒)。我听说过像data.table这样的软件包,它们可以使此过程更快,但不确定如何在此处使用。

2 个答案:

答案 0 :(得分:1)

您可以将其转换为函数(目前不确定add_queen函数的用途是因为它只是围绕循环),然后从{{ 1}}。

这应该比循环快,但是如果没有完整的数据集,很难确定

adply

答案 1 :(得分:0)

这是另一种解决方案,可以完全向量化解决方案,并避免使用plyr包装。这应该比adply函数有更多的改进。

假设您的数据框名为:数据行,其中有1列名为moves。

library(stringi)

#find index of last move( set default to 1 if move is not found)
white_last_queen_position_index <- stri_locate_last_regex(datarow$moves, "W\\d+\\.Qx?")[,2]
white_last_queen_position_index[is.na(white_last_queen_position_index)]<-1
#extract the queens last move
white_last_queen_move<-stri_extract_last_regex(datarow$moves, "W\\d+\\.Qx?..")
#find board position of last move, set default to d1 if no moves were found
white_last_queen_position <-ifelse(is.na(white_last_queen_move), "d1" , substring(white_last_queen_move, nchar(white_last_queen_move)-1))

#find all moves after white queens last move
white_remainder_of_game <- stri_sub(df$moves, white_last_queen_position_index + 3, nchar(df$moves))
#determine if queens was captured
white_is_captured <- stri_detect_fixed(white_remainder_of_game, white_last_queen_position)


#repeat for black queen
black_last_queen_position_index <- stri_locate_last_regex(datarow$moves, "B\\d+\\.Qx?")[,2]
black_last_queen_position_index[is.na(black_last_queen_position_index)]<-1
black_last_queen_move<-stri_extract_last_regex(datarow$moves, "B\\d+\\.Qx?..")
black_last_queen_position <-ifelse(is.na(black_last_queen_move), "d8" , substring(black_last_queen_move, nchar(black_last_queen_move)-1))
black_remainder_of_game <- stri_sub(df$moves, black_last_queen_position_index + 3, nchar(df$moves))
black_is_captured <- stri_detect_fixed(black_remainder_of_game, black_last_queen_position)

#create the final answer
answer<-data.frame(white_last_queen_position, white_is_captured, black_last_queen_position, black_is_captured)

我还替换了stri_sub函数,以消除对stringer包的依赖。