JerryWho
JerryWho

Reputation: 3105

How to convert matrix (or list) into data.frame

I've got a list with different types in it. They are arranged in matrix form:

tmp <- list('a', 1, 'b', 2, 'c', 3)
dim(tmp) <- c(2,3)
tmp
     [,1] [,2] [,3]
[1,] "a"  "b"  "c" 
[2,] 1    2    3   

That's the form I get it out of another more complex function.

Now I want to transpose it and convert to a data.frame. So I do the following:

data <- as.data.frame(t(tmp))
data
  V1 V2
1  a  1
2  b  2
3  c  3

This looks great. But it's got the wrong structure:

str(data)
'data.frame': 3 obs. of  2 variables:
 $ V1:List of 3
  ..$ : chr "a"
  ..$ : chr "b"
  ..$ : chr "c"
 $ V2:List of 3
  ..$ : num 1
  ..$ : num 2
  ..$ : num 3        

So how do I get rid of the extra level of lists?

Upvotes: 1

Views: 1831

Answers (2)

Josh O&#39;Brien
Josh O&#39;Brien

Reputation: 162391

This should do the trick:

df <- data.frame(lapply(data.frame(t(tmp)), unlist), stringsAsFactors=FALSE)
str(df)
# 'data.frame':   3 obs. of  2 variables:
#  $ X1: chr  "a" "b" "c"
#  $ X2: num  1 2 3
  • The inner data.frame() call converts the matrix into a two column data.frame, with one "character" column and one "numeric" column.**

  • lapply(..., unlist) strips away extra list() layer.

  • The outer data.frame() call converts the resulting list into the data.frame you're after.

** (OK, that intermediate "character" column is really of class "factor", but it ends up making no difference in the final result. If you like, you could force it to be have class "character" by adding a stringsAsFactors=FALSE for the inner data.frame() call as well, but I don't think neglecting to do so would ever make a difference...)

Upvotes: 5

agstudy
agstudy

Reputation: 121588

Or this :

 as.data.frame(matrix(unlist(tmp),ncol=2,byrow=TRUE))

You can inspect the result:

 str(as.data.frame(matrix(unlist(tmp),ncol=2,byrow=TRUE)))
'data.frame':   3 obs. of  2 variables:
 $ V1: Factor w/ 3 levels "a","b","c": 1 2 3
 $ V2: Factor w/ 3 levels "1","2","3": 1 2 3

Upvotes: 2

Related Questions