【发布时间】: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