Analysing data with repeated measure variable

I have following type of repeated measures data (table 1), and events data in table 2 (single measure). I want to perform following tasks (in R or excel sheet please).

  • To filter subjects who had any response at least 3 days.
  • Response should be > 5 in each day.
  • Then table 1 should have another column, date first response recorded.
  • Then both tables should be merged

Table 1 – Response Data

Sub_No Response Date
1 5 01-Jan
1 5 02-Jan
2 5 01-Jan
2 10 02-Jan
2 10 03-Jan
2 10 04-Jan
2 10 05-Jan
3 10 01-Jan
3 10 02-Jan
3 10 03-Jan
4 5 01-Jan
4 5 02-Jan
4 10 03-Jan
4 10 04-Jan
4 10 05-Jan


Table 2 – Event Data

Sub_No Response Date
1 No
2 Yes 30 Jan
3 Yes 29 Jan
4 No


Thanks for your help.

This problem illustrates the difference between an object, such as a data frame, that is intended to store data in a form that permits analysis and another object that is intended to present various aspects of data.

There is not necessarily a simple mapping from one to another. It's not difficult to extract the pieces. How to put them back together isn't clear from the problem description.

suppressPackageStartupMessages({
  library(dplyr)
})

dat <- data.frame(
  x = c(1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4),
  Sub_No = c(5, 5, 5, 10, 10, 10, 10, 10, 10, 10, 5, 5, 10, 10, 10),
  Response_Date = c("01-Jan", "02-Jan", "01-Jan", "02-Jan", "03-Jan", "04-Jan", "05-Jan", "01-Jan", "02-Jan", "03-Jan", "01-Jan", "02-Jan", "03-Jan", "04-Jan", "05-Jan"))

dat %>% group_by(x) %>% count() -> counts
include <- which(counts$n > 2)
dat %>% filter(x %in% include & Sub_No > 5)
#>    x Sub_No Response_Date
#> 1  2     10        02-Jan
#> 2  2     10        03-Jan
#> 3  2     10        04-Jan
#> 4  2     10        05-Jan
#> 5  3     10        01-Jan
#> 6  3     10        02-Jan
#> 7  3     10        03-Jan
#> 8  4     10        03-Jan
#> 9  4     10        04-Jan
#> 10 4     10        05-Jan
dat %>% filter(x %in% include & Sub_No > 5) %>% group_by(x) %>% slice_head(n = 1)
#> # A tibble: 3 x 3
#> # Groups:   x [3]
#>       x Sub_No Response_Date
#>   <dbl>  <dbl> <chr>        
#> 1     2     10 02-Jan       
#> 2     3     10 01-Jan       
#> 3     4     10 03-Jan

Much appreciated your time and help. I will try on actual data.

Cheers.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.