R loop and if else
<p>I would like to assign variable 'ax' with a value in looping. My code is :</p>

<pre><code> ax&lt;-0
 for (i in i:4){
 if(i%%4==1)
 {
    ax[i]&lt;-"one"
 }
 else if(i%%4==2)
 {
     ax[i]&lt;-"two"
 }
 else if(i%%4==3)
 {
     ax[i]&lt;-"three"
 }
else
 {
     ax[i]&lt;-"four"
 }
</code></pre>

<p>}</p>

<p>I expect I got ax[1], ax[2], ax[3], and ax[4] ,</p>

<pre><code>"one"    "two"     "three"     "four"
</code></pre>

<p>but I got :</p>

<pre><code>"0"    NA     NA     "four"
</code></pre>

<p>This is just a try, a real code are much more longer. but the 0nly main problem is about assigning value to respective array variable. Anything wrong with this?</p>

<p>here I add the real code</p>

<pre><code>#library(sqldf)
n = 4
a=0
b=0
c=0
d=0
e=0
f=0
g=0
h=0

Sui_Q1_STS&lt;-0
Sui_Q1_TS&lt;-0
Sui_Q1_S&lt;-0
Sui_Q1_SS&lt;-0
Sui_Q2_STS&lt;-0
Sui_Q2_TS&lt;-0
Sui_Q2_S&lt;-0
Sui_Q2_SS&lt;-0
for (i in 1:n){

 if(i%%4==1)
 {
a&lt;-sqldf("SELECT count([X.0]) from testes where [X.0]='Sangat Tidak Setuju' and (StartDate like '%11/05/2015 00:%' or StartDate like '%11/05/2015 01:%' or StartDate like '%11/05/2015 02:%' or StartDate like '%11/05/2015 03:%' or StartDate like '%11/05/2015 04:%' or StartDate like '%11/05/2015 05:%' or StartDate like '%11/05/2015 06:00%')")
#b c d e f g and h are also sqldf
Sui_Q1_STS[i]&lt;-as.numeric(a)
Sui_Q1_TS[i]&lt;-as.numeric(b)
Sui_Q1_S[i]&lt;-as.numeric(c)
Sui_Q1_SS[i]&lt;-as.numeric(d)
Sui_Q2_STS[i]&lt;-as.numeric(e)
Sui_Q2_TS[i]&lt;-as.numeric(f)
Sui_Q2_S[i]&lt;-as.numeric(g)
Sui_Q2_SS[i]&lt;-as.numeric(h)


Sui_Q1&lt;-list()
Sui_Q1[[i]]&lt;-cbind(Sui_Q1_STS[i],Sui_Q1_TS[i],Sui_Q1_S[i],Sui_Q1_SS[i])
Sui_Q2&lt;-list()
Sui_Q2[[i]]&lt;-cbind(Sui_Q2_STS[i],Sui_Q2_TS[i],Sui_Q2_S[i],Sui_Q2_SS[i])
Total[i]&lt;-Sui_Q1[[i]][,1]+Sui_Q1[[i]][,2]+Sui_Q1[[i]][,3]+Sui_Q1[[i]][,4]
}
   else if(i%%4==2){

  #only different in SQL
   }
   else if(i%%4==3){

  #only different in SQL
}
   else if(i%%4==0){

   #only different in SQL
}
</code></pre>

<p>}</p>

<p>and I got :</p>

<pre><code>&gt; Sui_Q1
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    4    4
</code></pre>