didimichael
didimichael

Reputation: 71

Convert data format in R

I have a data set, dat, which was got from a model run.

The head of the dataset looks like this:

[[1]]

[1] -1

[[2]]

[2] -2

[[3]]

[3] -1

[[4]]

[4] 0

[[5]]

[5] -6

[[6]]

[6] -7

How can I convert dat to a simple data frame with a single column like this

-1
-2
-1
0
-6
-7

Thanks

Dan

Upvotes: 0

Views: 549

Answers (2)

ariddell
ariddell

Reputation: 3423

You probably want to use the unlist function. For example:

unlist(list(1,2,3,4,5))
[1] 1 2 3 4 5

And you can turn it into a column by cbinding the results

a = unlist(list(1,2,3,4,5))
> cbind(a)
     a
[1,] 1
[2,] 2
[3,] 3
[4,] 4
[5,] 5

Upvotes: 1

Gavin Simpson
Gavin Simpson

Reputation: 174788

You have something like this:

L <- as.list(1:10)
L

So, one way is to:

> data.frame(name = t(data.frame(L)))
     name
X1L     1
X2L     2
X3L     3
X4L     4
X5L     5
X6L     6
X7L     7
X8L     8
X9L     9
X10L   10

Replace name with whatever you want the name of the variable to be.

Upvotes: 2

Related Questions