[R] Collapsing panel data

Karina Knaus Karina.Knaus at e-control.at
Tue Feb 3 09:43:04 CET 2009

Dear R-helpers,

I've been thinking about this for some time, maybe someone can help. I have
a fairly large dataset with thousands of firms, call the a, b, c, etc..
such as

  [,1]       [,2]
[1,] "A"    0.5
[2,] ""     0.2
[3,] ""     0.3
[4,] "B"    0.1
[5,] ""     0.9
[6,] "C"    0.4

Or to put it differently two vectors such as

y <- c("A", "", "", "B", "", "C")
x <- c(0.5, 0.2, 0.3, 0.1, 0.9, 0.4)

The empty lines "" always belong to the firm above. Now I want to collapse
the dataset so that each firm (A,B, C, etc) has one line only, using

So what I would like is

yNew <- c("A", "B", "C")
xNew <- c(1, 1, 0.4)

The problem I'm having is that each firm has a different number of entries
for x, so some like C have just one and others have ten or more, so I have
difficulty imagining how to use a loop in this case.
I'd be greatful for any suggestions.

More information about the R-help mailing list