R将列的每个数据值与列中的其余值进行比较?

时间:2015-05-10 21:29:11

标签: r

我想创建一个查看一列值的函数。从这些值中单独查看每个值,并评估哪个其他数据点值最接近该数据点。 我猜测它可以通过检查数据帧的长度来完成,以1为步长列出各自的长度。然后使用该列表来引用正在分析哪个单元格与列的其余部分。虽然我不知道如何实现它。 例如。 数据:

  1. 20
  2. 17
  3. 29
  4. 33

    • 1)最接近2)
    • 2)最接近1)
    • 3)最接近4)
    • 4)最接近3)
  5. 我找到了这个测试相似性的例子,但我想知道要分配的是什么字母。

    x=c(1:100)
    your.number=5.43
    which(abs(x-your.number)==min(abs(x-your.number)))
    

    另外,如果你知道如何做到这一点,你能否了解代码的各个部分及其含义?

1 个答案:

答案 0 :(得分:0)

我编写了一个快速函数,它与您提供的代码完全相同。

您提供的代码采用数字与向量中每个值之间的差值的绝对值,并将该值与该向量的最小值进行比较。这与我在下面使用的DEPRECATION: --no-install, --no-download, --build, and --no-clean are deprecated. Downloading/unpacking matplotlib Running setup.py (path:/tmp/pycharm-packaging7.tmp/matplotlib/setup.py) egg_info for package matplotlib Traceback (most recent call last): File "<string>", line 17, in <module> File "/tmp/pycharm-packaging7.tmp/matplotlib/setup.py", line 155, in <module> result = package.check() File "/tmp/pycharm-packaging7.tmp/matplotlib/setupext.py", line 961, in check min_version='2.3', version=version) File "/tmp/pycharm-packaging7.tmp/matplotlib/setupext.py", line 445, in _check_for_pkg_config if (not is_min_version(version, min_version)): File "/tmp/pycharm-packaging7.tmp/matplotlib/setupext.py", line 173, in is_min_version return found_version >= expected_version File "/usr/lib/python3.4/distutils/version.py", line 76, in __ge__ c = self._cmp(other) File "/usr/lib/python3.4/distutils/version.py", line 343, in _cmp if self.version < other.version: TypeError: unorderable types: str() < int() ============================================================================ Edit setup.cfg to change the build options BUILDING MATPLOTLIB matplotlib: yes [1.4.3] python: yes [3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2]] platform: yes [linux] REQUIRED DEPENDENCIES AND EXTENSIONS numpy: yes [version 1.9.2] six: yes [using six version 1.5.2] dateutil: yes [using dateutil version 2.4.2] pytz: yes [using pytz version 2015.2] tornado: yes [using tornado version 4.1] pyparsing: yes [using pyparsing version 2.0.1] pycxx: yes [Official versions of PyCXX are not compatible with matplotlib on Python 3.x, since they lack support for the buffer object. Using local copy] libagg: yes [pkg-config information for 'libagg' could not be found. Using local copy.] Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 17, in <module> File "/tmp/pycharm-packaging7.tmp/matplotlib/setup.py", line 155, in <module> result = package.check() File "/tmp/pycharm-packaging7.tmp/matplotlib/setupext.py", line 961, in check min_version='2.3', version=version) File "/tmp/pycharm-packaging7.tmp/matplotlib/setupext.py", line 445, in _check_for_pkg_config if (not is_min_version(version, min_version)): File "/tmp/pycharm-packaging7.tmp/matplotlib/setupext.py", line 173, in is_min_version return found_version >= expected_version File "/usr/lib/python3.4/distutils/version.py", line 76, in __ge__ c = self._cmp(other) File "/usr/lib/python3.4/distutils/version.py", line 343, in _cmp if self.version < other.version: TypeError: unorderable types: str() < int() ============================================================================ Edit setup.cfg to change the build options 函数相同。我按照下面的步骤进行操作。希望这会有所帮助。

制作一些数据

which.min

a = 1:100 yourNumber = 6 是您的号码,Num是向量

x

然后,如果运行此命令,它应返回与您指定数字的最接近值对应的向量值的索引。

getClosest=function(x, Num){
 return(which.min(abs(x-Num)))
}