将变量与另一个数据帧中的值进行比较,并将其替换为另一个值

时间:2017-07-25 09:46:55

标签: r

我有一个Data.Frame:

Height <- c(169,176,173,172,176,158,168,162,178)

和另一个参考高度和重量。

heights_f <- c(144.8,147.3,149.9,152.4,154.9,157.5,160,162.6,165.1,167.6,170.2,172.7,175.3,177.8,180.3,182.9,185.4,188,190.5,193,195.6)
weights_f <- c(38.6,40.9,43.1,45.4,47.7,49.9,52.2,54.5,56.8,59,61.3,63.6,65.8,68.1,70.4,72.6,74.9,77.2,79.5,81.7,84)
weightfactor_f <- data.frame(heights_f, weights_f)

我现在需要将第一个data.frame中的高度值与第二个中最适合的高度参考值匹配,并给出相应的参考权重。

我还没有取得任何成功,因为我还没有找到任何关于不完全相同的匹配值的信息。

4 个答案:

答案 0 :(得分:5)

如果我了解您的目标,而不是采用最接近的值,请通过approx函数考虑interpolating。例如:

approx(weightfactor_f$heights_f,weightfactor_f$weights_f,xout=Height)$y
#[1] 60.23846 66.44400 63.85385 62.95600 66.44400 50.36000 59.35385 53.96923
#[9] 68.28400

答案 1 :(得分:0)

你可以这样做:

Height<- c(169,176,173,172,176,158,168,162,178)
heights_f<- as.numeric(c(144.8,147.3,149.9,152.4,154.9,157.5,160,162.6,165.1,167.6,170.2,172.7,175.3,177.8,180.3,182.9,185.4,188,190.5,193,195.6)) 
weights_f<- as.numeric(c(38.6,40.9,43.1,45.4,47.7,49.9,52.2,54.5,56.8,59,61.3,63.6,65.8,68.1,70.4,72.6,74.9,77.2,79.5,81.7,84)) 

df = data.frame(Height=Height, match_weight=
          sapply(Height, function(x) {weights_f[which.min(abs(heights_f-x))]}))

即。对于Height中的每个条目,通过执行which.min(abs(heights_f-x)找到heights_f向量中的相应元素,并从weights_f向量中获取相应的条目。

输出:

  Height match_weight
1    169         61.3
2    176         65.8
3    173         63.6
4    172         63.6
5    176         65.8
6    158         49.9
7    168         59.0
8    162         54.5
9    178         68.1

答案 2 :(得分:0)

library(dplyr)

结构略有不同,可再现的例子:

Height <- data.frame(height = as.numeric(c(169,176,173,172,176,158,168,162,178)))

其余的是相同的:

heights_f<- as.numeric(c(144.8,147.3,149.9,152.4,154.9,157.5,160,162.6,165.1,167.6,170.2,172.7,175.3,177.8,180.3,182.9,185.4,188,190.5,193,195.6)) 
weights_f<- as.numeric(c(38.6,40.9,43.1,45.4,47.7,49.9,52.2,54.5,56.8,59,61.3,63.6,65.8,68.1,70.4,72.6,74.9,77.2,79.5,81.7,84)) 
weightfactor_f<- data.frame(heights_f,weights_f)

然后,舍入到最接近的整数:

weightfactor_f$heights_f <- round(weightfactor_f$heights_f, 0)

然后只是:

left_join(Height, weightfactor_f, by = c("height" = "heights_f"))

输出:

  height weights_f
1    169        NA
2    176        NA
3    173      63.6
4    172        NA
5    176        NA
6    158      49.9
7    168      59.0
8    162        NA
9    178      68.1

答案 3 :(得分:-1)

z <- vector()
for(i in 1:length(Height)) {
z[i] <- weightfactor_f$weights_f[which.min(abs(Height[i]-weightfactor_f$heights_f))]
}