Replace a value in a dataframe with the row name

Viewed 2777

I have a data frame that looks like this:

         Jill Jimmie Alex Jane 
    Jill   1    0      1    1    
    Jimmie 0    1      1    0    
    Alex   0    1      1    0    
    Jane   1    1      1    0    

I want to change every 1 to its corresponding row name, such as this:

         Jill Jimmie Alex Jane 
    Jill Jill    0   Jill  Jill  
    Jimmie 0  Jimmie Jimmie 0   
    Alex   0   Alex  Alex   0   
    Jane Jane  Jane  Jane   0  

After that, I want to remove all zeros from the data frame and move up the values in the columns.

I've tried:

    # for (i in ibm_data){
    #   if (ibm_data == 1){
    #     names <- row.names(i)
    #     ibm_data[ibm_data == 1] <- names
    #   }
    #   else{
    #     ibm_data[ibm_data == 0] <- "NA"
    #   }
    # }

And then I would remove by NA values, but I think I am making this overly complicated. I plan to build a forced graph from the list of names to see correlations.

3 Answers
Related