I have a data structure that looks like the following:
groupA1 groupA2 groupB1 groupB2 date text
0 1 1 1 2013-01-01 the dog
For each groupB
variable, I want to list one row for each column that has a value of 1.
I need to list all combinations of groupA
and groupB
where 1s are present into one row, but then also add the date and text to each of those combinations as columns in that row.
Transformed data would appear as:
var_groupB var_groupA date text
groupB1 groupA2 2013-01-01 the dog
groupB2 groupA2 2013-01-01 the dog
I've tried combinations of melt
and ddply
but am always left without one of the variables I need.
One thing I tried was melt(x, id.vars=c("text", "date"))
but then I lose all information about the relationships between groupA
and groupB
.
I could accomplish this using a messy loop, but wasn't sure if a reshape
utility exists that I'm unaware of and could do the job.