Introduction to R (see R-start.doc)
Be careful -- R is case sensitive.
Setting and getting the working directory
- Use File > Change dir...
- setwd("P:/Data/MATH/Hartlaub/Nonparametrics")
- getwd()
Reading data (Creating a dataframe)
- mydata=read.csv(file=file.choose())
Commands for dataframes
- mydata #shows the entire data set
- head(mydata) #shows the first 6 rows
- tail(mydata) #shows the last 6 rows
- str(mydata) #shows the variable names and types
- names(mydata) #shows the variable names
- ls() #shows a list of objects that are available
- attach(mydata) #attaches the dataframe to the R search path, which makes it easy to access variable names
Descriptive Statistics
- mean(x) #computes the mean of the variable x
- median(x) #computes the median of the variable x
- sd(x) #computes the standard deviation of the variable x
- IQR(x) #computer the IQR of the variable x
- summary(x) #computes the 5-number summary and the mean of the variable x
- cor(x,y) #computes the correlation coefficient
Graphical Displays
- hist(x) #creates a histogram for the variable x
- boxplot(x) # creates a boxplot for the variable x
- boxplot(y~x) # creates side-by-side boxplots
- stem(x) #creates a stem plot for the variable x
- plot(y~x) #creates a scatterplot of y versus x
- abline(lm(y~x)) #adds regression line to plot
- lines(lowess(x,y)) # adds lowess line (x,y) to plot
Nonparametric Tests
- binom.test(x, n, p) #Binomial test for proportions
- SIGN.test(x, md=0) #Need BSDA package, Sign test
- wilcox.test(x) #Wilcoxon signed rank test
- wilcox.test(x, y) #Wilcoxon rank sum test
- ansari.test(x, y) #Ansari-Bradley test
- fligner.test(x, g) #Fligner-Killeen test of equal variances; g is a grouping variable
- ks.test(x,y) #Kolmogorov-Smirnov test
- kruskal.test(x, g) #Kruskal-Wallis rank sum test; g is a grouping variable
- kruskalmc(response, group) #Need pgirmess package; g is a grouping variable
- JT.test(data, g) #Need SAGx package; Jonckheere-Terpstra test; data is a matrix; g is a grouping variable
- cor.test(x,y) #correlation test plus CI for several measures of association (r, rho, tau)
- friedman.test(y~A|B)
# y are the data values, A is a grouping factor, and B is a blocking factor