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: 
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

```
Here is a method using a more traditional `split-apply-combine`. It's more idiomatic R code than the `for` loop, though {dplyr} and {data.table} solutions seem to more common than this type of {base} R solution. This method uses `dcast` from {reshape2} but it could also use `reshape()` for a purely {base} R solution. 

This method is likely not much faster than the `for` loop given in the question. I'd be interested in knowing how the three methods given compare for a large dataset. I'm a beginner and have been working on learning R data manipulation lately. Any feedback is welcome.

```
library(reshape2)

#Split the data by person and apply the function
actions <- lapply(split(df.in, df.in$who), function(tmp) {

    tmp <- tmp[order(tmp$when),]
    dup <- duplicated(tmp$what)
    df.out <- data.frame(who = tmp$who[!dup], what = tmp$what[!dup])
    df.out$actionNo <- paste("action", c(1:nrow(df.out)), sep = "")
    return(df.out)

})

#Combine the results
act_rbind <- do.call(rbind, actions)
act_cast <- dcast(act_rbind, who ~ actionNo, value.var = "what")
print(act_cast)

    #      who action1 action2 action3
    # 1 George   sleep     run    <NA>
    # 2   John   sleep     run    <NA>
    # 3   Paul   sleep     eat    walk
    # 4  Ringo     eat    walk    <NA>

```