无法复制tableB [tableA]的预期结果

时间:2019-06-25 12:02:57

标签: r join data.table

我无法对我的数据使用tableB [tableA]产生预期的结果。但是在简单的示例数据上也可以正常工作。请解码我在做什么错。

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

<header class="fixed-top" id="header">
  <div class="container">
    <div class="row">
      <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2">
        <img src="media/logo.png" alt="Logo" class="logo">
      </div>
      <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
        <form class="form-inline my-2 my-lg-0 search">
          <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
          <button class="btn btn-secondary my-2 my-sm-0 orange" type="submit">
							<img src="media/icons/search.png" alt="Search" class="icon">
						</button>
        </form>
      </div>
      <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
          <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
							<span class="navbar-toggler-icon"></span>
						</button>

          <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">
              <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
              </li>
              <li class="nav-item">
                <a class="nav-link" href="#">Link</a>
              </li>
              <li class="nav-item dropdown">
                <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
										Dropdown
									</a>
                <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                  <a class="dropdown-item" href="#">Action</a>
                  <a class="dropdown-item" href="#">Another action</a>
                  <div class="dropdown-divider"></div>
                  <a class="dropdown-item" href="#">Something else here</a>
                </div>
              </li>
              <li class="nav-item">
                <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
              </li>
            </ul>
          </div>
        </nav>
      </div>
    </div>
  </div>
</header>

这是预期的。但是..


> tableA <- data.table(col1 = c( 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6,        1.7, 1.8, 1.9), key = 'col1')

> tableA
    col1
 1:  1.0
 2:  1.1
 3:  1.2
 4:  1.3
 5:  1.4
 6:  1.5
 7:  1.6
 8:  1.7
 9:  1.8
10:  1.9

> tableB <- data.table(col1 = c( 1.0, 1.2, 1.5, 1.9), col2 = c( "A", "B", "C", "D"), col3 = c( "AA", "BB", "CC", "DD"), key = 'col1')

> tableB
   col1 col2 col3
1:  1.0    A   AA
2:  1.2    B   BB
3:  1.5    C   CC
4:  1.9    D   DD

> tableA <- tableB[ tableA]

> tableA
    col1 col2 col3
 1:  1.0    A   AA
 2:  1.1 <NA> <NA>
 3:  1.2    B   BB
 4:  1.3 <NA> <NA>
 5:  1.4 <NA> <NA>
 6:  1.5    C   CC
 7:  1.6 <NA> <NA>
 8:  1.7 <NA> <NA>
 9:  1.8 <NA> <NA>
10:  1.9    D   DD

它不是任何“错别字”。我可以一次又一次地复制相同的精确结果。 任何见解都是有价值的。

没有错误。我期望16个有效行和4个NA行。但是,我只得到13个有效行和7个NA行。

1 个答案:

答案 0 :(得分:1)

这是由于V1中十进制值的浮点错误。这不是R(或data.table)问题,而仅仅是计算机使用十进制数字的方式。
请阅读:Why are these numbers not equal?,以了解更多信息。

为防止此类“错误”,一种解决方案是将联接列设置为字符。

NA

将给出预期的结果

tableA[, V1 := as.character(V1)]
tableB[, V1 := as.character(V1)]

tableB[tableA, on = .(V1)]