rreshape2

Reshape data frame by row


I have a data frame similar to the following example:

> df <- data.frame(imp = c("Johny", "Johny", "Lisa", "Max"), item = c(5025, 1101, 2057, 1619))
> df
     imp     item  
[1,] "Johny" "5025"
[2,] "Johny" "1101"
[3,] "Lisa"  "2057"
[4,] "Max"   "1619"

I would like to have an unique row for each user. The final result should be something like this:

> df
     imp     item1  item2 
[1,] "Johny" "5025" "1101"
[2,] "Lisa"  "2057" NA    
[3,] "Max"   "1619" NA

Solution

  • ## Add an ID column to distinguish multiple measurements per imp
    ## There's probably a better way to do this?
    df <- do.call(rbind, lapply(
        split(df, df$imp),
        function(x) {
            x$item_id <- seq(nrow(x))
            return(x)
        }
    ))
    
    ## Then simply use the dcast function from the reshape2 package
    df <- dcast(df, imp ~ item_id, value.var='item')
    
    ## Tidy up the column names
    names(df) <- sub('^(\\d+)$', 'item_\\1', names(df))