2017-07-25 29 views
1

替代他們,我有一個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匹配高度的值,並給我參考通訊員重量。

我還沒有取得任何成功,因爲我一直無法找到任何有關匹配值不完全相同的值。

+0

請使用重複的例子,即刪除'...' – akrun

+0

你試過像'which.min(ABS(X - 一個))'哪裏'x'是矢量,'a'是你找到的匹配數? – Aramis7d

+1

你可以添加你想要的輸出嗎? –

回答

5

如果我理解你的目標,而不是採取就近值,考慮interpolating通過approx功能。例如:

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

這是什麼樣的魔術! o_O – Sotos

+0

完美工作,非常感謝! – mle

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 
-1
z <- vector() 
for(i in 1:length(Height)) { 
z[i] <- weightfactor_f$weights_f[which.min(abs(Height[i]-weightfactor_f$heights_f))] 
} 
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 
相關問題