Search code examples
rdataframeinversion

Inverting Dataframe in R


Here as an example. Participants rank their favorite colors between green, blue and red. id represent participants. Each participant ranked three colors from 1-3(best=1, second favorite=2, least favorite=3). Imagine the data looks like this:

         id1      id2     id3
 rank1   red     green    blue
 rank2   green   blue     red
 rank3   blue    red      green

I need to change the values so that it looks like this:

        id1   id2    id3
 green   2      1     3
 blue    3      2     1
 red     1      3     2 

Essentially, I want to create a row with the color and record its ranking. My actual dataframe is 25 columns x 100 rows. I am doing this because data entry is easier in version 1.

What is the easist way to change the data?


Solution

  • sapply a match to each column of your dataframe:

    # example data
    df <- data.frame(
        id1 = c("red", "green", "blue"),
        id2 = c("green", "blue", "red"),
        id3 = c("blue", "red", "green"),
        stringsAsFactors = FALSE
    )
    
    # create ranking dataframe
    sapply(df, match, x=c("green", "blue", "red"))
    

    Result:

      id1 id2 id3 
    1   2   1   3 
    2   3   2   1 
    3   1   3   2