R回路,如果其他 [英] R loop and if else

查看:133
本文介绍了R回路,如果其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在循环值赋给变量斧头。我的code是:

  AX< -0
 为(i的I:4){
 如果(ⅰ%% 4 == 1)
 {
    AX [1] - ; - 一
 }
 否则如果(ⅰ%% 4 == 2)
 {
     AX [1] - ; - 二
 }
 否则,如果(我%% 4 == 3)
 {
     AX [1] - ; - 三化
 }
其他
 {
     AX [1] - ; - 四个一
 }

}

我希望我得到了斧头[1],AX [2],AX [3]和AX [4]

 一二三四

但我得到了:

 0NA​​ NA四个一

这仅仅是一个尝试,真正的code是多长。但0nly主要问题是即将各自的数组变量分配值。什么错呢?

在这里我想补充的真正code

  #library(sqldf)
N = 4
a = 0时
B = 0
C = 0
D = 0
E = 0
F = 0
G = 0
H = 0Sui_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
为(i的1:N){ 如果(ⅰ%% 4 == 1)
 {
A<从睾丸-sqldf(SELECT COUNT([X.0])其中[X.0] ='Sangat Tidak Setuju'和(起始日期LIKE'%11月5日/ 2015 00:%或起始日期,如11% / 05/2015年01:%或起始日期,如%11/05/2015年02:%或起始日期,如%11/05/2015年03:%或起始日期,如%11/05/2015年04:%或起始日期,如%11/05/2015年05:%或起始日期,如%11/05/2015年06:00%'))
#B C D E F G和H也sqldf
Sui_Q1_STS [1] - ; -as.numeric(一)
Sui_Q1_TS [1] - ; -as.numeric(二)
Sui_Q1_S [1] - ; -as.numeric(三)
Sui_Q1_SS [1] - ; -as.numeric(四)
Sui_Q2_STS [1] - ; -as.numeric(五)
Sui_Q2_TS [1] - ; -as.numeric(F)
Sui_Q2_S [1] - ; -as.numeric(G)
Sui_Q2_SS [1] - ; -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])
总[1] - ; -Sui_Q1 [[Ⅰ] [1] + Sui_Q1 [[Ⅰ] [2] + Sui_Q1 [[I]] [1,3] + Sui_Q1 [[I]] [,4]
}
   否则如果(ⅰ%% 4 == 2){  在SQL #only不同
   }
   否则如果(ⅰ%% 4 == 3){  在SQL #only不同
}
   否则,如果(我%% 4 == 0){   在SQL #only不同
}

}

和我有:

 > Sui_Q1
[[1]]
空值[[2]
空值[3]]
空值[[4]
     [1] [2] [3] [,4]
[1] 0 0 4 4


解决方案

如果我们需要将数字转换为英文单词

 库(英文)
英语(V1)
#[1]二一三三三四二四四两


或使用基 - [R 无任何环

  C(四有,一,二,三化)[as.numeric(因子((V1 %% 4 == 1)+
             2 *(V1 %% 4 == 2)+ 4 *(V1 %% 4 == 3)))]
#[1]二一三化三化三四二四有四有两化


如果我们真的需要一个循环

  AX<  - 数字(4)
 为(i的1:4){
  如果(ⅰ%% 4 == 1){
  AX [1] - ; - 一
  }否则如果(ⅰ%% 4 == 2){
 AX [1] - ; - 二
 }否则如果(ⅰ%% 4 == 3){
  AX [1] - ; - 三化
 }其他{
  AX [1] - ; - 四个一
  }
 }斧头
#[1]一二三四

数据

  set.seed(24)
V1< - 样品(1:4,10,替换= TRUE)

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

解决方案

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)

这篇关于R回路,如果其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆