Getting Started with R

1. Downloading and Installing R

  • R Studio
  • R Language

2. Getting Help on a Function


help(func)
?(func)
args(func)
example(func)

3. Viewing the Supplied Documentation


help.start()

4. Searching the Web for Help

5. Reading Tabular Datafiles


dfrm <- read.table("filename.txt",="" sep=":" )="" print="" dftm="" <="" code="">

6. Reading from CSV Files


table <- read.csv("filename",="" header="FALSE)" <="" code="">

7. Creating a Vector


v1 <- c(1,2,3)="" v2="" <-="" c("a","b","c")="" mode(c(3.1415,="" "foo"))="" <="" code="">

8. Computing Basic Statistics


mean(x)
median(x)
sd(x)
var(x)
cor(x,y)
cov(x,y)

9. Initializing a Data Frame form Column Data


dfrm <- data.frame(v1,="" v2,="" v3,="" f1,="" f2)="" dfrm="" <-="" as.data.frame(list.of.vectors)="" data.frame(pred1,="" pred2,="" pred3,="" resp)="" data.frame(p1="pred1," p2="pred2," p3="pred3," r="resp)" <="" code="">

10. Selecting Data Frame Columns by Position


dfrm[[n]]
dfrm[n]
dfrm[c(n_1, n_2, ..., n_k)]
dfrm[, n]
dfrm[, c(n_1, n_2, ..., n_k)]
dfrm[, vec, drop=FALSE]

11. Selecting Data Frame Columns by Name


dfrm[[anme]]
dfrm$name
dfrm["name"]
dfrm[c("name_1", "name_2", ..., "name_k")]
dfrm[, "name"]
dfrm[, c("name_1", "name_2", ..., "name_k")]

12. Forming a Confidence Interval for a Mean


x <- rnorm(50,="" mean="100," sd="50)" t.test(x)="" t.test(x,="" conf.level="0.99)" <="" code="">

13. Forming a Confidence Interval for a Proportion


prop.test(n, x)
prop.test(6, 9)
prop.test(n, x,,p, conf.level=0.99) # 99% confidence level

14. Comparing the Means of Two Samples


t.test(x, y)
t.test(x, y, paired=TRUE)

15. Testing a Correlation for Significance


cor.test(x, y)
cor.test(x, y, method="Spearman")

16. Creating a Scatter Plot


plot(x, y)
plot(dfrm)

17. Creating a Bar Chart


barplot(c(height1, height2, height3))

barplot(heights,
        main="Mean Temp. by Month"
        names.arg=c("May", "Jun", "July", "Aug", "Sep"),
        ylab="Temp (deg. F)")

18. Creating a Box Plot


boxplot(x)

19. Creating a Histogram


data(Cars93, package="MASS")
hist(Cars93$MPG.city)
hist(Cars93$MPG.city, 20)
hist(Cars93$MPG.city, 20, main="City MPG (1993)", xlab="MPG")

20. Performing Simple Linear Regression


lm(y ~ x)
lm(y ~ x, data=dfrm) # Take x and y from dfrm

21. Performing Multiple Linear Regression


y=c(6.584519, 6.425215, 7.830578, 2.757777, 5.794566, 7.314611, 2.533638, 8.696910, 6.304464, 8.095094)
u=c(0.79939065, -2.31338537, 1.71736899, 1.27652888, 0.39643488, 1.82247760, -1.34186107, 0.75946803, 0.92000133, 1.02341093)
v=c(2.7971413, 2.7836201, 2.7570401, 0.4191765, 2.3785468, 1.8291302, 2.3472593, 3.4028180, 2.0654513, 2.6729252)
w=c(4.366557, 4.515084, 3.865557, 2.547935, 3.265971, 4.518522, 2.570884, 4.442560, 2.835248, 3.868573)
lm(y ~ u + v + w)
lm(y ~ u + v + w, data=dfrm)

22. Getting Regression Statistics


y=c(6.584519, 6.425215, 7.830578, 2.757777, 5.794566, 7.314611, 2.533638, 8.696910, 6.304464, 8.095094)
u=c(0.79939065, -2.31338537, 1.71736899, 1.27652888, 0.39643488, 1.82247760, -1.34186107, 0.75946803, 0.92000133, 1.02341093)
v=c(2.7971413, 2.7836201, 2.7570401, 0.4191765, 2.3785468, 1.8291302, 2.3472593, 3.4028180, 2.0654513, 2.6729252)
w=c(4.366557, 4.515084, 3.865557, 2.547935, 3.265971, 4.518522, 2.570884, 4.442560, 2.835248, 3.868573)
m <- lm(y="" ~="" u="" +="" v="" w)="" summary(m)="" anova(m)="" coefficients(m)="" coef(m)="" confint(m)="" deviance(m)="" effects(m)="" fitted(m)="" residuals(m)="" resid(m)="" vcov(m)="" <="" code="">

23. Diagnosing a Linear Regression


m <- lm(y="" ~="" x)="" plot(m)="" library(car)="" outlier.test(m)="" <="" code="">

24. Predicting New Values


m <- lm(y="" ~="" u="" +="" v="" w)="" preds="" <-="" data.frame(u="3.1," w="5.5)" predict(m,="" newdata="preds)" data.frame(="" 3.1,="" 3.2,="" 3.3),="" 4.0,="" 4.1,="" 4.2),="" 5.5,="" 5,7,="" 5.9)="" )="" <="" code="">

25. Accessing the Functions in a Package


library(packagename)
library(MASS)
lda(f ~ x + y)
detach(package:MASS)

Chinese Translation