Skip to content
Permalink
Browse files

added code from the book

  • Loading branch information...
markvanderloo committed Mar 16, 2018
1 parent 7b8fcae commit c6d2ecf985ac44c241e887ccee9925acdc5181e1
@@ -0,0 +1,4 @@
## ------------------------------------------------------------------------
age <- sample(100,25,replace=TRUE)
mean(age)

@@ -0,0 +1,348 @@
## ------------------------------------------------------------------------
1 + 1

## ----eval=FALSE----------------------------------------------------------
## 1 + 1
## 3^2
## sin(pi/2)
## (1 + 4) * 3
## exp(1)
## sqrt(16)

## ------------------------------------------------------------------------
x <- 10
y <- 20

## ------------------------------------------------------------------------
x

## ------------------------------------------------------------------------
x + y
z <- x * y
q <- x^2*z
q

## ------------------------------------------------------------------------
x <= y
x == y
x > y

## ----eval=FALSE----------------------------------------------------------
## ?mean

## ----eval=FALSE----------------------------------------------------------
## ??average

## ----eval=FALSE----------------------------------------------------------
## # a vector with numbers 1,2 and 3
## c(1,3,5)
## # a vector with two text elements
## c("hello world","hello universe")

## ----eval=FALSE----------------------------------------------------------
## # a vector with numbers 1,2,...,10
## 1:10
## # a sequence of numbers from 1 to 6 in 100 steps.
## seq(1,6,length.out=100)

## ----eval=FALSE----------------------------------------------------------
## # 100 numbers drawn from the standard normal distribution
## rnorm(100)
## # 50 numbers drawn from the uniform distribution on [2,7]
## runif(50,min=2,max=7)

## ------------------------------------------------------------------------
c(1,"hello", 3.14)

## ----eval=TRUE, result=FALSE---------------------------------------------
x <- 1:3
y <- c("foo", "bar")
class(x)
class(y)

## ------------------------------------------------------------------------
length(y)

## ------------------------------------------------------------------------
shoesize <- c(jan=43, pier=39, joris=45, korneel=42)

## ------------------------------------------------------------------------
mean(shoesize)

## ------------------------------------------------------------------------
names(shoesize)

## ------------------------------------------------------------------------
x <- c(2,3,5,7)
y <- c(1,2,4,8)
x + y
x < y
exp(-x) + sin(y)

## ------------------------------------------------------------------------
3 + x
z <- c(1,2)
z + y

## ----eval=FALSE----------------------------------------------------------
## 1:3 + 5:8

## ------------------------------------------------------------------------
x <- rnorm(100)
# compute the mean
mean(x)
# compute the sample variance
var(x)
# standard deviation
sd(x)
# Tukey's five-number summary
fivenum(x)

## ------------------------------------------------------------------------
summary(x)

## ----eval=FALSE----------------------------------------------------------
## x <- rnorm(100)
## y <- x + rnorm(100)
## # scatterplot
## plot(x,y)
## # boxplot
## boxplot(x)
## # histogram
## hist(x)

## ------------------------------------------------------------------------
A <- array(1:12, dim=c(2,3,2))

## ----eval=FALSE----------------------------------------------------------
## 2 * A
## c(1,2) * A

## ----eval=TRUE-----------------------------------------------------------
A <- matrix(1:6,ncol=2)
b <- matrix(c(-1,1),nrow=2)

## ------------------------------------------------------------------------
x <- c(1L, 2L, 7L)

## ------------------------------------------------------------------------
y <- 2 * x

## ------------------------------------------------------------------------
data(iris)
head(iris,3)

## ----eval=FALSE----------------------------------------------------------
## summary(iris)
## plot(iris)

## ----eval=FALSE----------------------------------------------------------
## # the number of rows
## nrow(iris)
## # the number of colums
## ncol(iris)
## # both nr of rows and columns
## dim(iris)
## # names of the columns
## names(iris)
## # a shorter summary of a data.frame
## str(iris)

## ------------------------------------------------------------------------
# compute the mean sepal width
mean(iris$Sepal.Width)

# add a 'ratio' column
iris$ratio <- iris$Sepal.Width/iris$Sepal.Length
head(iris, 2)

# remove the 'ratio' column again
iris$ratio <- NULL

## ----eval=FALSE----------------------------------------------------------
## # specify a linear model
## m <- lm(Sepal.Length ~ Sepal.Widht + Species, data=iris)
## summary(m)
##
## # create a boxplot for each species
## boxplot(Sepal.Length ~ Species, data = iris)
##
## # create a scatterplot
## plot(Sepal.Length ~ Sepal.Width, data = iris)

## ------------------------------------------------------------------------
subset(women, height > 70)

## ----eval=TRUE, result=FALSE---------------------------------------------
x <- c(1,7,10,13,19)
# select the 2nd element
x[2]
# select the 2nd and 5th element
x[c(2,5)]
# set the first element to 0
x[1] <- 0
# select all elements equal to zero
x[x==0]
# set all elements equal to zero equal to 1
x[x==0] <- 1

## ------------------------------------------------------------------------
# find x < 15; I is a logical vector
I <- x < 15
# find x > 1; J is a logical vector
J <- x > 2
# select elements in x satisfying both I and J
x[I&J]

## ------------------------------------------------------------------------
max(x)
which.max(x)
which(x == 7)

## ------------------------------------------------------------------------
iris[3:7,2:4]

## ----eval=FALSE----------------------------------------------------------
## iris[1, ]

## ----eval=FALSE----------------------------------------------------------
## iris[ ,2:4]

## ----eval=FALSE----------------------------------------------------------
## iris[,1,drop=FALSE]

## ----eval=FALSE----------------------------------------------------------
## iris[1]

## ----eval=FALSE----------------------------------------------------------
## iris[ iris$Sepal.Length < 6, 'Species', drop=FALSE]

## ------------------------------------------------------------------------
6/0
6/0 - Inf

## ------------------------------------------------------------------------
x <- c(1,2,Inf)
x == Inf

## ------------------------------------------------------------------------
x <- c(1,2,NaN)
x == NaN

## ------------------------------------------------------------------------
is.nan(x)

## ------------------------------------------------------------------------
is.finite(c(2,1,3,NA,7,Inf,NaN))

## ------------------------------------------------------------------------
length( c(1,NULL,2) )
length( c(1,NA,2) )

## ------------------------------------------------------------------------
c(1,2,3) == NULL

## ------------------------------------------------------------------------
iris$somecolumn

## ------------------------------------------------------------------------
is.null(iris$somecolumn)

## ------------------------------------------------------------------------
x <- c(1.4,3.2,NA,7.1)

## ------------------------------------------------------------------------
x + 1

## ------------------------------------------------------------------------
mean(x)

## ------------------------------------------------------------------------
NA == 27
NA == NA
NA == "foo"

## ------------------------------------------------------------------------
x <- c(1,1,2,NA,5)
is.na(x)

## ------------------------------------------------------------------------
mean(x, na.rm=TRUE)

## ------------------------------------------------------------------------
myiris <- iris
save(myiris, file="myiris.RData")

## ----eval=FALSE----------------------------------------------------------
## load("myiris.RData")
## # check that the data frame is indeed loaded.
## ls()

## ------------------------------------------------------------------------
# create a linear model
m <- lm(Sepal.Length ~ Sepal.Width + Species, data=iris)
# store the model and the data set
save(m, iris, file="mymodel.RData")

## ----eval=FALSE----------------------------------------------------------
## write.csv(iris, file="myiris.csv", row.names=FALSE)

## ----eval=FALSE----------------------------------------------------------
## my_iris <- read.csv(file="myiris.csv")

## ----eval=FALSE----------------------------------------------------------
## "C:/My Documents/myfile.csv"
## "C:\\My Documents\\myfile.csv"

## ----eval=FALSE----------------------------------------------------------
## fl <- tempfile(fileext=".csv")
## write.csv2(iris, file=fl)
## file.exists(fl)
## file.info(fl)

## ----echo=FALSE----------------------------------------------------------
suppressPackageStartupMessages(library(RSQLite))

## ------------------------------------------------------------------------
library(RSQLite)
# set up an in-memory SQLite database.
db <- dbConnect(SQLite())
# create a table in the database named "IRIS", and load the iris dataset.
dbWriteTable(db, name="IRIS", value=iris)
# execute a query and retrieve its result
dbGetQuery(db, "select * from IRIS where [Sepal.Width] = 4.2")
# close the connection
dbDisconnect(db)

## ----eval=FALSE----------------------------------------------------------
## 1 + 1

## ----eval=FALSE----------------------------------------------------------
## `+`(1,1)

## ------------------------------------------------------------------------
data <- c(1,7,NA,10)
# default behavior of 'mean': return NA
mean(data)

## ------------------------------------------------------------------------
mean(data, na.rm=TRUE)

## ------------------------------------------------------------------------
mean(x=data, na.rm=TRUE)
mean(na.rm=TRUE, x=data)

## ------------------------------------------------------------------------
f <- function(x){
x^2 - x - 1
}

## ------------------------------------------------------------------------
f(3)

## ------------------------------------------------------------------------
g <- function(x){
out <- x^2 - x - 1
out
}

Oops, something went wrong.

0 comments on commit c6d2ecf

Please sign in to comment.
You can’t perform that action at this time.