I am trying to create a simple data frame that contains information about what authors and their respective papers. I have a matrix that contains the author IDs as the rows and the paper IDs as the columns. This matrix contains 1s and 0s, where a 1 indicates that the author worked on that paper. For example, if A2P[1,1] == 1, that means that the author with ID 1 worked on the paper with ID 1.
I am trying to convert this matrix into a simple data frame that contains all of these relationships, something that just contains the author IDs and the papers that they worked on. As in,
au_ID P_ID 1 1 1 12 # Author 1 has worked on both paper 1 and 12 2 1 # Author 2 has also worked on paper 1, in addition to papers 2 and 3. 2 2 2 3 ... Here is what I am doing:
list1 <- list() list2 <- list() # Rows are Author IDs # Columns are Paper IDs for (row in 1:nrow(A2P)){ for (col in 1:ncol(A2P)){ if (A2P[row,col] == 1){ list1 <- append(list1, row) list2 <- append(list2, col) } } } authorship["au_ID"] = list1 authorship["P_ID"] = list2 I am having difficulty getting this code to run quickly. It is taking forever to run, going on twenty minutes now. I think it has something to do with appending each row and column value to each of the lists, but I am unsure.
Any help would be greatly appreciated! Thank you so much!