0

I have two lists (Phase and Rate in the example). What I need to do is to create a new list with two nested lists where the elements of the second one (Rate) are grouped by the levels of the first one (Phase). It might be a very basic question but I could not come up with a solution.

Phase <- c("A", "A", "A", "A", "B", "B", "B")
Rate <- c(1, 2, 3, 2, 6, 2, 9)

list(A = c(1, 2, 3, 2), B = c(6, 2, 9)) 

Thanks for any help!

Michael Matta
  • 394
  • 2
  • 16
  • 1
    This question is general and should have a duplicate somewhere, but couldn't find one hence posting an answer. – Ronak Shah Aug 16 '18 at 01:38
  • @RonakShah let me find for you https://stackoverflow.com/questions/27388706/split-a-vector-into-unequal-chunks-in-r – BENY Aug 16 '18 at 02:07
  • @Wen Thanks but I don't think so, this question is more simpler than that. However, if you think it answers the OP's question please go ahead and mark it as dupe. – Ronak Shah Aug 16 '18 at 02:14

1 Answers1

4

We can use split, to split Rate based on Phase and it returns a list

split(Rate, Phase)

#$A
#[1] 1 2 3 2

#$B
#[1] 6 2 9
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213