I have the following table in R:
S <- c("A","A","A","B","B","B","C","C","C")
TS <- c(1,1,1,2,2,2,3,3,3)
f1 <- c(10,20,30,15,25,35,17,27,37)
p <- c(100,200,300,150,250,350,170,270,370)
df <- data.frame(S, TS, f1, p)
So it looks like that:
| S | TS | f1 | p |
|---|---|---|---|
| A | 1 | 10 | 100 |
| A | 2 | 20 | 200 |
| A | 3 | 30 | 300 |
| B | 1 | 15 | 150 |
| B | 2 | 25 | 250 |
| B | 3 | 35 | 350 |
| C | 1 | 17 | 170 |
| C | 2 | 27 | 270 |
| C | 3 | 37 | 370 |
Now I want to transform my dataframe so that I have unique values for TS (timestamps) for every row and binded my variables right to it, like this:
| TS | SA_f1 | pA | SB_f1 | pB | SC_f1 | pC |
|---|---|---|---|---|---|---|
| 1 | 10 | 100 | 15 | 150 | 17 | 170 |
| 2 | 20 | 200 | 25 | 250 | 27 | 270 |
| 3 | 30 | 300 | 35 | 250 | 37 | 370 |
What is the most elegant way doing this?