HAVE
is an edge-list for an odd two mode network. People in actor
are major award-winning actors; people in supporter
are other actors who performed in one or more films
as a supporting role. Note: actor
includes some members of supporter
but not all, which is why I am treating this as two-mode data.
index actor supporter films
1 f1 f4 2
2 f1 f2 1
3 f1 f7 6
4 f1 f5 5
5 f2 f6 6
6 f2 f3 3
7 f3 f1 9
8 f3 f4 1
9 f3 f2 4
WANT
is a two-mode adjacency matrix made from HAVE
- row names represent all ids in actor
; column names include every name in both actor
and supporter
.
f1 f2 f3 f4 f5 f6 f7
f1 0 1 0 2 5 0 6
f2 0 0 3 0 0 6 0
f3 9 4 0 1 0 0 0
How do I transform HAVE
to WANT
? This process is complicated by the fact that (a) each mode has ids with an identical naming scheme and (b) asymmetry (eg, f2 led and f1 supported once, but f1 never led in a film where f2 supported).
I think you can just use your favorite reshape
r function:
dat <- read.table(text ="index actor supporter films
1 f1 f4 2
2 f1 f2 1
3 f1 f7 6
4 f1 f5 5
5 f2 f6 6
6 f2 f3 3
7 f3 f1 9
8 f3 f4 1
9 f3 f2 4", header=TRUE)
adj <- reshape(dat[,-1], v.names = "films", idvar = "actor",
timevar = "supporter", direction = "wide")
adj[is.na(adj)] <- 0
adj[,order(colnames(adj))]
# actor films.f1 films.f2 films.f3 films.f4 films.f5 films.f6 films.f7
# 1 f1 0 1 0 2 5 0 6
# 5 f2 0 0 3 0 0 6 0
# 7 f3 9 4 0 1 0 0 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.