1. 程式人生 > >R語言函式

R語言函式

firstfun<-function(data1=x1,data2=x2,key=c)
{
  result<-merge(data1,data2,by=key)
  return(result)
}
report1<-read.table(file="ReportCard1.txt",header = TRUE)
report2<-read.table(file="ReportCard2.txt",header = TRUE)
newdata<-firstfun(report1,report2,"xh")
#應用一:資料基本處理(車險資料)
Des.Fun<-function(x,...){
  Av<-mean(x,na.rm = TRUE)
  Sd<-sd(x,na.rm = TRUE)
  N<-length(x[!is.na(x)])
  Sk<-sum((x[!is.na(x)]-Av)^3/Sd^3)/N  
  Ku<-sum((x[!is.na(x)]-Av)^4/Sd^4)/N-3    
  result<-list(avg=Av,sd=Sd,skew=Sk,kurt=Ku)      
  return(result)
}
ClaimData<-read.table(file="車險資料.txt",header=TRUE)#分隔符不加
with(ClaimData,{
  Des1<-sapply(ClaimData[,c(1,5)],FUN = Des.Fun)
  print(Des1)
  Des2<-tapply(nclaims, INDEX=vehiclegroup, FUN=mean,na.rm=TRUE)
  print(Des2)#不同車型理賠次數平均值
  cor(nclaims,holderage,use="complete.obs")#理賠次數和投保人年齡簡單相關係數
})
#計算各車型的平均理賠金額
for(i in unique(ClaimData$vehiclegroup)){
  temp<-subset(ClaimData,vehiclegroup==i)
  Des3<-sum((temp$claimamt*temp$nclaims),na.rm = TRUE)/sum(temp$nclaims,na.rm = TRUE)
  print(paste(i,round(Des3,2),sep = ":"))
}


#應用二將彙總錶轉化為原始表資料
MyTable<-function(mytable){
  rows<-dim(mytable)[1]
  cols<-dim(mytable)[2]
  tabledata<-NULL
  for (i in 1:rows) {
    for (j in 1:mytable$freq[i]) {
      rowdata<-mytable[i,c(1:(cols-1))]
      tabledata<-rbind(tabledata,rowdata)
    }
  }
  row.names(tabledata)<-c(1:dim(tabledata)[1])
  return(tabledata)
}
Grade<-rep(c("B","C","D","E"),times=2)  
Sex<-rep(c("M","F"),each=4)           
Freq<-c(2,11,12,5,2,13,10,3)   
Table<-data.frame(sex=Sex,grade=Grade,freq=Freq)
MyData<-MyTable(Table)
head(MyData)