[英]Compare column values against another column
I have the following data: 我有以下数据:
set.seed(1)
data <- data.frame(
id = 1:500, ht_1 = rnorm(500,10:20), ht_2 = rnorm(500,15:25),
ht_3 = rnorm(500,20:30), ht_4 = rnorm(500,25:35),
ht_5 = rnorm(500,20:40)
)
I would like to identify the values in columns ht_1:ht_4
that are greater than the values in column ht_5
(number of observations and means). 我想,以确定列中的值
ht_1:ht_4
是比在列中的值更大ht_5
(观察和装置的数量)。
For each of these columns, I would then like to replace any values that are greater than ht_5
with ht_5
. 对于这些列中的每一列,我
ht_5
用ht_5
替换任何大于ht_5
的ht_5
。
Hi you can use the mutate_at
function like this: 嗨,您可以像这样使用
mutate_at
函数:
library(tidyverse)
data %>% as_tibble %>%
mutate_at(vars(paste0("ht_", 1:4)), ~if_else(.x > ht_5, ht_5, .x))
In this case you can also use pmin
instead of if_else
which should be faster. 在这种情况下,您也可以使用
pmin
代替if_else
,它应该更快。
data %>% as_tibble %>%
mutate_at(vars(paste0("ht_", 1:4)), ~pmin(.x, ht_5))
To see how many values are greater than ht_5
you can use the summarise_at
function: 看看有多少值都大于
ht_5
可以使用summarise_at
功能:
data %>% as_tibble %>%
summarize_at(vars(paste0("ht_", 1:4)), ~ length(.x[.x > ht_5]))
# A tibble: 1 x 4
ht_1 ht_2 ht_3 ht_4
<int> <int> <int> <int>
1 6 39 131 258
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.