我必须在R中创建一个函数(或循环)来检测超频。检测超频频率的要求将在180天内到达3次,如果该要求得到满足,则该人将变得异常频繁,不仅是在未来,而且在过去的访问中,他也没有满足超频繁的要求。
pacient <- c(10,10,10,10,10,11,11,12,12,12,13, 13, 15, 14); pacient
date <- as.Date(c("01/01/2018","02/05/2018", "04/06/2018", "10/11/2018", "05/12/2018", "02/01/2018", "06/08/2018", "01/01/2018", "03/01/2018", "06/03/2018", "05/08/2018", "05/08/2019", "05/07/2019", "08/07/2017"), format = "%d/%m/%Y"); date
DF <- data.frame(pacient, date); DF
count_visit <- function(x){
DF <- data.table(DF)
DTord<-DF[with(DF , order(DF $ date)), ]; DTord
DTord[,num_visit := order(date), by = pacient];DTord
DTordID <- DTord[with(DTord, order(DTord$pacient)), ]; DTordID
DTordID[,max_visit := max(num_visit), by = pacient];DTordID
framedatos <- as.data.frame(DTordID)
return(framedatos)}
REUP_visit <- count_visit(DF); head(REUP_visit)
pacient date num_visit max_visit
10 01/01/2018 1 5
10 02/05/2018 2 5
10 04/06/2018 3 5
10 10/11/2018 4 5
10 05/12/2018 5 5
11 02/01/2018 1 2
11 06/08/2018 2 2
12 01/01/2018 1 3
12 03/01/2018 2 3
12 06/03/2018 3 3
13 05/08/2018 1 2
13 05/08/2019 2 2
14 08/07/2017 1 1
15 05/07/2019 1 1到目前为止,我只创建了一个功能,它告诉我每名病人的就诊次数和病人的最大探视次数(这是我需要做其他事情的):
pacient date num_visit max_visit days_visit <180 future_hyperf past_hyperf
10 01/01/2018 1 5 0 1 no yes
10 02/05/2018 2 5 121 2 no yes
10 04/06/2018 3 5 33 3 yes yes
10 10/11/2018 4 5 159 4 yes yes
10 05/12/2018 5 5 25 5 yes yes
11 02/01/2018 1 2 0 1 no no
11 06/08/2018 2 2 216 1 no no
12 01/01/2018 1 3 0 1 no yes
12 03/01/2018 2 3 2 2 no yes
12 06/03/2018 3 3 62 3 yes yes
13 05/08/2018 1 2 0 1 no no
13 05/08/2019 2 2 365 1 no no
14 08/07/2017 1 1 0 1 no no
15 05/07/2019 1 1 0 1 no no 我需要的输出有:"day_visit“、"<180”、"future_hyperf“和"past_hyperf”。
变量"day_visit“的目标是将病人第一次到急诊室就诊的次数定为0,然后计算两次就诊之间的天数。
DF <- DF %>%
group_by(pacient) %>%
arrange(date) %>%
mutate(days_visit= date - lag(date, default = first(date)))变量" <180“将是第一次出现的数字1、第二个变量(如果是上次访问的<180天)、第三个变量(如果是上一次访问的<180天)等等。例如,如果病人达到2,而第三次就诊不能满足<180天,则有必要再次放置1(循环将被重新启动)。
变量"future_hyperf“说是或不是。如果病人在<180的变量中达到3岁,就好像它创造了未来一样,如果探视时间晚于180天,而且不符合规定,这并不重要。一旦达到了标准,就永远如此。
变量"past_hyperf“将变量"future_hyperf”中的所有患者都转换为过去。
谢谢!
解决方案
DF3 <- DF %>%
arrange(pacient, date) %>%
group_by(pacient) %>%
mutate(days_visit = as.integer(date - lag(date, default = first(date))) ,
less_180 = days_visit < 180) %>%
mutate(counter = rowid(pacient, cumsum(date - shift(date, fill=first(date)) > 180)),
future_hyperf = case_when(counter >= 3 ~ "yes",
TRUE ~ "no"),
past_hyperf = case_when(max(counter, na.rm = T) >= 3 ~ "yes",
TRUE ~ "no"))
DF3 <- DF3[with(DF3,order(pacient,date)),]发布于 2019-04-05 09:09:07
试试这个:
pacient <- c(10, 10, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 15, 14)
pacient
date <-
as.Date(
c(
"01/01/2018",
"02/05/2018",
"04/06/2018",
"10/11/2018",
"05/12/2018",
"02/01/2018",
"06/08/2018",
"01/01/2018",
"03/01/2018",
"06/03/2018",
"05/08/2018",
"05/08/2019",
"05/07/2019",
"08/07/2017"
),
format = "%d/%m/%Y"
)
date
DF <- data.frame(pacient, date)
DF
#packages
library(dplyr)
library(lubridate)
#time zone
lct <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
DF <- DF %>%
group_by(pacient) %>%
mutate(num_visit = cumsum(pacient) / pacient) %>% # number of visits
mutate(max_visit = max(num_visit)) %>% # max visit
mutate(days_visit = as.Date(date, "%d/%m/%Y") - lag(as.Date(date, "%d/%m/%Y"))) %>% # days between visits
mutate(minus_180_days = case_when(days_visit < 180 &
!is.na(days_visit) ~ num_visit,
TRUE ~ 1)) %>% # is days between visits < 180
mutate(future_hyperf = case_when(minus_180_days > 3 ~ "yes",
TRUE ~ "no")) %>% # future hyperf
mutate(past_hyperf = case_when(max(minus_180_days, na.rm = T) >= 3 ~ "yes",
TRUE ~ "no")) # past hyperf希望它能帮上忙
发布于 2019-04-05 08:59:20
我会这么做的。说明在注释中。
library(tidyverse)
DF %>%
group_by(pacient) %>% # group the data by "pacient"
mutate(lag_date = lag(date, n = 2)) %>% # create the variable of lag dates by 2 visits
mutate(date_diff = as.integer(date - lag_date)) %>% # Calculate the difference in dates
mutate(date_diff = case_when(is.na(date_diff) ~ 9999L, # replace NAs with 999 (cummin does not allow na.rm)
TRUE ~ date_diff)) %>% #
mutate(min_period = cummin(date_diff)) %>% # calculate the cumulative minimum of the differencce
mutate(future_hyperf = min_period < 180) %>% # check the cumulative min is less than 180
mutate(past_hyperf = min(min_period) < 180) %>%
ungroup()
## # A tibble: 14 x 7
## pacient date lag_date date_diff min_period future_hyperf past_hyperf
## <dbl> <date> <date> <int> <int> <lgl> <lgl>
## 1 10 2018-01-01 NA 9999 9999 FALSE TRUE
## 2 10 2018-05-02 NA 9999 9999 FALSE TRUE
## 3 10 2018-06-04 2018-01-01 154 154 TRUE TRUE
## 4 10 2018-11-10 2018-05-02 192 154 TRUE TRUE
## 5 10 2018-12-05 2018-06-04 184 154 TRUE TRUE
## 6 11 2018-01-02 NA 9999 9999 FALSE FALSE
## 7 11 2018-08-06 NA 9999 9999 FALSE FALSE
## 8 12 2018-01-01 NA 9999 9999 FALSE TRUE
## 9 12 2018-01-03 NA 9999 9999 FALSE TRUE
## 10 12 2018-03-06 2018-01-01 64 64 TRUE TRUE
## 11 13 2018-08-05 NA 9999 9999 FALSE FALSE
## 12 13 2019-08-05 NA 9999 9999 FALSE FALSE
## 13 15 2019-07-05 NA 9999 9999 FALSE FALSE
## 14 14 2017-07-08 NA 9999 9999 FALSE FALSE https://stackoverflow.com/questions/55530851
复制相似问题