Please work through the commands and check out functions used using help()
. There is also a plain R script Intro1.R
with most of the R commands, in case you want to execute the commands from the script.
Try out the following commands.
2 + 3
x = 2 + 3
exp(-4 * 4 / 2) / sqrt(2 * pi)
dnorm(4, 0, 1) ## or dnorm(0, 4, 1)
Note that "=" and "<-" are equivalent in R. For consistency with other languages this tutorial will use "=", however you can use "<-" if you prefer. You can also use "->" for assignment in the opposite direction but this is not frequently used.
x = 2 + 3
y <- 2 + 3
2 + 3 -> z
x
y
z
ages <- c(30, 40, 55, 46, 57)
ages
Download the file chickwt.csv and put it in the folder where you are running R.
mydata <- read.csv("chickwt.csv")
summary(mydata)
str(mydata)
write.table(mydata, file="mydata.txt", sep=";") # write out as ASCI with ; as delimitor.
mydata$weight
mean(mydata$weight)
var(mydata$weight)
(mylm <- lm(weight ~ feed, mydata))
plot(density(mydata$weight)) # plot a kernel density estimate of the weight distribution
par(mfrow=c(2,2)) # make next 4 plots appear in 2x2 subplot layout
plot(mylm) # produce 4 plots showing diagnostic information from the linear model
par(mfrow=c(1,1)) # reset to regular full sized plot layout
#boxplots for tooth growth againts absorbic acid and orange juice using 'ToothGrowth' dataset
boxplot(len ~ dose, data = ToothGrowth,
boxwex = 0.25, at = 1:3 - 0.2,
subset = supp == "VC", col = "yellow",
main = "GuineaPigs' Tooth Growth",
xlab = "Vitamin C dose mg",
ylab = "tooth length",
xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i")
boxplot(len ~ dose, data = ToothGrowth, add = TRUE,
boxwex = 0.25, at = 1:3 + 0.2,
subset = supp == "OJ", col = "orange")
legend(2, 9, c("Ascorbic acid", "Orange juice"),
fill = c("yellow", "orange"))
mymat <- data.matrix(mydata) #save data in matrix format
colMeans(mymat) #print mean of matrix columns
mymat[70, 1] <- NA #insert an NA value into column 1
colMeans(mymat) #column means now give an error because of NA
colMeans(mymat, na.rm=TRUE) #use na.rm flag to ignore entries that are NA
save(mymat, file = "mymat.RData")
load("mymat.RData")
faraway
:¶install.packages("faraway")
library(faraway) # attaches the package to the search path
library(help=faraway) # gives you a list of all available objects and functions in the package