ben_aaron ben_aaron - 3 months ago 11
R Question

Collapse variables in long format dataframe R

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?.

Answer

Try this:

 dfw <- reshape(df,
 timevar = "varA",
 idvar = c("id"),
 direction = "wide")
 dfw
  id varB.a varB.b varB.c
1  1    112    212    308
4  2     99    123    452

Then you can do any math operations on the new data frame.

Comments