I was looking for help with a problem I'm currently stuck on in a project (reprex after the text).
Basically what I'm trying to do is populate a variable with a criteria of levels, based on the number of times a patient recorded data for the week in order to explore quality of recording.
The criteria for levels is as follows:
3+ readings/week == "4",
3 readings/week == "3",
2 readings/week == "2",
1 reading/week == "1",
NA == "0"
I first created a new variable for weeks by using the week() function of lubridate, which gave me week numbers based on where the dates were in the year. Ideally I'd like to allocate week numbers in ascending order (1-n), starting at 1 from the first date the patient recorded to the last date.
Been thinking of using a for loop but currently using case_when. The problem I'm currently encountering is setting the condition to check the frequency of levels for each patient id in order to then assign the criteria.
Any help would be beneficial as I've been stuck on it for most of today, many thanks (reprex below).
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"