首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套循环遍历r中的行/列

嵌套循环遍历r中的行/列
EN

Stack Overflow用户
提问于 2015-07-30 21:04:33
回答 1查看 455关注 0票数 0

我有一个数据集,如下所示:

代码语言:javascript
复制
1. ID   RESULT_DATE Hyperkalemia    Is.Hemolyzed
2. 1    5/27/2008   2   FALSE
3. 1    5/28/2008   2   FALSE
4. 1    5/29/2008   2   FALSE
5. 1    5/29/2008   2   FALSE
6. 1    5/29/2008   3   FALSE
7. 1    5/30/2008   2   FALSE
8. 1    6/15/2008   4   FALSE
9. 1    10/14/2014  1   FALSE
10. 1   10/16/2014  NA  FALSE
11. 2   8/12/2013   2   FALSE
12. 3   2/26/2012   2   FALSE
13. 3   2/27/2012   2   FALSE
14. 3   4/18/2012   3   FALSE
15. 3   4/18/2012   4   FALSE
16. 3   4/21/2012   4   FALSE
17. 3   4/23/2012   4   FALSE
18. 3   4/27/2012   4   FALSE
19. 3   5/8/2012    4   FALSE
20. 3   5/12/2012   4   FALSE
21. 3   5/15/2012   4   FALSE
22. 3   5/15/2012   NA  FALSE

我要找出钾试验的次数,高血钾评分为3或4分,同一天重复一次is.HEmolyzed = FALSE (必须按病人ID计数重复次数),客观的是试验合格重复的总次数,然后是重复的总次数。

有人能帮我把我的伪码翻译成R码吗?

代码语言:javascript
复制
    # data.frame = pots
    # for every row  (sorted by patient and result date) 
   for (i in 1:nrow(pots){  

      # for each patient (sorted by result date)
        # how do I do I count the rows for the individual patient?
        for (i in 1:length(pots$ID)) {

          # assign result date to use for calculation
          result_date = pots$result_date

          # if Hyperkalemia = 3 or 4
          if (Hyperkalemia == 3 | Hyperkalemia == 4)

            # go find the next result for patient where is.Hemolyzed = FALSE
            # how do I get the next result?
            for (i+1)

             # assign date to compare to first date
              next_result_date = pots$result_date
              if next_result_date > result_date 
                    then repeated_same_day <- FALSE 
                else if result_date == result_date
                      then repeated_same_day <- TRUE

        }
    }

目标:我想计算一个3或4级的非溶血钾结果在24小时内进行另一次钾测试的频率(我现在使用的是不同的字段--我想我可以添加一些日期函数来计算24小时)。

编辑:我终于让它和for循环一起工作了!!分享,以防对任何人都有帮助。后来,我确实看到了一个错误,但对于我的数据集,这是可以的。

代码语言:javascript
复制
library(dplyr)
pots <- read.csv("phis_potassium-2015-07-30.csv",  
                      head=TRUE,  stringsAsFactors = FALSE)

pots <- arrange(pots, MRN, COLLECTED_DATE)

pots$Hyperkalemia[is.na(pots$Hyperkalemia)] <- 0
pots$repeated_wi24hours <- NA
pots$met_criteria <- NA
pots$next_test_time_interval <- NA

# data.frame = pots
# for every patient  (sorted by patient and collected date) 
for (mrn in unique(pots$MRN)){ 
   # for each row for each patient (sorted by collected date)
     for (i in 1:length(pots$MRN[pots$MRN == pots$MRN[mrn]])) {
          # if Hyperkalemia = 3 or 4 AND Is.Hemolyzed == FALSE
         if((pots$Hyperkalemia[i] == 3 | pots$Hyperkalemia[i] == 4) & pots$Is.Hemolyzed[i] == FALSE){
           pots$met_criteria[i] <- TRUE
           # get time interval between tests
           pots$next_test_time_interval[i] <- difftime(pots$COLLECTED_DATE[i+1], pots$COLLECTED_DATE[i], units = "hours")
           # if next date is within 24 hours, then test repeated 
           if (pots$next_test_time_interval[i] <= 24 ){
                                pots$repeated_wi24hours[i] <- TRUE

               }
                else {
                  pots$repeated_wi24hours[i] <- FALSE
                }
}
}
}

期望输出

代码语言:javascript
复制
ID  RESULT_DATE Hyperkalemia    Is.Hemolyzed    Met_criteria    Repeated
1   5/27/2008              2           FALSE        
1   5/28/2008              2           FALSE        
1   5/29/2008              2           FALSE        
1   5/29/2008              2           FALSE        
1   5/29/2008              3           FALSE    TRUE               FALSE

1   5/30/2008              2           FALSE        
1   6/15/2008              4           FALSE        
1   10/14/2014             1           FALSE        
2   8/12/2013              2           FALSE        
3   2/26/2012              2           FALSE        
3   2/27/2012              2           FALSE        
3   4/18/2012              3           FALSE    TRUE               TRUE

3   4/18/2012              4           FALSE    TRUE               FALSE

3   4/21/2012              4           FALSE    TRUE               FALSE
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-30 23:09:17

这个怎么样:

代码语言:javascript
复制
metCriteria <- function( dfPots )
{
  (dfPots$Hyperkalemia==3 | dfPots$Hyperkalemia==4) & !dfPots$Is.Hemolyzed
}

#----------------------------------------------------------------------

pots <- read.table(filename, header=TRUE)

d <- paste( as.character(pots$RESULT_DATE),
            "_ID",
            as.character(pots$ID))

lastOccurence <- unlist(lapply(d,function(x){which.min(diff(c(d,FALSE)==x))}))

pots <- cbind(pots, data.frame( Met_criteria = rep(FALSE,nrow(pots))),
                                Repeated     = rep(TRUE ,nrow(pots))   )

pots$Repeated[lastOccurence]                <- FALSE
pots$Met_criteria[which(metCriteria(pots))] <- TRUE

日期和ID被粘贴在向量"d“中。向量"lastOccurence“的第一个组件是发生日期/ID对di的行号或最后一次。

数据框架"pots“由"Met_criteria”和“重复”两列进行扩展。

  • "Met_criteria“被初始化为"FALSE”。然后“哪个(metCriteria(Pots))”选择符合条件的行号。在这些行中,"Met_critaria“设置为"TRUE”。
  • “重复”被初始化为"TRUE“。在上次发生相应日期和ID的行中,它被设置为"FALSE“。

示例:

代码语言:javascript
复制
> pots
   ID RESULT_DATE Hyperkalemia Is.Hemolyzed Met_criteria Repeated
1   1   5/27/2008            2        FALSE        FALSE    FALSE
2   1   5/28/2008            2        FALSE        FALSE    FALSE
3   3   5/28/2008            2        FALSE        FALSE    FALSE
4   1   5/29/2008            2        FALSE        FALSE     TRUE
5   1   5/29/2008            2        FALSE        FALSE     TRUE
6   1   5/29/2008            3        FALSE         TRUE    FALSE
7   2   5/29/2008            4        FALSE         TRUE    FALSE
8   1   5/30/2008            2        FALSE        FALSE    FALSE
9   1   6/15/2008            4        FALSE         TRUE    FALSE
10  1  10/14/2014            1        FALSE        FALSE    FALSE
11  1  10/16/2014           NA        FALSE        FALSE    FALSE
12  2   8/12/2013            2        FALSE        FALSE    FALSE
13  3   2/26/2012            2        FALSE        FALSE    FALSE
14  3   2/27/2012            2        FALSE        FALSE    FALSE
15  3   4/18/2012            3        FALSE         TRUE     TRUE
16  3   4/18/2012            4        FALSE         TRUE    FALSE
17  3   4/21/2012            4        FALSE         TRUE    FALSE
18  3   4/23/2012            4        FALSE         TRUE    FALSE
19  3   4/27/2012            4        FALSE         TRUE    FALSE
20  3    5/8/2012            4        FALSE         TRUE    FALSE
21  3   5/12/2012            4        FALSE         TRUE    FALSE
22  3   5/15/2012            4        FALSE         TRUE     TRUE
23  3   5/15/2012           NA        FALSE        FALSE    FALSE
> 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31734025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档