1. 程式人生 > >R語言裡的多層迴圈和巢狀的用法

R語言裡的多層迴圈和巢狀的用法

可以先列出提綱,也就是大框架,列框架時,須注意。

if(){}else{}  表示先執行if括號後面的條件語句,如果正確就執行第一個大括號裡的程式,如果錯誤就執行else後面大括號裡的語句。
    但是如果出現下面的情況就會報錯:

if{}

else{}
   也就是else語句沒有在大括號的後面,這是r會認為if語句已經執行完畢,但執行else發現前面無法執行,else必須緊挨著if語句後的大括號,這時才不會出錯。為了避免出現漏大括號,和出現錯誤,我們可以先列出提綱:

我的資料處理中要用到兩個迴圈,迴圈是巢狀的,巢狀的迴圈中包含三種情況(if),第一種情況裡面包含三個條件分支(也就是三種情況)(if-else if -else);第二種情況裡也包含三個條件分支(也就是三種情況)(if-else if -else);第三種情況裡也包含一個條件分支else(也就是yi種情況):

草稿如下:

if (條件1) {
      if  (條件1-1) {
    print()
      }else if  (條件1-1) {
                print()
      }else {#條件1-3
              print()
               }
}else if (條件2) {
               if (條件2-1) {
               print()
               }else if (條件2-2) {
                print()
               }else{#條件2-2
               print()
              }
}else {#(條件3)
           print()
          }

然後框架寫好以後只需要把條件換成自己所要求的條件就行了,然後把print語句換成想要輸出的形式就行了,這一點和python相比落後一點,不能夠自動縮排,很費勁,敬請期待用python實現。下面的程式碼是我根據自己的需求填寫的程式碼,看起來很充實吧

setwd("c:/users/11565/Desktop/合併二/可以用資料")
sentiment<-read.csv("001.csv")
names(sentiment)
dim(sentiment)
attach(sentiment)
sentiment$date<-as.character(sentiment$date)#轉換成字串
#刪除含有確認的噪聲資料(如確認收貨之類),重複六次可以全部刪除
for (j in 1:6){ 
  for (i in (1:length(sentiment$c_bianhao))){
    if ("確" %in% unlist(strsplit(sentiment$date[i], "")) == T)
      sentiment=sentiment[-i,]
    else{
      sentiment[i,]=sentiment[i,]
    }
  }
}
dim(sentiment)
table(sentiment$date)
sentiment<-sentiment[c(1:31600),]
detach()

#對sentiment_value1進行分類計算
for (i in (1:length(sentiment$c_bianhao)))
{
  if (sentiment_value1[i]>=0.7)
  {sentiment$critic1[i]=1
  sentiment$class1[i]="好評"}
  if (sentiment_value1[i]<0.7 & sentiment_value1[i]>0.3)
  {sentiment$critic1[i]=2
  sentiment$class1[i]="中評"}
  if (sentiment_value1[i]<=0.3 && sentiment_value1[i]>0)
  {sentiment$critic1[i]=-1
  sentiment$class1[i]="差評"} 
}
#對sentiment_value2進行分類計算
for (i in (1:length(sentiment$c_bianhao)))
{
  if (sentiment_value2[i]>=0.7){
    sentiment$critic2[i]=1
    sentiment$class2[i]="好評"}
  if (sentiment_value2[i]<0.7 & sentiment_value2[i]>0.3)
  {sentiment$critic2[i]=2
  sentiment$class2[i]="中評"}
  if (sentiment_value2[i]<=0.3 & sentiment_value2[i]>=0)
  {sentiment$critic2[i]=-1
  sentiment$class2[i]="差評"}
}
detach()
#
write.table(sentiment,"sentiment.csv",sep=",",col.names = T,row.names = F)
sentiment<-read.csv("sentiment.csv",na.strings="",stringsAsFactors = F)#已經定義好class,和去噪聲的表格
x10<-table(sentiment$c_bianhao)
write.table(x10,"x10.csv",sep=",",col.names = T,row.names = F)
x10<-read.csv("x10.csv",stringsAsFactors = F)
c_bianhao1<-x10[,1]
head(sentiment)

for (i in (1:length(c_bianhao1))){
  x11<-subset(sentiment,sentiment$c_bianhao==c_bianhao1[i])
  date1<-table(x11$date)
  write.table(date1,"date1.csv",sep=",",col.names = T,row.names = F)
  date1<-read.csv("date1.csv",stringsAsFactors = F)
  date1<-date1[,1]
  for (j in (1:length(date1))){
    x12<-subset(x11,x11$date == date1[j])
     x13<-aggregate(x12$critic1,by=list(class=x12$class1),sum)
    #轉換成了資料框
    x13<-t(as.data.frame(x13))
    colnames(x13)<-x13[1,]
    x13<-as.data.frame(x13)
    x14<-dim(x13)
    #是一種型別的解法
    if (x14[2]==1) {
      if  (x13[1,1]=="差評") {
        x15$差=x13[,1]
        x15$中=c("中評",0)
        x15$好=c("好評",0)
        x15$c_bianhao=c(c_bianhao1[i],c_bianhao1[i])
        x15$date=c(date1[j],date1[j])
        zonghe<-rbind(zonghe,x15)
      }else if  (x13[1,1]=="中評") {
        x15$差=c("差評",0)
        x15$中=x13[,1]
        x15$好=c("好評",0)
        x15$c_bianhao=c(c_bianhao1[i],c_bianhao1[i])
        x15$date=c(date1[j],date1[j])
        zonghe<-rbind(zonghe,x15)
      }else {x15$差=c("差評",0)
      x15$中=c("中評",0)
      x15$好=x13[,1]
      x15$c_bianhao=c(c_bianhao1[i],c_bianhao1[i])
      x15$date=c(date1[j],date1[j])
      zonghe<-rbind(zonghe,x15)
      }
    }else if (x14[2]==2) {
      if ((x13[1,1]=="差評") & (x13[1,2]=="中評")) {
        x15$差=x13[,1]
        x15$中=x13[,2]
        x15$好=c("好評",0)
        x15$c_bianhao=c(c_bianhao1[i],c_bianhao1[i])
        x15$date=c(date1[j],date1[j])
        zonghe<-rbind(zonghe,x15)
      }else if (x13[1,1]=="差評" & x13[1,2]=="好評") {
        x15$差=x13[,1]
        x15$中=c("中評",0)
        x15$好=x13[,2]
        x15$c_bianhao=c(c_bianhao1[i],c_bianhao1[i])
        x15$date=c(date1[j],date1[j])
        zonghe<-rbind(zonghe,x15)
      }else{
        x15$差=c("差評",0)
        x15$中=x13[,2]
        x15$好=x13[,1]
        x15$c_bianhao=c(c_bianhao1[i],c_bianhao1[i])
        x15$date=c(date1[j],date1[j])
        zonghe<-rbind(zonghe,x15)
      }
    }else{
      x15$差=x13[,1]
      x15$中=x13[,2]
      x15$好=x13[,3]
      x15$c_bianhao=c(c_bianhao1[i],c_bianhao1[i])
      x15$date=c(date1[j],date1[j])
      zonghe<-rbind(zonghe,x15)
    }
  }
}