你能拉最小从一个变量给定条件下的另一个变量在R?



假设我有来自下面最小可复制示例的数据。我想用ready的最小值更新MinReadyAge变量。data$ reage from ready。其中的数据准备就绪。data$Ready == 'Yes'。有没有有效的方法来做这件事?

如果有方法返回"Never Ready"对于任何年龄都没有"是"的情况

ID <- c(1,2,3,4,5)
MinReadyAge <- 1:5
min.age.data <- as.data.frame(cbind(ID,MinReadyAge))
ID <- c(1,1,1,1,1,
2,2,2,2,2,
3,3,3,3,3,
4,4,4,4,4,
5,5,5,5,5)
RetAge <- rep(seq(from = 65, to = 69, by = 1),5)
Ready <- c("No","No","No","No","No",
"No","No","No","No","Yes",
"No","No","No","Yes","Yes",
"No","No","Yes","Yes","Yes",
"Yes","Yes","Yes","Yes","Yes")
ready.data <- as.data.frame(cbind(ID,RetAge,Ready))

我们可以对'min.age '进行join操作。

将"Ready"为"Yes"的"RetAge"的min值按"ID"替换为"Ready"的"reage"的CC_2值。
library(dplyr)
ready.data %>% 
filter(Ready == 'Yes') %>% 
group_by(ID) %>% 
slice_min(RetAge) %>%
ungroup %>% 
select(ID, MinRetAge = RetAge) %>%
right_join(min.age.data) %>%
arrange(ID)
# A tibble: 5 x 3
#     ID MinRetAge MinReadyAge
#  <dbl>     <dbl>       <int>
#1     1        NA           1
#2     2        69           2
#3     3        68           3
#4     4        67           4
#5     5        65           5

如果我们需要更新'MinRetAge'列

min.age.data <- ready.data %>% 
filter(Ready == 'Yes') %>% 
group_by(ID) %>% 
slice_min(RetAge) %>% 
ungroup %>% 
select(ID, RetAge) %>% 
right_join(min.age.data) %>%
transmute(ID, MinReadyAge = coalesce(RetAge, MinReadyAge)) %>%
arrange(ID)

与产出

min.age.data
# A tibble: 5 x 2
#     ID MinReadyAge
#  <dbl>       <dbl>
#1     1           1
#2     2          69
#3     3          68
#4     4          67
#5     5          65

数据
# // as.data.frame(cbind is not needed and it will unnecessarily chage the type
# // cbind by default returns a matrix and matrix can have only a single type
# // instead using data.frame directly
ready.data <- data.frame(ID, RetAge, Ready)
min.age.data <- data.frame(ID, MinReadyAge)

相关内容

  • 没有找到相关文章

最新更新