0

I have an initial survey (demongraphic,code as time 0) and 10 days of diary survey (time 1 - time 10) data. I am trying to merge initial data and diary data together and then add a column to give the time sequence for each participants. For example, I want to code 0 (under the Time column) for each participant in the initial survey. Then code 1 for the first daily survey, 2 for the second daily survey, and so on.

I tried to merge initial data (df1) and repeated diary data (daily) and name it as Total. Then I added the time column as the following:

Total=merge(df1, daily,by="ID", all=F)
Total$time <- ave(seq_along(Total$ID), Total$ID, FUN = function(x) x - min(x))

But it does not seem right because the constructs in the initial survey is different from the daily survey. Is there other ways to do it? Thank you!

Phil
  • 7,287
  • 3
  • 36
  • 66
Shawna
  • 1
  • 1
    Hi Shawna! Welcome to StackOverflow. Would you be able to create a Minimal Reproducible Example? Basically, just provide enough code and data that we can reproduce your problem. Instructions are here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Mark Jun 24 '23 at 06:03

1 Answers1

0

something along these lines?

df1 <- data.frame(ID = 'ID1', name = 'John', age = '31')
> df1
   ID name age
1 ID1 John  31
daily <- data.frame(ID = gl(2, 3, labels = paste0('ID', 1:2)),
                    time = c(3:5, 27:29),
                    value = rnorm(6)
                    )
> daily
   ID time      value
1 ID1    3 -1.4088505
2 ID1    4  0.9160193
3 ID1    5 -0.1912790
4 ID2   27  0.8032832
5 ID2   28  1.8874745
6 ID2   29  1.4738812
library(dplyr)

df1 |>
  left_join(daily, by = 'ID') |>
  group_by(name) |>
  mutate(time = 1 + time - min(time))
# A tibble: 3 x 5
# Groups:   name [1]
  ID    name  age    time  value
  <chr> <chr> <chr> <dbl>  <dbl>
1 ID1   John  31        1 -1.41 
2 ID1   John  31        2  0.916
3 ID1   John  31        3 -0.191
I_O
  • 4,983
  • 2
  • 2
  • 15