计算两次测量之间的增量,同时保留R中各行的相同单位

时间:2018-07-25 17:17:09

标签: r dataframe dplyr

我有2个这样的数据框

width:1.067em; height:1em

我试图在df2中创建2列MEASUREMENT <- c("Length","Width","Breadth","Height") MULTIPLIER <- c(1,0.1,1000,1) df1 <- data.frame(MEASUREMENT,MULTIPLIER) INPUT_MEASUREMENT <- c("Length","Length","Breadth","Breadth","Height") INPUT_AVERAGE <- c(4.3,4.2,0.001,0.003,7) INPUT_TARGET <- c(4,4,0.005,0.005,10) OUTPUT_MEASUREMENT <- c("Breadth","Breadth","Length","Width","Height") OUTPUT_AVERAGE <- c(0.006,0.011,6.8,68,9) OUTPUT_TARGET <- c(0.005,0.005,6,60,10) df2 <- data.frame(INPUT_MEASUREMENT,INPUT_AVERAGE,INPUT_TARGET, OUTPUT_MEASUREMENT,OUTPUT_AVERAGE,OUTPUT_TARGET) DELTA_AVERAGE,分别是(输出-输入)平均值和目标的绝对值。由于每次测量的标度不同,因此我想在计算增量平均值和目标值之前将df1中的乘数列用于各个测量,以使所得数据帧在所有行中具有相同的单位。

我想要的输出是

DELTA_TARGET

我正在尝试以这种方式进行操作,但并没有正确完成

  INPUT_MEASUREMENT INPUT_AVERAGE INPUT_TARGET OUTPUT_MEASUREMENT OUTPUT_AVERAGE OUTPUT_TARGET DELTA_AVERAGE
             Length         4.300        4.000            Breadth          0.006         0.005           1.7
             Length         4.200        4.000            Breadth          0.011         0.005           6.8
            Breadth         0.001        0.005             Length          6.800         6.000           5.8
            Breadth         0.003        0.005              Width         68.000        60.000           3.8
             Height         7.000       10.000             Height          9.000        10.000           2.0
  DELTA_TARGET
             1
             1
             1
             1
             0

有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

这里的“技巧”是制作df1的2个副本并重命名各列:

# create data
df1 <- data.frame(
    measurement = c("Length","Width","Breadth","Height"),
    multiplier  = c(1,0.1,1000,1),
    stringsAsFactors = FALSE
)

df2 <- data.frame(
    input_measurement = c("Length","Length","Breadth","Breadth","Height"),
    input_average = c(4.3,4.2,0.001,0.003,7),
    input_target = c(4,4,0.005,0.005,10),
    output_measurement = c("Breadth","Breadth","Length","Width","Height"),
    output_average = c(0.006,0.011,6.8,68,9),
    output_target = c(0.005,0.005,6,6,10),
    stringsAsFactors = FALSE
)

# copy and rename df1 so can be merged as both input and output
df1_input <- df1
names(df1_input) <- c("input_measurement", "input_multiplier")

df1_output <- df1
names(df1_output) <- c("output_measurement", "output_multiplier")

# merge df1 onto df2 twice (once for input, once for output)
df2 <- merge(df2, df1_input,  by="input_measurement")
df2 <- merge(df2, df1_output, by="output_measurement")

# calculate deltas
df2$delta_average <- df2$output_average * df2$output_multiplier - df2$input_average * df2$input_multiplier
df2$delta_target  <- df2$output_target  * df2$output_multiplier - df2$input_target  * df2$input_multiplier