# Re: [R] Compact Indicator Matrices

From: amarkos <amarkos_at_gmail.com>
Date: Sun, 11 May 2008 07:49:47 -0700 (PDT)

> Do you mean that you want to collapse similar rows into a single row > and perhaps a count of the number of times that this row occurs?

Let me rephrase the problem by providing an example.

Input:

A =

nocases <- dim(obj)[1]
novars <- dim(obj)[2]

# variable levels
levels.n <- sapply(obj, nlevels)
n <- cumsum(levels.n)

# Indicator matrix calculations

```Z        <- matrix(0, nrow = nocases, ncol = n[length(n)])
newdat   <- lapply(obj, as.numeric)
offset   <- (c(0, n[-length(n)]))
```

for (i in 1:novars)
Z[1:nocases + (nocases * (offset[i] + newdat[[i]] - 1))] <- 1

#######

Output:

Z =

Z is an indicator matrix in the Multiple Correspondence Analysis framework.
My problem is to collapse identical rows (e.g. 2 and 9) into a single row and
store the row ids.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 11 May 2008 - 18:03:33 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 12 May 2008 - 14:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.