Question: I'm using `explode()` to basically take apart paragraphs into individual words. Works great. The looping through with a `foreach()`. Also works great. Nothing complicated here. 

```
$title_pieces = explode(" ", $title_fixed);
foreach($title_pieces as $tpiece){
echo "<b>$tpiece<br>";
}

```

Unfortunately this returns just an ugly long list of words. What I'd like to do but can't quite figure out how is to put this all in a nice table. Creating the table is no problem, the part I can't figure out is how to get it to write more than one `$tpiece` per row. I'd like to have maybe 5 `<td>`s in each row. 

So if I do:

```
foreach($title_pieces as $tpiece){
echo "<tr><td>$tpiece</td></tr>";
}

```

I'm still just left with a long list. Can someone point me in the right direction here.

Answer: 
I got different sums, but it were b/c I forgot the seed:

```
> dat1$x <- ave(dat1$x, dat1$id, FUN=sum)
> dat1[!duplicated(dat1$id), ]
    id year    month     x
1 1234 2006 December 25.18
2 1321 2006 December 15.06
3 4321 2006 December 15.50
4 7423 2006 December  7.16
6 8503 2007  January 13.23
7 2961 2007  January  7.38
9 8564 2007  January  7.21

```

(To be safer It would be better to work on a copy. And you might need to add an ordering step.)
You could do this with `data.table` (quicker, more memory efficiently than plyr)

With a bit of self-joining fun using `mult` ='first'. Keying by id year and month will sort by id, year then month. 

```
library(data.table)
DT <- data.table(dat1, key = c('id','year','month'))

# setnames is required as there are two x columns that get renamed x, x.1
DT1 <- setnames(DT[DT[,list(x=sum(x)),by=id],mult='first'][,x:=NULL],'x.1','x')

```

---

Or a simpler approach :

```
DT = as.data.table(dat1)

DT[,x:=sum(x),by=id][!duplicated(id)]

     id year    month     x
1: 1234 2006 December 36.42
2: 1321 2006 December 11.55
3: 4321 2006 December 17.31
4: 7423 2006 December  5.97
5: 8503 2007  January 12.48
6: 2961 2007  January 10.22
7: 8564 2007  January 11.41

```