Reputation: 1144
I have discogs data about artists who perform on jazz albums and would like to create network maps of these individuals. Sample data are provided below. I need to compute all possible pairs of artists on a given album. To illustrate the desired result, the figure below shows the original data (left side) and how additional rows must be added to achieve a unique set of all possible pairs WITHIN an album. Additional information on role must be retained. In the example shown, there are originally 12 records for three albums. The restructured data will have 31 records and the same columns.
A post here seems similar but deals with data in a different structure.
jnet<-structure(list(leadArtist = c("Milt Jackson", "Milt Jackson",
"Milt Jackson", "Milt Jackson", "Milt Jackson", "Milt Jackson",
"Milt Jackson", "Milt Jackson", "Milt Jackson", "Milt Jackson",
"Milt Jackson", "Milt Jackson"), albumid = c(2460190, 2460190,
2460190, 2460190, 444693, 444693, 444693, 3019083, 3019083, 3019083,
3019083, 3019083), extraArtists = c("Sahib Shihab", "Art Blakey",
"Horace Silver", "Joe Newman", "Steve Novosel", "Vinnie Johnson",
"Johnny O'Neal", "Percy Heath", "Lawrence Marable", "Skeeter Best",
"John Lewis (2)", "Lucky Thompson"), role = c("Baritone Saxophone",
"Drums", "Piano", "Trumpet", "Bass", "Drums", "Piano", "Bass",
"Drums", "Guitar", "Piano", "Tenor Saxophone")), row.names = c(NA,
-12L), class = c("tbl_df", "tbl", "data.frame"))
Upvotes: 0
Views: 66
Reputation: 37641
Here is at least one way.
Pairs = matrix("", nrow=0, ncol=2)
for(AID in unique(jnet$albumid)) {
Selector = jnet$albumid == AID
Artists = unique(c(jnet$leadArtist[Selector],
jnet$extraArtists[Selector]))
Pairs = rbind(Pairs, t(combn(Artists, 2)))
}
head(Pairs)
[,1] [,2]
[1,] "Milt Jackson" "Sahib Shihab"
[2,] "Milt Jackson" "Art Blakey"
[3,] "Milt Jackson" "Horace Silver"
[4,] "Milt Jackson" "Joe Newman"
[5,] "Sahib Shihab" "Art Blakey"
[6,] "Sahib Shihab" "Horace Silver"
Upvotes: 1