Question: Imagine that I have 9 sampling records for three species distributed as such:

```
sp1 sp2 sp3
3 1 5

```

What I want to obtain is a records x species matrix, and fill it with 1s and 0s as such:

```
sp1 sp2 sp3 
1 0 0
1 0 0
1 0 0
0 1 0
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1

```

The number of columns matches with the number of species and the number of rows with the number of records. Note that each row represents a unique record for one species.

Answer: 
Use `rep` to produce the `1`s and `dcast` from `reshape2` to shape and fill.

```
library(reshape2)
x<-list(sp1=3,sp2=1,sp3=5)
d<-melt(lapply(x,function(i) rep(1,i)))
dcast(d,1:nrow(d)~L1,fill=0)[-1]

```

```

  sp1 sp2 sp3
1   1   0   0
2   1   0   0
3   1   0   0
4   0   1   0
5   0   0   1
6   0   0   1
7   0   0   1
8   0   0   1
9   0   0   1

```
Here is another option using `stack` and `spread`

```
library(tidyr)

stackedList = stack(apply(df, 2, function(x) rep(1, x)))
out = spread(stackedList, ind, values, fill = 0)

#> out
#  sp1 sp2 sp3
#1   1   0   0
#2   1   0   0
#3   1   0   0
#4   0   1   0
#5   0   0   1
#6   0   0   1
#7   0   0   1
#8   0   0   1
#9   0   0   1

```

**data**

```
df = data.frame(sp1 = 3, sp2 = 1, sp3 = 5) 

```