#18july2020 ###################################### ### edgelist to nodelist function #### ###################################### # Function is designed to convert many rows to one based on a common id. # Function will isolate two cols of data from a larger dataframe. One must be the the common id, # the other is the variable. For network data, this is know as converting an edgelist to a nodelist; # or, two columns of N1 and N2 to rows with ego and alter(s). # The function takes 5 arguments: the name of the dataframe, the two columns of data to isolate (id and variable respectively; or N1 and N2 for network data), and the desired column names for the output (idName and variableName, respectively). Default output names are "id" and "v". Each variable column (or alter column in the case of network data) will also be numbered 1 to N (e.g., v1, v2, etc.) ############################################### #requires that library(plyr) is installed !!!! ############################################## ### here is some test data #### data<- data.frame( q = c("a","a","a","b","b","c","d","e"), v1 = c(c(1:6),NA, 8), v2 = c(rep(c("dog", "cat"),3),NA, "cat"), v3 = c("TNC", "EPA", "URI", "EPA", "RISDI", "Brown", NA, "snep") ) ; data ############################## ### here is the function #### ############################# edgeToNodeList<-function (data, id, variable, idName="id", variableName="v"){ dataOut<-NULL temp<-data[, c(id, variable)] colnames(temp)<-c("id", "v") #specify col names for subset for (i in unique(temp[[1]])) { temp2<-subset(temp, id == i) newRow<-data.frame(id = i, v = t(temp2[[2]])) #make sure a single entry case gets assigned to first col if(ncol(newRow)==2) {colnames(newRow)<-c("id", "v.1")} dataOut<-plyr::rbind.fill(dataOut, newRow) } colnames(dataOut)<-c(idName, paste0(variableName, 1:(ncol(dataOut)-1))) #assign user defined names return(dataOut) } ############################ ### now try the function ### ############################ edgeToNodeList(data, id="q", variable="v3", idName="ego", variableName = "alter")