Question: I am using Expressionengine 2. I would like to check to see if the file description is not empty so that if there is any content it will be used instead of the entry title in the alt tag. 

Currently I have the code below which brings in the entry title but I can't work out how I get the logic in place to check the existence of descriptive text for the file (image):

```
<img class="aClass" src="{work_detail_extra_image_01}" 
alt="
    {if work_detail_extra_image_01:description != ""}
        {work_detail_extra_image_01}{description}{/work_detail_extra_image_01}
    {if:else}
        {title}
    {/if}
" 
border="0" />

```

Thanks for taking a look

Answer: 
We could use `dcast` from the devel version of `data.table`, ie. `v1.9.5`. We can install it from [`here`](https://github.com/Rdatatable/data.table/wiki/Installation)

```
library(data.table)#v1.9.5+
dcast(setDT(df.in)[order(when),action:= paste0('action', 1:.N) ,who],
                           who~action, value.var='what')

```

If you need `unique` 'what' for each 'who'

```
dcast(setDT(df.in)[, .SD[!duplicated(what)], who][order(when),
    action:= paste0('action', 1:.N), who], who~action, value.var='what')
#         who action1 action2 action3
#1: George   sleep     run      NA
#2:   John   sleep     run      NA
#3:   Paul   sleep     eat    walk
#4:  Ringo     eat    walk      NA

```

Or using `.I` will be a bit more fast

```
 ind <- setDT(df.in)[,.I[!duplicated(what)], who]$V1 

 dcast(df.in[ind][order(when),action:= paste0('action', 1:.N) ,who], 
            who~action, value.var='what')

```

Or using `setorder` and `unique` which may be a memory efficient as `setorder` reorder the dataset by reference.

```
 dcast(unique(setorder(setDT(df.in), who, when), by=c('who', 'what'))[,
     action:= paste0('action', 1:.N), who], who~action, value.var='what')
 #     who action1 action2 action3
 #1: George   sleep     run      NA
 #2:   John   sleep     run      NA
 #3:   Paul   sleep     eat    walk
 #4:  Ringo     eat    walk      NA

```
You can also you use the combo `dplyr` + `tidyr`

```
library(dplyr)
library(tidyr)

df.in %>%
  group_by(who) %>%
  mutate(when = rank(when), when = paste0("action", when)) %>%
  spread(key = when, value = what)
 ##      who action1 action2 action3 action4
 ## 1 George   sleep     run      NA      NA
 ## 2   John   sleep     run      NA      NA
 ## 3   Paul   sleep     eat     eat    walk
 ## 4  Ringo     eat    walk      NA      NA

```

**EDIT**

If you need just the first occurence of the `what` columns, you can just filter the data first

```
df.in %>%
  arrange(when) %>%
  group_by(who) %>%
  filter(!duplicated(what)) %>%
  mutate(when = rank(when), when = paste0("action", when)) %>%
  spread(key = when, value = what)
##      who action1 action2 action3
## 1 George   sleep     run      NA
## 2   John   sleep     run      NA
## 3   Paul   sleep     eat    walk
## 4  Ringo     eat    walk      NA

```