Note that there are some explanatory texts on larger screens.

plurals
  1. POFor loop ignore my first item
    primarykey
    data
    text
    <p>I would be appreciative if you can help me with the following two issues:</p> <ol> <li>The 'for' loop below ignores my first looping item. The code below is a small part of my program. Here, I am iterating the object 'num' and looping through 'mySet' using the function 'Grep' which flags certain items from mySet. </li> </ol> <p>The 'for' loop works except at the first element '0' of the 'num' object. Consequently, it does not find any item that starts with '0' in mySet.</p> <pre><code>num &lt;- c(0,2,4,6,8,10,15,20,30,40,45,50,55,60,65,70,80,85,90,92,94,96,98,100) mySet &lt;- c("0.C.A", "2.C.A", "4.C.A", "6.C.A", "8.C.A", "10.C.A", "15.C.A", "20.C.A", "30.C.A", "40.C.A", "45.C.A", "50.C.A", "55.C.A", "60.C.A", "65.C.A", "70.C.A", "80.C.A", "85.C.A", "90.C.A", "92.C.A", "94.C.A", "96.C.A", "98.C.A", "100.C.A") varLoop &lt;- c() Grep &lt;- function(num)grep(paste("^" , num, "\\.", sep=""), mySet, value = TRUE) for(i in num){ varLoop[i] &lt;- Grep(i) print(varLoop[i])} </code></pre> <ol> <li>The second issue is an incomplete output and warnings if I modify the script as follows. The warning is: 'number of items to replace is not a multiple of replacement length'. The incomplete out put does not include any of the items ending with the letter B</li> </ol> <p>The script below produces the problem:</p> <pre><code>num &lt;- rep(c(0,2,4,6,8,10,15,20,30,40,45,50,55,60,65,70,80,85,90,92,94,96,98,100), 2) mySet &lt;- c("0.C.A", "2.C.A", "4.C.A", "6.C.A", "8.C.A", "10.C.A", "15.C.A", "20.C.A", "30.C.A", "40.C.A", "45.C.A", "50.C.A", "55.C.A", "60.C.A", "65.C.A", "70.C.A", "80.C.A", "85.C.A", "90.C.A", "92.C.A", "94.C.A", "96.C.A", "98.C.A", "100.C.A", "0.C.B", "2.C.B", "4.C.B", "6.C.B", "8.C.B", "10.C.B", "15.C.B", "20.C.B", "30.C.B", "40.C.B", "45.C.B", "50.C.B", "55.C.B", "60.C.B", "65.C.B", "70.C.B", "80.C.B", "85.C.B", "90.C.B", "92.C.B", "94.C.B", "96.C.B", "98.C.B", "100.C.B") varLoop &lt;- c() Grep &lt;- function(num)grep(paste("^" , num, "\\.", sep=""), mySet, value = TRUE) for(i in num){ varLoop[i] &lt;- Grep(i) print(varLoop[i])} </code></pre> <p>Thank you in advance, Ragy Isaac</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload