I have found it useful to create quick and simple vectors and data frames within R since I know that much of the real world data I might be working with may have certain peculiarities that sometimes impede the learning process for me. Here are some of the common functions I use:
Note: placing a ? before R functions in the console will bring up their help page, which for the following functions will be useful to know.
rnorm()
This is a function draws a sample of size n from a normal distribution with a specified mean (mu) and standard deviation (sd) (if not specified these are set to 0 and 1 respectively. Here’s and example of using this function to create a vector:
x<-rnorm(1000,25,12)
This is a vector of length 1000, mu = 25, sd = 12
rbinom()
This function will draw a sample of size n from the binomial distribution with a specified number of trials (n1) and probability of success (p). This is often useful for simulating categorical response variables. Here’s and example:
y<-rbinom(100,1,.5)
I’ve simulated 100 cases from a binomial distribution with n1=1 trial and a .5 probability of a success (1).
These two do a lot for me but there are many, many others that are useful and may be more useful to you in your work such as chisq(), unif(), t(), gamma(), hyper(), geom(), and pois().
Putting the simulated vectors into a data frame is relatively simple:
data<-data.frame(x,y)
attach(data)
Now it’s ready to use.