我想知道是否有人有一種有效的方法可以根據同一資料框中的列值洗掉不同的行。如果此列與 0 最接近,我想保留其中的值的行。
例如,我有這個資料框:
df = data.frame(cond1=c("a","a","a","b","b"),cond2=c(1,1,2,3,3),value=c(10,-20,5,-5,12))
df
cond1 cond2 value
a 1 10
a 1 -20
a 2 5
b 3 -5
b 3 12
我想獲得的是洗掉具有相同cond1且距 0cond2最遠的行value:
cond1 cond2 value
a 1 10
a 2 5
b 3 -5
在這種情況下value==-value我最好將兩行保留在我的資料框中。你有什么建議來克服我的問題嗎?我正在考慮將group_by、arrange和filterfrom結合起來,dplyr但我很難讓它適應我的情況。謝謝
uj5u.com熱心網友回復:
適用于您的示例資料的其他答案的變體。請注意,說:“最近從0”(我假定你的意思最接近的另一種方式,以0)簡直是絕對值最小。
library(dplyr)
df %>%
group_by(cond1, cond2) %>%
filter(abs(value) == min(abs(value))) %>%
ungroup()
結果:
# A tibble: 4 × 3
cond1 cond2 value
<chr> <dbl> <dbl>
1 a 1 10
2 a 2 5
3 b 3 -5
如果我們改變df使得第二個 (a, 1) = -10 我們得到:
# A tibble: 4 × 3
cond1 cond2 value
<chr> <dbl> <dbl>
1 a 1 10
2 a 1 -10
3 a 2 5
4 b 3 -5
uj5u.com熱心網友回復:
使用dplyr,我們有:
df %>%
mutate(dist = abs(0 - value)) %>%
group_by(cond1, cond2) %>%
filter(dist == min(dist)) %>%
select(-dist)
輸出:
# A tibble: 3 x 3
# Groups: cond1, cond2 [3]
cond1 cond2 value
<chr> <dbl> <dbl>
1 a 1 10
2 a 2 5
3 b 3 -5
對于-value == value條件,這也適用:
資料:
structure(list(cond1 = c("a", "a", "a", "b", "b"), cond2 = c(1,
1, 2, 3, 3), value = c(10, -10, 5, -5, 12)), row.names = c(NA,
-5L), class = "data.frame")
cond1 cond2 value
1 a 1 10
2 a 1 -10
3 a 2 5
4 b 3 -5
5 b 3 12
代碼:
df %>%
mutate(dist = abs(0 - value)) %>%
group_by(cond1, cond2) %>%
filter(dist == min(dist)) %>%
select(-dist)
輸出:
cond1 cond2 value
<chr> <dbl> <dbl>
1 a 1 10
2 a 1 -10
3 a 2 5
4 b 3 -5
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/364204.html
上一篇:根據多個變數添加組名
