# NOT RUN {
## prepare example data
set.seed(9992722L, kind="Mersenne-Twister")
trt.original=gl(5,8)[sample(40)]
## equivalent recoding:
(trt.equivalent=grpDuplicated(trt.original, factor=TRUE))
## check equivalence: should be a permutation matrix:
(table(trt.original, trt.equivalent)!=0)*1
## equivalent recoding based on a design matrix
x.double=model.matrix(~trt.original)
(trt.equivalent=grpDuplicated(x.double, factor=TRUE, MARGIN=1))
## check equivalence: should be a permutation matrix:
(table(trt.original, trt.equivalent)!=0)*1
## check equivalence: recovering the original matrix from unique:
x.uniq.row=unique(x.double, MARGIN=1L)
all.equal(x.double, x.uniq.row[trt.equivalent,], check.attributes=FALSE)
x.uniq.row=unique(x.double, MARGIN=1L, fromLast=TRUE)
all.equal(x.double, x.uniq.row[grpDuplicated(x.double, fromLast=TRUE),], check.attributes=FALSE)
# }
Run the code above in your browser using DataLab