如何使用列中的近似值(例如 5)提取行?我想提取某一列中資料值最接近 5 的行。 在此處輸入圖片說明
例如,我想提取資料值最接近 5 in 的行pro.f column。
uj5u.com熱心網友回復:
您可以在使用pro.f列最接近5號的位置abs()沿which.min()。然后,將其用作索引以提取該特定行。
df[which.min(abs(df$pro.f - 5)),]
輸出
var freq pro.f
3 328 12 5.09
資料
structure(list(
var = c(326, 327, 328),
freq = c(10, 11, 12),
pro.f = c(4.24, 4.65, 5.09)
),
class = "data.frame",
row.names = c(NA,-3L))
uj5u.com熱心網友回復:
在dplyr你可以使用slice_min:
df %>%
slice_min(abs(pro.f - 5))
var freq pro.f pro.f_temp
1 328 12 5.09 0.09
資料:
df <- data.frame(
var = c(326, 327, 328, 322, 321),
freq = c(10, 11, 12, 10, 11),
pro.f = c(4.24, 4.65, 5.09, 5.10, 5.11)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350431.html
標籤:r
上一篇:如何構建用于重復事件分析的資料?
