0

I would like to assign variable 'ax' with a value in looping. My code is :

 ax<-0
 for (i in i:4){
 if(i%%4==1)
 {
    ax[i]<-"one"
 }
 else if(i%%4==2)
 {
     ax[i]<-"two"
 }
 else if(i%%4==3)
 {
     ax[i]<-"three"
 }
else
 {
     ax[i]<-"four"
 }

}

I expect I got ax[1], ax[2], ax[3], and ax[4] ,

"one"    "two"     "three"     "four"

but I got :

"0"    NA     NA     "four"

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?

here I add the real code

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

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

 if(i%%4==1)
 {
a<-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]<-as.numeric(a)
Sui_Q1_TS[i]<-as.numeric(b)
Sui_Q1_S[i]<-as.numeric(c)
Sui_Q1_SS[i]<-as.numeric(d)
Sui_Q2_STS[i]<-as.numeric(e)
Sui_Q2_TS[i]<-as.numeric(f)
Sui_Q2_S[i]<-as.numeric(g)
Sui_Q2_SS[i]<-as.numeric(h)


Sui_Q1<-list()
Sui_Q1[[i]]<-cbind(Sui_Q1_STS[i],Sui_Q1_TS[i],Sui_Q1_S[i],Sui_Q1_SS[i])
Sui_Q2<-list()
Sui_Q2[[i]]<-cbind(Sui_Q2_STS[i],Sui_Q2_TS[i],Sui_Q2_S[i],Sui_Q2_SS[i])
Total[i]<-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
}

}

and I got :

> Sui_Q1
[[1]]
NULL

[[2]]
NULL

[[3]]
NULL

[[4]]
     [,1] [,2] [,3] [,4]
[1,]    0    0    4    4
13
  • You don't need a loop. c("four", "one", "two", "three")[as.numeric(factor((v1%%4==1) + 2*(v1%%4==2) + 4*(v1%%4==3)))] where v1 <- sample(1:4, 10, replace=TRUE) Commented May 2, 2016 at 3:14
  • Check your code i in i:4 what is i in i:4 Commented May 2, 2016 at 3:19
  • @akrun I need it. the looping is not just 4 actually. I will do like 50-100++ looping Commented May 2, 2016 at 3:19
  • The above code has a typo. The for statement should be "1:4" and not "i:4". In any case see akrun's suggestion for much better R coding style. Commented May 2, 2016 at 3:19
  • @akrun oh typio, it is 1:4 Commented May 2, 2016 at 3:20

2 Answers 2

3

If we need to convert the numbers to english words

library(english)
english(v1)
#[1] two   one   three three three four  two   four  four  two  

Or using base R without any loop

c("four", "one", "two", "three")[as.numeric(factor((v1%%4==1) + 
             2*(v1%%4==2) + 4*(v1%%4==3)))]
#[1] "two"   "one"   "three" "three" "three" "four"  "two"   "four"  "four"  "two"  

If we really need a loop

 ax <- numeric(4)
 for(i in 1:4){
  if(i%%4 ==1){
  ax[i] <- "one"
  } else if (i%%4 ==2){
 ax[i] <- "two"
 }else if (i%%4 ==3){
  ax[i] <- "three"
 } else {
  ax[i] <- "four"
  }
 }

ax
#[1] "one"   "two"   "three" "four" 

data

set.seed(24)
v1 <- sample(1:4, 10, replace=TRUE)
Sign up to request clarification or add additional context in comments.

3 Comments

I did post the new question with reproducible code. here : stackoverflow.com/questions/36974742/… please check
I got the answer. my fault to assign Sui_Q1 as a list more than once.
and ur way to do the looping can be implemented. thank you
2

This very simple expression does what you want:

i <- 1:4
c("four", "one", "two", "three")[i %% 4 + 1]
## [1] "one"   "two"   "three" "four"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.