Hannah O.
Hannah O.

Reputation: 383

Converting Elements in a Dataframe to Columns in a Table

I have a dataframe that looks like this:

$RandArcBo1
$RandArcBo1$x
 [1]  150.5  166.5  135.5  177.5 -146.5  153.5 -152.5  152.5  157.5 -174.5 -155.5 -139.5 -129.5  136.5 -157.5   73.5

$RandArcBo1$y
 [1] 53.56732 78.56732 79.56732 58.56732 73.56732 57.56732 74.56732 55.56732 50.56732 84.56732 82.56732 81.56732 83.56732 78.56732
[15] 73.56732 73.56732

$RandArcBo1$Species
[1] "RandArcBo1"

I would like to convert the dataframe to a table in which each dataframe element corresponds to a column in a table, as follows:

Species       x             y
RandArcBo1    150.5         53.56732
RandArcBo1    166.5         78.56732

..etc.

I have tried using xtabs(), but can't seem to phrase the code in a way that doesn't result in an error, and am also investigating the reshape package, but so far I've been stymied. Any ideas?

Upvotes: 0

Views: 82

Answers (1)

eddi
eddi

Reputation: 49448

Use the data.table package, it will do the recycling that you seem to want:

l = list(x = c(1:10), y = c(11:20), species = "test")

library(data.table)
as.data.table(l)
#     x  y species
# 1:  1 11    test
# 2:  2 12    test
# 3:  3 13    test
# 4:  4 14    test
# 5:  5 15    test
# 6:  6 16    test
# 7:  7 17    test
# 8:  8 18    test
# 9:  9 19    test
#10: 10 20    test

Upvotes: 1

Related Questions