R 学习笔记《九》 R语言初学者指南--循环和函数

1 循环

载入数据


 setwd("E:/R/R-beginer-guide/data/RBook")
 Owls <- read.table(file="Owls.txt",header=TRUE)
 names(Owls)
 str(Owls)
弄清鸟巢的名字



> 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")

通用一点:

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文件

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()
构造循环:

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 函数

载入数据:

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

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"
>



你可能感兴趣的:(R 学习笔记《九》 R语言初学者指南--循环和函数)