I have a dataframe in long format like
id varA varB
1 'a' 112
1 'b' 212
1 'c' 308
2 'a' 99
2 'b' 123
2 'c' 452
I want to collapse per id
the values in varB
for varA == 'a'
and varA == 'b'
.
In a wide dataframe like:
id varBa varBb varBc
1 112 212 308
2 99 123 452
... I'd simply use apply or the vectorised addition like df$collapsed = df$varBa + df$varBb
.
How can I do this with the long-format dataframe? (Having only a wide format is not an option due to factor-wise follow-ups analysis).
Is it possible to collapse (or do any other arithmetic operation) in a long format so that I'd have an additional row for the collapsed values?.