【问题标题】:Populating a variable based on the total of each level根据每个级别的总和填充变量
【发布时间】:2020-04-23 14:56:51
【问题描述】:

我正在为我目前在项目中遇到的问题寻求帮助(文本后的表示)。

基本上,我要做的是根据患者在一周内记录数据的次数,用水平标准填充一个变量,以探索记录质量。

等级标准如下:

3+ readings/week == "4",
3 readings/week == "3",
2 readings/week == "2",
1 reading/week == "1",
NA == "0"

我首先使用 lubridate 的 week() 函数创建了一个新的变量周数,它根据日期在一年中的位置为我提供了周数。理想情况下,我想按升序 (1-n) 分配周数,从患者记录的第一个日期到最后一个日期从 1 开始。

一直在考虑使用 for 循环,但目前使用的是 case_when。我目前遇到的问题是设置条件以检查每个患者 ID 的级别频率,然后分配标准。

任何帮助都是有益的,因为我今天大部分时间都在坚持,非常感谢(下面的代表)。

library(lubridate)
#> Warning: package 'lubridate' was built under R version 3.5.3
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.5.3
#> Warning: package 'ggplot2' was built under R version 3.5.3
#> Warning: package 'tibble' was built under R version 3.5.3
#> Warning: package 'tidyr' was built under R version 3.5.3
#> Warning: package 'readr' was built under R version 3.5.3
#> Warning: package 'purrr' was built under R version 3.5.3
#> Warning: package 'dplyr' was built under R version 3.5.3
#> Warning: package 'stringr' was built under R version 3.5.3
#> Warning: package 'forcats' was built under R version 3.5.3

##Variables##

patientid <- c("-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", 
               "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646", "-2147483646")

date <- c("2018-08-06", "2018-08-07", "2018-08-07", "2018-08-07", "2018-08-15", "2018-08-15", "2018-08-15", "2018-08-20", "2018-08-20",
          "2018-08-20", "2018-08-27", "2018-08-27", "2018-08-27", "2018-09-03", "2018-09-03", "2018-09-03")

week <- week(date)

adherence <- ""

test.df <- data.frame(patientid, date, week, adherence) #test df with variables above

##Dataframe and attempt##

table(test.df$week) #See frequency of each
#> 
#> 32 33 34 35 36 
#>  4  3  3  3  3

test.df <- test.df %>% #Dataframe
  mutate(
    patientid = as.factor(patientid),
    date = as.Date(date),
    week = as.factor(week))

adherence <- test.df %>% #Attempt to create if/else/else if loop to populate adherence
  mutate(week = 
           if(count(week) > 3){adherence == "4"})
#> Error in UseMethod("summarise_"): no applicable method for 'summarise_' applied to an object of class "factor"

【问题讨论】:

    标签: r dataframe tidyverse lubridate


    【解决方案1】:

    我认为这是您想要的版本:

    test.df <- data.frame(patientid, date) #test df with variables above
    
    test.df %>% #Dataframe
      mutate(
        patientid = as.factor(patientid),
        date = as.Date(date),
        week = floor_date(date, "week")
      ) %>% 
      group_by(patientid, week) %>% 
      summarize(total_readings = n(),
             adherence = case_when(is.na(total_readings) ~ 0L,
                                   total_readings < 4 ~ total_readings,
                                   total_readings >= 4 ~ 4L,
                                   TRUE ~ NA_integer_))
    
    

    唯一没有考虑的是周排序。

    【讨论】:

      【解决方案2】:

      您可以使用add_count 计算每周的实例数。对于pmin,我们选择介于 4 和 n 之间的最小值。

      library(dplyr)
      test.df %>% add_count(week) %>% mutate(week_num = pmin(4, n))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-07
        • 1970-01-01
        • 2021-05-28
        • 2012-12-17
        • 1970-01-01
        • 2018-08-14
        • 1970-01-01
        • 2011-05-14
        相关资源
        最近更新 更多