Francis Smart
Francis Smart

Reputation: 4055

R: Turn List Data into Dataframe

I have list data generated from a JSON file created by ODK Aggregate. It looks something like:

P1 <- list(a01 = 1:11, a02 = letters[1:11], a03 = list(letters), a04 = (1:11)^2)

What I would like to do is basically convert P1 into a dataframe by dropping any nested lists (a03). With this simple example we would get the following as our final output.

P1data <- data.frame(a01 = 1:11, a02 = letters[1:11], a04 = (1:11)^2)

Thank you for any help that you can provide!

Upvotes: 3

Views: 123

Answers (1)

David Arenburg
David Arenburg

Reputation: 92282

Try this

as.data.frame(Filter(Negate(is.list), P1))
#    a01 a02 a04
# 1    1   a   1
# 2    2   b   4
# 3    3   c   9
# 4    4   d  16
# 5    5   e  25
# 6    6   f  36
# 7    7   g  49
# 8    8   h  64
# 9    9   i  81
# 10  10   j 100
# 11  11   k 121

Upvotes: 7

Related Questions