####################### # Functions # ####################### #A lot of functions in SPLUS are pre-written x<-c(1:100) sum(x) #Sum function sqrt(x) #Square-root, element by element log(x) #Natural log, element by element #etc but it is also possible to write your own functions #For example, for a function to take a matrix and produce a #vector of the row sums the following definition can be used rowsum.func<-function(x){ nr<-nrow(x) #nrow finds out how many rows x has svec<-numeric(length=nr) for(i in 1:nr){ svec[i]<-sum(x[i,]) } svec } xmat<-matrix(c(1:50),ncol=5,byrow=T) rowsum.func(xmat) #Actually, this operation can be implemented by using the "apply" function apply(xmat,1,sum) #This Worksheet has been completed