Create block diagonal data frame in R -
i have info set looks this:
person team 114 1 115 1 116 1 117 1 121 1 122 1 123 1 214 2 215 2 216 2 217 2 221 2 222 2 223 2
"team" ranges 1 33, , teams vary in terms of size (i.e., there can 5, 6, or 7 members, depending on team). need create info set looks this:
1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1
the sizes of individual blocks given number of people in team. how can in r?
another idea, although, guess less efficient/elegant rstudent's:
df = data.frame(person = sample(100, 21), team = rep(1:5, c(3,6,4,5,3))) df lengths = tapply(df$person, df$team, length) mat = matrix(0, sum(lengths), sum(lengths)) mat[do.call(rbind, mapply(function(a, b) arrayind(seq_len(a ^ 2), c(a, a)) + b, lengths, cumsum(c(0, lengths[-length(lengths)])), simplify = f))] = 1 mat
r
No comments:
Post a Comment