简体   繁体   中英

Named List To/From Data.Frame

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.

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, ie 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 .

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

Another option is enframe from tibble

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

data

lst1 <- list(a=1:3, b=4:6)

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

http://my.safaribooksonline.com/book/programming/r/9780596809287/6dot1dot-splitting-a-vector-into-groups/id3392005

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM