I would like to unstack a data frame in R according to two columns, i.e. starting with
id segment count freq
1 a x1a f1a
1 b x1b f1b
1 c x1c f1c
2 a x2a f2a
2 b x2b f2b
2 c x2c f2c
I want to get:
id count_a count_b count_c freq_a freq_b freq_c
1 x1a x1b x1c f1a f1b f1c
2 x2a x2b x2c f2a f2b f2c
Basically, this amounts to unstacking the data frame by the first two columns id and segment. However, I don't see how to get this to work using the unstack() function in R. I can do this using a very naive approach (nested for loop, concatenating column names etc and then binding), but there must be a more straightforward and efficient way.