Some Basic Functions

Here are a couple of functions that are from scratch. The basic form is always the same:

x<-function(){ #open the function here
#body of function
return() #value of list vector to be returned
} #close the function here
#Try out the functions below and try writing one of your own
#Building Functions
#1. Recreating the mean function
mean1<-function(x){
mean<-sum(x)/length(x)
return(mean)
}
x<-seq(1:5)
mean1(x)
mean(x)
#2. A function to return z-scores
z.score<-function(x){ #x is a vector of length n
z.score=(x-mean(x)/sd(x))
z.score
}
z.score(x)
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>