Skip to content
Prev 275222 / 398506 Next

Expanding rows of a data frame into multiple rows

Here's another approach using the plyr package:

# Function to process each row of input:
g <- function(d) {
    y <- unlist(d$observations)
    if(length(y) > 0)
      data.frame(site = d$site, sector = d$sector, y = y) else NULL
  }

library('plyr')
site sector  y
1     1  north  1
2     1  north  2
3     1  north  3
4     2  south  4
5     2  south  3
6     4   west 14
7     4   west 12
8     4   west 53
9     4   west  2
10    4   west  4
11    5   east  3
12    6  south 23

HTH,
Dennis

On Sat, Oct 22, 2011 at 4:51 PM, Peter Meilstrup
<peter.meilstrup at gmail.com> wrote: