比较两个数据帧并更新一列

时间:2017-09-26 15:32:56

标签: r

我理解如何比较两个数据帧:

setdiff(df1,df2)

但是有没有比较一个列,如果它不同,请更新该值。所以说在df1我有

A  |  B
-------
a  |  3
b  |  7
c  |  8

在df2中我有

A  |  B
-------
a  |  3
b  |  10
c  |  8

是否有办法将值的变化(b已更改为10)应用于df1?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

df1 = data.frame(A = c("a", "b", "c"),
                 B = c(3, 7, 8))

df2 = data.frame(A = c("a", "b", "c"),
                 B = c(3, 10, 8))


diff_index = which(df1$B != df2$B)
df1$B[diff_index] = df2$B[diff_index]

diff_index提取df1$B不等于df2$B的索引,然后使用该索引将df1$B替换为df2$B

中的内容

答案 1 :(得分:0)

df1 = data.frame(a =字母[1:3],b = c(3,7,8))

df2 = data.frame(a =字母[1:3],b = c(3,10,8))

<?xml version="1.0"?>
<TOP-LEVEL-PACKAGES>
  <AR-PACKAGE>
    <SHORT-NAME>DataType</SHORT-NAME>
    <ELEMENTS>
      <INTEGER-TYPE>
        <SHORT-NAME>EngineSpeed_T</SHORT-NAME>
        <SW-DATA-DEF-PROPS>
          <COMPU-METHOD-REF DEST="COMPU-METHOD">/DataType/DataTypeSemantics/EngineSpeed_T</COMPU-METHOD-REF>
        </SW-DATA-DEF-PROPS>
        <LOWER-LIMIT INTERVAL-TYPE="CLOSED">0</LOWER-LIMIT>
        <UPPER-LIMIT INTERVAL-TYPE="CLOSED">65535</UPPER-LIMIT>
      </INTEGER-TYPE>
      <INTEGER-TYPE>
        <SHORT-NAME>VehicleSpeed_T</SHORT-NAME>
        <SW-DATA-DEF-PROPS>
          <COMPU-METHOD-REF DEST="COMPU-METHOD">/DataType/DataTypeSemantics/VehicleSpeed_T</COMPU-METHOD-REF>
        </SW-DATA-DEF-PROPS>
        <LOWER-LIMIT INTERVAL-TYPE="CLOSED">0</LOWER-LIMIT>
        <UPPER-LIMIT INTERVAL-TYPE="CLOSED">65535</UPPER-LIMIT>
      </INTEGER-TYPE>
      <INTEGER-TYPE>
        <SHORT-NAME>Percent_T</SHORT-NAME>
        <SW-DATA-DEF-PROPS>
          <COMPU-METHOD-REF DEST="COMPU-METHOD">/DataType/DataTypeSemantics/Percent_T</COMPU-METHOD-REF>
        </SW-DATA-DEF-PROPS>
        <LOWER-LIMIT INTERVAL-TYPE="CLOSED">0</LOWER-LIMIT>
        <UPPER-LIMIT INTERVAL-TYPE="CLOSED">255</UPPER-LIMIT>
      </INTEGER-TYPE>
    </ELEMENTS>
    <SUB-PACKAGES>
      <AR-PACKAGE>
        <SHORT-NAME>DataTypeSemantics</SHORT-NAME>
        <ELEMENTS>
          <COMPU-METHOD>
            <SHORT-NAME>EngineSpeed_T</SHORT-NAME>
            <UNIT-REF DEST="UNIT">/DataType/DataTypeUnits/rpm</UNIT-REF>
            <COMPU-INTERNAL-TO-PHYS>
              <COMPU-SCALES>
                <COMPU-SCALE>
                  <COMPU-RATIONAL-COEFFS>
                    <COMPU-NUMERATOR>
                      <V>0</V>
                      <V>1</V>
                    </COMPU-NUMERATOR>
                    <COMPU-DENOMINATOR>
                      <V>8</V>
                    </COMPU-DENOMINATOR>
                  </COMPU-RATIONAL-COEFFS>
                </COMPU-SCALE>
              </COMPU-SCALES>
            </COMPU-INTERNAL-TO-PHYS>
          </COMPU-METHOD>
          <COMPU-METHOD>
            <SHORT-NAME>VehicleSpeed_T</SHORT-NAME>
            <UNIT-REF DEST="UNIT">/DataType/DataTypeUnits/kph</UNIT-REF>
            <COMPU-INTERNAL-TO-PHYS>
              <COMPU-SCALES>
                <COMPU-SCALE>
                  <COMPU-RATIONAL-COEFFS>
                    <COMPU-NUMERATOR>
                      <V>0</V>
                      <V>1</V>
                    </COMPU-NUMERATOR>
                    <COMPU-DENOMINATOR>
                      <V>64</V>
                    </COMPU-DENOMINATOR>
                  </COMPU-RATIONAL-COEFFS>
                </COMPU-SCALE>
              </COMPU-SCALES>
            </COMPU-INTERNAL-TO-PHYS>
          </COMPU-METHOD>
          <COMPU-METHOD>
            <SHORT-NAME>Percent_T</SHORT-NAME>
            <UNIT-REF DEST="UNIT">/DataType/DataTypeUnits/Percent</UNIT-REF>
            <COMPU-INTERNAL-TO-PHYS>
              <COMPU-SCALES>
                <COMPU-SCALE>
                  <COMPU-RATIONAL-COEFFS>
                    <COMPU-NUMERATOR>
                      <V>0</V>
                      <V>0.4</V>
                    </COMPU-NUMERATOR>
                    <COMPU-DENOMINATOR>
                      <V>1</V>
                    </COMPU-DENOMINATOR>
                  </COMPU-RATIONAL-COEFFS>
                </COMPU-SCALE>
              </COMPU-SCALES>
            </COMPU-INTERNAL-TO-PHYS>
          </COMPU-METHOD>
          <COMPU-METHOD>
            <SHORT-NAME>CoolantTemp_T</SHORT-NAME>
            <UNIT-REF DEST="UNIT">/DataType/DataTypeUnits/DegreeC</UNIT-REF>
            <COMPU-INTERNAL-TO-PHYS>
              <COMPU-SCALES>
                <COMPU-SCALE>
                  <COMPU-RATIONAL-COEFFS>
                    <COMPU-NUMERATOR>
                      <V>-40</V>
                      <V>1</V>
                    </COMPU-NUMERATOR>
                    <COMPU-DENOMINATOR>
                      <V>2</V>
                    </COMPU-DENOMINATOR>
                  </COMPU-RATIONAL-COEFFS>
                </COMPU-SCALE>
              </COMPU-SCALES>
            </COMPU-INTERNAL-TO-PHYS>
          </COMPU-METHOD>
        </ELEMENTS>
      </AR-PACKAGE>
      <AR-PACKAGE>
        <SHORT-NAME>DataTypeUnits</SHORT-NAME>
        <ELEMENTS>
          <UNIT>
            <SHORT-NAME>rpm</SHORT-NAME>
            <DISPLAY-NAME>rpm</DISPLAY-NAME>
          </UNIT>
          <UNIT>
            <SHORT-NAME>kph</SHORT-NAME>
            <DISPLAY-NAME>kph</DISPLAY-NAME>
          </UNIT>
          <UNIT>
            <SHORT-NAME>Percent</SHORT-NAME>
            <DISPLAY-NAME>Percent</DISPLAY-NAME>
          </UNIT>
          <UNIT>
            <SHORT-NAME>DegreeC</SHORT-NAME>
            <DISPLAY-NAME>DegreeC</DISPLAY-NAME>
          </UNIT>
        </ELEMENTS>
      </AR-PACKAGE>
    </SUB-PACKAGES>
  </AR-PACKAGE>
</TOP-LEVEL-PACKAGES>