I have some data in a form similar to this: 

```
xmpl <- data.frame(x = c("022406391116","034506611298", "015410661242"))
xmpl

    X
  1 022406391116
  2 034506611298
  3 015410661242

```

Every value is made up of pairs of numbers (two digit each):  

 Item number, item value, item number, item value.

So for the first line in the example, I have value 24 for item #2, value 39 for item #6, value 16 for item #11. in line two I have item #3 with value 45 etc'. In the example maximum item number is 12.

I want to "unfold" the data so I have a new column for each item number that comes up, with it's value in the corresponding line.
In the example it **should look like this**:

```
  X             item1  item2  item3  item6  item11  item12
1 022406391116    NA     24     NA     39     16      NA
2 034506611298    NA     NA     45     61     NA      98
3 015411161242    54     NA     NA     NA     16      42

```

To get to that I tried to use a double loop:

```
for (nq in c(0,1,2)) {
  for (qs in 1:12) {
    if (as.numeric(substr(xmpl$x, 4 * nq + 1, 4 * nq + 2)) == qs) {
      xmpl[[paste0("item", qs)]] <- as.numeric(substr(xmpl$x, 4 * nq + 3, 4 * nq + 4))
    }
  }
}

```

I get this warning for every time the `if` is run in the loop:

> 
> In if (as.numeric(substr(xmpl$x, 4 \* nq + 1, 4 \* nq + ... : the
>  condition has length > 1 and only the first element will be used
> 
> 
> 

And sure enough **the (bad) result is**:

```
> xmpl
             x item2 item6 item11
1 022406391116    24    39     16
2 034506611298    45    61     98
3 015410661242    54    66     42

```

New columns are created only for the first line, while the rest of the values are interpreted accurately but only put in the existing columns, defined for the first line.

How can I get this to work on each line separately? Or if it can't be done this way (please explain why) - what is a better strategy?

**EDIT:**
Just to clarify - I already have this working, but only through a longish process (splitting, reshaping to long and back to wide). This loop is my attempt at shortening the process, and I need help in understanding why the loop won't work.