R 學習筆記《九》 R語言初學者指南--循環和函數

1 循環ide

載入數據函數

setwd("E:/R/R-beginer-guide/data/RBook")
Owls <- read.table(file="Owls.txt",header=TRUE)
names(Owls)
str(Owls)

  

弄清鳥巢的名字ui

unique(Owls$Nest)
[1] AutavauxTV      Bochet          Champmartin     ChEsard        
[5] Chevroux        CorcellesFavres Etrabloz        Forel          
[9] Franex          GDLV            Gletterens      Henniez        
[13] Jeuss           LesPlanches     Lucens          Lully          
[17] Marnand         Moutet          Murist          Oleyes         
[21] Payerne         Rueyes          Seiry           SEvaz          
[25] StAubin         Trey            Yvonnand      
27 Levels: AutavauxTV Bochet Champmartin ChEsard ... Yvonnand

  

提取屬於某個鳥巢的數據並畫出ArrivalTime 和NegPerChick變量的plot圖設計

Owls.ATV <- Owls[Owls$Nest=="AutavauxTV",]
plot(x=Owls.ATV$ArrivalTime,y=Owls.ATV$NegPerChick,
     xlab="Arrival Time",main="AutauxTV",ylab="Negotiation behavaiour")

  

通用一點:blog

Nest.i <- "Bochet"  
Owls.i <- Owls[Owls$Nest == Nest.i,]
plot(x=Owls.i$ArrivalTime,y=Owls.i$NegPerChick,
     xlab="Arrival Time",main="AutauxTV",
     ylab="Negotiation behavaiour")

  

將plot結果保存爲jpeg文件get

setwd("E:/R/R-beginer-guide/jpegs")
Nest.i <- "Bochet"  
Owls.i <- Owls[Owls$Nest == Nest.i,]
YourFileName <- paste(Nest.i,".jpeg",sep="")
jpeg(file=YourFileName)
plot(x=Owls.i$ArrivalTime,y=Owls.i$NegPerChick,
     xlab="Arrival Time",main="AutauxTV",
     ylab="Negotiation behavaiour")
dev.off()

  

構造循環:it

ALLNests <- unique(Owls$Nest)
for(i in 1:27){
  Nest.i <- ALLNests[i]  
  Owls.i <- Owls[Owls$Nest == Nest.i,]
  YourFileName <- paste(Nest.i,".jpeg",sep="")
  jpeg(file=YourFileName)
  plot(x=Owls.i$ArrivalTime,y=Owls.i$NegPerChick,
       xlab="Arrival Time",main="AutauxTV",
       ylab="Negotiation behavaiour")
  dev.off()
}

  

2 函數io

 

載入數據:table

setwd("E:/R/R-beginer-guide/data/RBook")
  Veg <- read.table(file="Vegetation2.txt",header=TRUE)     
  names(Veg)

  

定義函數:ast

NAPerVariable <- function(X1){
    D1 <- is.na(X1)
    colSums(D1)
  }

 

執行函數:

NAPerVariable(Veg[,5:24])
       R     ROCK   LITTER       ML BARESOIL FallPrec  SprPrec  SumPrec 
       0        0        0        0        0        0        0        0 
 WinPrec FallTmax  SprTmax  SumTmax  WinTmax FallTmin  SprTmin  SumTmin 
       0        0        0        0        0        0        0        0 
 WinTmin  PCTSAND  PCTSILT  PCTOrgC
       0        0        0        0

  

函數解釋:

 

函數的第一個參數X1列表是標量,行表是觀察值.is.na(X1)生成了一個與X1維數相同的布爾矩陣,若是X1中某個值爲確實值,那麼獲得的矩陣對應的元素的值就是TRUE,不然爲FALSE。colSums是R自帶的一個函數,其做用是計算每一列中元素的和.通常colSums做用與數值矩陣,可是當其做用與布爾矩陣時,將TRUE轉化爲1將FALSE轉化爲0.

使用函數:

載入數據:

Parasite <- read.table(file="CodParasite.txt",header=TRUE)
   names(Parasite)
NAPerVariable(Parasite)
    Sample  Intensity Prevalence       Year      Depth     Weight     Length 
         0         57          0          0          0          6          6 
       Sex      Stage        Age       Area
         0          0          0          0

  

變量Intensity中有57個缺失值,weight和Length有6個缺失值

 

定義另一函數,統計每一個變量中到底有多少個0

ZeroPerVaviable <- function(X1){
       D1=(X1==0)
       colSums(D1)
   }

  

運用這個函數:

ZeroPerVaviable <- function(X1){
+        D1=(X1==0)
+        colSums(D1)
+    }

ZeroPerVaviable(Parasite)
    Sample  Intensity Prevalence       Year      Depth     Weight     Length 
         0         NA        654          0          0         NA         NA 
       Sex      Stage        Age       Area
        82         82         84          0

  

有NA值,重新定義函數:

ZeroPerVaviable <- function(X1){
       D1=(X1==0)
       colSums(D1,na.rm=TRUE)
   }
ZeroPerVaviable <- function(X1){
+        D1=(X1==0)
+        colSums(D1,na.rm=TRUE)
+    }

ZeroPerVaviable(Parasite)
    Sample  Intensity Prevalence       Year      Depth     Weight     Length 
         0        654        654          0          0          0          0 
       Sex      Stage        Age       Area
        82         82         84          0

  

多參數函數

VariableInfo <- function(X1,Choice1){
      if(Choice1 == "Zeros"){
        D1=(X1==0)
      }
      if(Choice1 == "NAs"){
        D1 <- is.na(X1)
      }
      colSums(D1,na.rm=TRUE)
    }

  

使用:

VariableInfo <- function(X1,Choice1){
+       if(Choice1 == "Zeros"){
+         D1=(X1==0)
+       }
+       if(Choice1 == "NAs"){
+         D1 <- is.na(X1)
+       }
+       colSums(D1,na.rm=TRUE)
+     }

VariableInfo(Parasite,"Zeros")
    Sample  Intensity Prevalence       Year      Depth     Weight     Length 
         0        654        654          0          0          0          0 
       Sex      Stage        Age       Area
        82         82         84          0

VariableInfo(Parasite,"NAs")
    Sample  Intensity Prevalence       Year      Depth     Weight     Length 
         0         57          0          0          0          6          6 
       Sex      Stage        Age       Area
         0          0          0          0

  

設計穩健的函數,默認值,拼寫容錯,ifelse使用

VariableInfo <- function(X1,Choice1="Zeros"){
      if(Choice1 == "Zeros"){
        D1=(X1==0)
      }
      if(Choice1 == "NAs"){
        D1 <- is.na(X1)
      }
      if(Choice1 !="Zeros" & Choice1 != "NAs"){
        print("you made a typo")
      } else{
          colSums(D1,na.rm=TRUE)
      }
       
    }
VariableInfo <- function(X1,Choice1="Zeros"){
+       if(Choice1 == "Zeros"){
+         D1=(X1==0)
+       }
+       if(Choice1 == "NAs"){
+         D1 <- is.na(X1)
+       }
+       if(Choice1 !="Zeros" & Choice1 != "NAs"){
+         print("you made a typo")
+       } else{
+           colSums(D1,na.rm=TRUE)
+       }
+      
+     }
VariableInfo(Parasite,"dsa")
[1] "you made a typo"
相關文章
相關標籤/搜索