## Suggestion [General Sta­tis­tics]

Hi Shuanghe,

» # remove subjects who has imcomplete periods. More elegant solution?» library(dplyr)

Didn’t know this package. The syntax within some functions (i.e., the %>%-operator) is – well – un­con­ven­tio­nal.

Code to keep only subjects completing all four periods:

summary(ema[2:4])                # show structure per.4  <- summary(ema$subj) == 4 # subjs have 4 levels? incomp <- per.4[per.4 == FALSE] # incomplete ones print(sort(names(incomp))) comp <- per.4[per.4 == TRUE] # complete ones print(sort(names(comp))) d <- subset(ema, subj %in% names(comp)) print(d) summary(d[2:4]) seq1 <- as.numeric(summary(d[, 3])[names(summary(d[, 3]))]) seq2 <- as.numeric(summary(d[, 3])[names(summary(d[, 3]))]) if(seq1 == seq2) { txt <- "Balanced: " } else { txt <- "Unbalanced: " } cat(paste0(txt, names(summary(d[, 3])), "=", seq1, ", ", names(summary(d[, 3])), "=", seq2, "\n")) Lines in red can be dropped (checking results only). Which subjects are incomplete? print(sort(names(incomp))) gives  "11" "20" "24" "31" "42" "67" "69" "71" summary(d[2:4]) gives  per seq treat 1:69 RTRT:144 R:138 2:69 TRTR:132 T:138 3:69 4:69 and the goody Unbalanced: RTRT=144, TRTR=132 The code works for fancy non-numeric subject-IDs and other sequence-codings as well. Try ema$subj <- as.factor(paste0("Sub #", ema$subj)) levels(ema$seq)[levels(ema$seq)=="RTRT"] <- 1 levels(ema$seq)[levels(ema$seq)=="TRTR"] <- 2 levels(ema$treat)[levels(ema$treat)=="T"] <- "A" levels(ema$treat)[levels(ema\$treat)=="R"] <- "B"

and run the code. I got for print(sort(names(incomp)))

 "Sub #11" "Sub #20" "Sub #24" "Sub #31" "Sub #42" "Sub #67" "Sub #69"  "Sub #71

and finally for summary(d[2:4])

 per    seq     treat  1:69   1:144   B:138  2:69   2:132   A:138  3:69         4:69

Same as above (apart from differing sequences and treatments). Edit: Once I saw Detlew’s code below, I can only say about my snippet: Forget it!

Dif-tor heh smusma 🖖
Helmut Schütz The quality of responses received is directly proportional to the quality of the question asked. 🚮
Science Quotes Ing. Helmut Schütz 