Q:

Knowledge structure to matrix in R

I have a knowledge structure, e.g.  ={{∅},{a},{b},{bc},{abd},{abcd},{abcde}} and want it displayed as a matrix. How can I do that in R? Function blim() in package pks takes a matrix, which requires that I have one.

Falmagne, J.-C., Koppen, M., Villano, M., Doignon, J.-P., & Johannesen, L. (1990). Introduction to knowledge spaces: How to build, test and search them. Psychological Review, 97, 201224. doi:10.1037/0033-295X.97.2.201

A:

An appropriate example of such a matrix or

a state-by-problem indicator matrix representing the knowledge
  structure. An element is one if the problem is contained in the state,
  and else zero

could be the one from the blim documentation:
data(DoignonFalmagne7)
DoignonFalmagne7$K
      a b c d e
00000 0 0 0 0 0
10000 1 0 0 0 0
01000 0 1 0 0 0
11000 1 1 0 0 0
11100 1 1 1 0 0
11010 1 1 0 1 0
11110 1 1 1 1 0
11101 1 1 1 0 1
11111 1 1 1 1 1

Assuming that knowledge structure is supplied as a character vector and the empty set corresponds to "" one could proceed as follows:
kStruct <- c("", "a", "b", "bc", "abd", "abcd", "abcde")
ul <- strsplit(kStruct, "")
cn <- unique(unlist(ul))
K <- t(sapply(ul, function(x) cn %in% x) * 1)
dimnames(K) <- list(apply(K, 1, paste, collapse = ""), cn)
K
      a b c d e
00000 0 0 0 0 0
10000 1 0 0 0 0
01000 0 1 0 0 0
01100 0 1 1 0 0
11010 1 1 0 1 0
11110 1 1 1 1 0
11111 1 1 1 1 1

Alternatively:
cn <- unique(unlist(strsplit(kStruct, "")))
K <- sapply(cn, grepl, kStruct) * 1
rownames(K) <- apply(K, 1, paste, collapse = "")

Also note that, as I can see, the names of rows of columns are not necessary for blim.

