Backlin
Backlin

Reputation: 14862

Gather ragged data frame into key-value columns

I recently discovered how to create ragged data frames using the I function, but are having a hard time integrating them with tidyr, ggplot2 and the rest of the Hadleyverse. More specifically, how do you gather a column containing named vectors into key-value-columns?

Suppose I create a data frame like this

make.vector <- function(length.out){
    x <- sample(9, length.out)
    names(x) <- switch(length.out,
        "Alice",
        c("Bob", "Charlie"),
        c("Dave", "Erin", "Frank"),
        c("Gwen", "Harold", "Inez", "James"))
    x
}
mydf <- data.frame(Game = gl(3, 3, labels=LETTERS[1:3]),
                   Set = rep(1:3, 3),
                   Score = I(lapply(rep(2:4, each=3), make.vector)))

producing

> print(mydf)
  Game Set      Score
1    A   1       8, 3
2    A   2       2, 8
3    A   3       3, 8
4    B   1    1, 5, 4
5    B   2    2, 3, 5
6    B   3    2, 8, 5
7    C   1 7, 2, 3, 4
8    C   2 1, 6, 3, 7
9    C   3 6, 9, 3, 7

The data frame can be manipulated with dplyr and tidyr in a straight forward manner as long as the results are of the expected length.

mydf %>%
    mutate(nPlayers = sapply(Score, length))
mydf %>% 
    group_by(Game) %>%
    summarize(TotalScore = list(Reduce("+", Score)))

However, I cannot figure out how to create multiple rows of result for each original row. Suppose I want to create the following data frame by manipulating mydf:

   Game Set  Player Score
1     A   1     Bob     8
2     A   1 Charlie     3
3     A   2     Bob     2
4     A   2 Charlie     8
5     A   3     Bob     3
6     A   3 Charlie     8
7     B   1    Dave     1
8     B   1    Erin     5
9     B   1   Frank     4
10    B   2    Dave     2
...

The only tool I know for doing so would be the gather function of the tidyr package, but it doesn't seem to play very well with non-atomic data.

mydf %>%
    mutate(Player = lapply(Score, names)) %>%
    gather(P = Player, S = Score)

I guess I could hack together a solution (as done in similar previous questions [1][2]),

cbind(
    mydf[rep(1:nrow(mydf), sapply(mydf$Score, length)),
         c("Game", "Set")],
    data.frame(
        Player = unlist(lapply(mydf$Score, names)),
        Score = unlist(mydf$Score)
    )
)

but I have a feeling I will have a hard time digesting it if look back at the code next week. Is there a "official" or at least smarter way to do this? Otherwise I'll make a general function for it and add to my personal library.

Update

In the light of David's answer below I figured out that the same result can be achieved with dplyr too.

mydf %>%
    group_by(Game, Set) %>%
    do(with(., data.frame(Player = names(unlist(Score)), 
                          Score = unlist(Score))))

#    Game Set  Player Score
# 1     A   1     Bob     8
# 2     A   1 Charlie     6
# 3     A   2     Bob     7
# 4     A   2 Charlie     6
# 5     A   3     Bob     5
# 6     A   3 Charlie     8
# 7     B   1    Dave     1
# 8     B   1    Erin     9
# 9     B   1   Frank     3
# 10    B   2    Dave     8
# ..  ... ...     ...   ...
# Warning message:
# In rbind_all(out[[1]]) : Unequal factor levels: coercing to character

Upvotes: 1

Views: 232

Answers (1)

David Arenburg
David Arenburg

Reputation: 92302

I would try unlisting by group using data.table. You can run this only once per each group while storing it in a temporary variable using curly brackets (as you would do within a function) within the jth expression

library(data.table) 
setDT(mydf)[, {
               temp <- unlist(Score) 
               .(Player = names(temp), Score = temp)
              }, by = .(Game, Set)]

#     Game Set  Player Score
#  1:    A   1     Bob     2
#  2:    A   1 Charlie     9
#  3:    A   2     Bob     6
#  4:    A   2 Charlie     3
#  5:    A   3     Bob     2
#  6:    A   3 Charlie     8
#  7:    B   1    Dave     1
#  8:    B   1    Erin     6
#  9:    B   1   Frank     5
# 10:    B   2    Dave     3
#...

Upvotes: 5

Related Questions