53

I'm looking for a quick way to get back and forth between a list of the following format:

$`a`
  [1] 1 2 3
$`b`
  [1] 4 5 6

to/from a data.frame of the following format:

   name x
 1    a 1
 2    a 2
 3    a 3
 4    b 4
 5    b 5
 6    b 6

(Don't really care what the names of the columns are, in this case.)

Here's the data frame used above in R-format:

df <- data.frame(name=c(rep("a",3),rep("b",3)), x=c(1:3,4:6))

Again, I'm looking for two separate operations: one to convert the above data.frame to a list, and another to convert it back to a data.frame.

Jeff Allen
  • 17,277
  • 8
  • 49
  • 70

4 Answers4

66

Use stack and unstack in base R:

x <- data.frame(a=1:3, b=4:6)

x
  a b
1 1 4
2 2 5
3 3 6

Use stack to from wide to tall, i.e. stack the vectors on top of one another.

y <- stack(x)
y
  values ind
1      1   a
2      2   a
3      3   a
4      4   b
5      5   b
6      6   b

Use unstack to do the reverse.

unstack(y)
  a b
1 1 4
2 2 5
3 3 6

If your data structure is more complicated than you described, stack and unstack may no longer be suitable. In that case you'll have to use reshape in base R, or melt and dcast in package reshape2.

Andrie
  • 176,377
  • 47
  • 447
  • 496
16

Another option is enframe from tibble

library(tidyverse)
enframe(lst1) %>%
   unnest

data

lst1 <- list(a=1:3, b=4:6)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
11

Maybe something like:

X <- split(df$x, df$name)
data.frame(name = rep(names(X), sapply(X, length)), 
    x=do.call('c', X))

EDIT: I decided to combine Andrie and I's solution into one that appears to be exactly what the OP asked for fairly simple. That being said I don't quite understand a situation where I would treat the data this way instead of how Andrie did it since a data frame is a list of equal length vectors anyway.

# Your data set
df <- data.frame(name=c(rep("a",3),rep("b",3)), x=c(1:3,4:6))

# converting it to list of vectors
X <- split(df[, 2], df[, 1])
# converting it to a dataframe
Y <- stack(X)[, 2:1]; names(Y) <- names(df)

# Take Y and feed it back to these lines to show it 
# switches back and forth
(X <- split(Y[, 2], Y[, 1]))
Y <- stack(X)[, 2:1]; names(Y) <- names(df);Y
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • +1 I was thinking maybe `melt(as.data.frame(...),value.name = 'x',variable.name = 'name')` to go back to the data frame. – joran May 03 '12 at 14:20
  • One should note that split reorders the data frame as it builds a factor of the second vector. See also [Creating a named list from two vectors (names, values)](http://stackoverflow.com/questions/17842705/creating-a-named-list-from-two-vectors-names-values) for a solution using `mapply`. – jnas Nov 27 '14 at 07:39
10

I wish to make the hopefully non-trivial remark that @Tyler Rinker's suggestion

X <- split(df$x, df$name)

can be done more generally with

X <- split(df, df$name)

@Tyler Rinker's split() explanation matches the R cookbook specifying that a vector can be grouped, while in fact the entire dataframe can be grouped. I would think that grouping the dataframe, not the vector, would be the more valuable tool (and in fact what brought me to this post).

(df <- data.frame(name=c(rep("a",3),rep("b",3), rep("c",3)), x=c(1:3,4:6, 7:9)))
(X <- split(df, df$name))

HTH.

bathyscapher
  • 1,615
  • 1
  • 13
  • 18
Jack Ryan
  • 2,134
  • 18
  • 26