Question: Suppose a matrix is given say d.

```
d=[1.32 4.354 6.78 4.56; 
   4.65 3.23  2.34 8.9; 
   2.32 7.65  4.98 2.78]

```

I require to pick values such that no two values chosen are same. Say I want to pick 5 values in an increment of 1 using Matlab.

```
for ii=1:5
  % pick value one by one such that no two values chosen are same
end

```

loop should function like this:

```
  ii=1, d'=4.354
  ii=2, d'=4.354 2.32

```

It shouldn't be :

```
   ii=2, d'=4.354 4.354 

```

Answer: 
Using for loops is generally not necessary in R. A more R-like solution is to use `apply`:

```
apply(numbers, 1, function(x) sum(abs(x) < 5e0))
        Mazda RX4     Mazda RX4 Wag        Datsun 710    Hornet 4 Drive
                6                 6                 7                 6
Hornet Sportabout           Valiant
                6                 6

```

Adding it as a column simply involves:

```
numbers$V12 = apply(numbers, 1, function(x) sum(abs(x) < 5e0))

```

Some remarks on your code:

* There is no need to wrap `1:(nrow(numbers))` with `c`.
* Generating the index vector can more easily be done using `seq_len`, i.e. `seq_len(nrow(numbers))`.
After your first iteration, the `V12` column is added to your `numbers` data.frame. Because that column contains `NA` from the second row, all subsequent iterations will include a `NA` in the `sum(abs(ly) < 5e0)` operation, hence returning `NA`. A quick fix is to add `na.rm = TRUE` to that `sum` call. But as @Paul points out, there are much better ways of doing what you are trying to achieve. One of them:

```
numbers$extra.col <- rowSums(abs(numbers) < 5)

```