подсчитать количество значений в одном столбце, если значение в другом столбце, соответствующее этой строке, является первым вхождением динамически в течение двух лет - PullRequest
0 голосов
/ 27 марта 2019

Данные включают три столбца: идентификатор врача, идентификатор медсестры, дата операции.Я хочу подсчитать количество опыта каждого врача, работающего с разными медсестрами в течение двух лет.Например, у меня есть этот фрейм данных:

Physician ID      & nurse ID    & time        
0011             0003         2018-6-1       
0011             0003         2017-6-1        
0011             0003         2008-6-1        
0011             0004         2018-6-1        
0011             0004         2017-6-1        
0012             0003         2008-6-1        
0012             0003         2009-6-1        
0012             0003         2008-6-1        

Я не знаю, как охарактеризовать уникальных медсестер, поэтому я могу динамически рассчитывать опыт хирурга только в течение двух лет.

    for (i in 1:nrow(df2))
{


  df2$e_s_new[i] = sum (df0$n

                       [
                         df0$SurgicalAttending_1_ID == df2$SurgicalAttending_1_ID[i]

                         & between(df0$Anesthesia_Start, 
                                   df2$Anesthesia_Start[i]-365*24*60*60, 
                                   df2$Anesthesia_Start[i]-1)])



}

Результат, который я ожидал:

Physician ID    &  nurse ID     & time       & count
0011              0003         2018-6-1     1  
0011              0003         2017-6-1     0 
0011              0003         2008-6-1     0 
0011              0004         2018-4-1     1 
0011              0004         2018-9-1     2 
0012              0003         2008-6-1     0 
0012              0003         2009-6-1     1 
0012              0003         2009-7-1     1

Ответы [ 2 ]

0 голосов
/ 07 апреля 2019

Если в вопросе предлагается добавить столбец с количеством отдельных медсестер, с которыми врач в каждом ряду работал до даты текущей строки, но не более чем за 2 года до этого, он может быть реализован как сложный левый присоединиться. Мы также добавили столбец, в котором указаны идентификаторы медсестер, с которыми работал врач за последние два года. Ввод в воспроизводимой форме показан в конце примечания.

library(sqldf)
sqldf("select a.*, 
              count(distinct b.nurse_ID) as nurses, 
              group_concat(distinct b.nurse_ID) as nurse_IDs
  from DF a
  left join DF b on b.time between a.time - 2 * 365 and a.time - 1 and
                    a.Physician_ID = b.Physician_ID
  group by a.rowid
  order by a.rowid")

дает:

  Physician_ID nurse_ID       time nurses nurse_IDs
1         0011     0003 2018-06-01      2 0003,0004
2         0011     0003 2017-06-01      0      <NA>
3         0011     0003 2008-06-01      0      <NA>
4         0011     0004 2018-06-01      2 0003,0004
5         0011     0004 2017-06-01      0      <NA>
6         0012     0003 2008-06-01      0      <NA>
7         0012     0003 2009-06-01      1      0003
8         0012     0003 2008-06-01      0      <NA>

Примечание

Lines <- 
"Physician_ID nurse_ID             time        
0011             0003         2018-6-1       
0011             0003         2017-6-1        
0011             0003         2008-6-1        
0011             0004         2018-6-1        
0011             0004         2017-6-1        
0012             0003         2008-6-1        
0012             0003         2009-6-1        
0012             0003         2008-6-1"
DF <- read.table(text = Lines, header = TRUE, colClasses = "character",
  strip.white = TRUE)
DF$time = as.Date(DF$time)
0 голосов
/ 06 апреля 2019

Я решаю эту проблему следующим образом:

#count AnesthesiaAttending_1_ID experience with new surgeon



#add case id
temp <- sqldf("select SurgicalAttending_1_ID, Anesthesia_Start, AnesthesiaAttending_1_ID from df group by AnesthesiaAttending_1_ID, Anesthesia_Start")


anes_name <- sqldf("select distinct AnesthesiaAttending_1_ID from temp")
time_ind <- sqldf("select distinct Anesthesia_Start from temp")
experience_doc <- rep(0,length(anes_name$AnesthesiaAttending_1_ID) * length(time_ind$Anesthesia_Start))
time_ind$Anesthesia_Start<-as.POSIXct(time_ind$Anesthesia_Start, format ="%Y-%m-%d %H:%M:%OS")
temp$Anesthesia_Start<-as.POSIXct(temp$Anesthesia_Start, format ="%Y-%m-%d %H:%M:%OS")
temp$anesPast <- temp$Anesthesia_Start - 730*24*60*60
temp$Anesthesia_Start<-as.character(temp$Anesthesia_Start)
temp$anesPast<-as.character(temp$anesPast)



temp$anes_experience = rep(NA, nrow(temp))

for (k in 1:nrow(temp)){
  i <- temp[k, c("AnesthesiaAttending_1_ID")]
  date_j <- temp[k, c("Anesthesia_Start")]
  date_past <- temp[k, c("anesPast")]
  query <-  paste('select distinct SurgicalAttending_1_ID from temp where AnesthesiaAttending_1_ID=',toString(i), " and Anesthesia_Start<'", date_j, "' and Anesthesia_Start>='", date_past, "'", sep = "")
  temp_table <- sqldf(query)
  temp[k, c("anes_experience")] <- nrow(temp_table)
}


start<-min(df$Anesthesia_Start)+730*24*60*60
temp<-subset(temp, temp$Anesthesia_Start>as.POSIXct(start))

df2<-merge( df2, temp, by = c("SurgicalAttending_1_ID","AnesthesiaAttending_1_ID","Anesthesia_Start"),
            all.x = TRUE, all.y = TRUE)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...