Skip to content

Commit 2ec482f

Browse files
author
hornik
committed
Tweaks.
git-svn-id: https://svn.r-project.org/R/trunk@87721 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 5da03b2 commit 2ec482f

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/library/base/R/interaction.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ interaction <- function(..., drop = FALSE, sep = ".", lex.order = FALSE)
4545
lvs <- paste(rep(l, length(lvs)),
4646
rep(lvs, each = length(l)), sep=sep)
4747
}
48-
if(anyDuplicated(lvs)) { ## fix them up
49-
ulvs <- unique(lvs)
50-
while((i <- anyDuplicated(flv <- match(lvs, ulvs)))) {
51-
lvs <- lvs[-i]
52-
ans[ans+1L == i] <- match(flv[i], flv[1:(i-1)]) - 1L
53-
ans[ans+1L > i] <- ans[ans+1L > i] - 1L
54-
}
55-
lvs <- ulvs
48+
while(j <- anyDuplicated(lvs)) {
49+
## If levels at positions i and j > i are the same, we
50+
## need to drop the one at j, change the code for that
51+
## level to the code for level i, and decrease all codes
52+
## beyond the code for level j by one.
53+
i <- match(lvs[j], lvs)
54+
lvs <- lvs[-j]
55+
j <- j - 1L
56+
ans[ans == j] <- i - 1L
57+
ans[ans > j] <- ans[ans > j] - 1L
5658
}
5759
if(drop) {
5860
olvs <- lvs

0 commit comments

Comments
 (0)