-1

I have a list of lists with the following structure:

fruit
-apple
--ID = 1
---condition = good
---colour = green
--ID = 2
---condition = bad
---colour = red
--ID = 3
---condition = okay
--- colour = red
-banana
--ID = 4
---condition = bad
---colour = yellow
---peel = TRUE
--ID = 5
---condition = bad
---colour = yellow
---peel = TRUE
--ID = 6
---condition = good
---colour = yellow
---peel = FALSE
-peach
--ID = 7
---colour = orange
--ID = 8
---colour = orange
--ID = 9
---colour = orange

is there a way to extract a vector of the colours? ie, "green" "red" "red" "yellow" ....

It is possible to grab them for a single fruit by: apply(fruit$apple, '[[', "colour") but I do not know how to grab through all fruit without looping through the names?

thanks in advance for the help!

pvalue
  • 9
  • 1

1 Answers1

1

You can use the pluck function from rvest package to brute force it if your structure is consistent eg

# pluck out the ID's
p1 <- rvest::pluck(fruit, "ID")
# from them, pluck the colours
ans <- rvest::pluck(p1, "colour")
Jonny Phelps
  • 2,687
  • 1
  • 11
  • 20