n.var <- sum((n-n.mean)^2/(length(n))
n.var <- sum((n-n.mean)^2/(length(n))
n.var <- sum((n-n.mean)^2)/length(n)
n.var - var(n)
n.var <- sum((n-n.mean)^2)/(length(n)-1)
n.var - var(n)
n.var <- sum((n-n.mean)^2)/length(n)
n.var - var(n)
n.var <- sum((n-n.mean)^2)/(length(n)-1)
n.var - var(n)
dt <- 0.01
t <- seq(0, pi/6, by = dt)
f <- cos(t)
sin(pi/6)
(I <- sum(f)*dt)
I - sin(pi/6)
dt <- 0.001
f <- cos(t)
(I <- sum(f)*dt)
dt <- 0.001
t <- seq(0, pi/6, by = dt)
f <- cos(t)
(I <- sum(f)*dt)
I - sin(pi/6)
x <- seq(0, 150, 10)
y <- (15 + 1/x)^(1/x)
plot(x, y)
n <- NA
is.na(n)
n <- c(50, NA, 200)
n
is.na(n)
any(is.na(n))
mean(n)
mean(n, na.rm = TRUE)
X <- NULL
X
100/0
n <- NA
is.na(n)
n <- c(50, NA, 200)
n
is.na(n)
any(is.na(n))
mean(n)
mean(n, na.rm = TRUE)
X <- NULL
X
5 == 6
5 == 5
5 != 6
c(0,0,0,1) | C(1,0,0,1)
c(0,0,0,1) | c(1,0,0,1)
xor(c(0,0,0,1), c(1,0,0,1))
x <- 1:20
x %% 4 == 0
(z <- x[x %% 4 == 0])
x <- c(2, NA, 3, 4)
x >2
x [x>2]
subset(x, subset = x > 2)
x <- 1:20
which(x %% 4 == 0)
x <- c(10,12,16,25,49)
which(x %% 4 == 0)
2*2 == 4
sqrt(2) * sqrt(2) == 2
all.equal(sqrt(2)*sqrt(2), 2)
x <- 0
x *sin(1/x) == 0
(x == 0) | (sin(1/x) == 0)
(x == 0) || (sin(1/x) == 0)
matrix(data, nrow = 1, ncol = 1, byrow = False)
(A <- matrix(1:6, nrow = 2, ncol = 3, byrow = TRUE))
(A <- matrix(1:6, nrow = 2, ncol = 3, byrow = FALSE))
dim(A)
A[2, 1]
A[, 1:2]
(B <- diag(c(4,8,9,12)))
(A <- matrix(c(3, 5, 4, 2), nrow = 2, ncol = 2)
)
(B <- matrix(c(1, 2, 4, 3), nrow = 2, ncol = 2))
A * B
A +B
A %*% B
(A.inv <- solve(A))
A %*% A.inv
A^(-1)-A.inv
savehistory(file = "history1")
savehistory(file = "history.txt")
savehistory(file = "history1.txt")
rm(list = ls())
#find zeros of a*x^2 + b*x + c = 0
#clear the workspace
rm(list = ls())
#input
a <- 5
b <- 8
c <- 2
#calculation
root1 <- (-b + sqrt(b^2 - 4*a*c))/(2*a)
root2 <- (-b - sqrt(b^2 - 4*a*c))/(2*a)
#output
print(c(root1, root2))
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr00.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr00.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr00.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr00.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr00.r")
savehistory(file = "history2")
x <- c(7, 10, 15)
x[1:2] <- swap(x[1:2])
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr14.r")
x[1:2] <- swap(x[1:2])
x[2:3] <- swap(x[2:3])
y <- 100
x[1:2] <- swap(x[1:2])
test <- function(s) {
f <- s + 1
return(f)
}
test(1)
s
f
formals(test)
test <- fubction(x = 1) {
test <- function(x = 1) {
return(x)
}
test(5)
test()
fcn <- function(argus = c("book", "car", "cat" )) {
argus <- match.arg(argus)
return(argus)
}
fcn()
fcn("car")
fcn("carcar")
?apply\
?apply
?sapply
?par
?sapply
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr15.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr15.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr15.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr15.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr16.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr17.r")
nfact_n(8)
nfact_n(6)
my_fun <- function(x) {
browser()
y <- x * z
return(y)
}
my_fun(c(1,2,3))
hair <- c("black", "gray", "gray", "brown", "blond", "brown")
is.character(hair)
is.factor(hair)
hair <- factor(hair)
is.factor(hair)
levels(hair)
hair <- factor(hair, levels = c("black", "gray", "brown", "none"))
table(hair)
hair <- factor(hair, levels = c("black", "gray", "brown", "blond","none"))
table(hair)
hair <- c("black", "gray", "gray", "brown", "blond", "brown")
hair <- factor(hair, levels = c("black", "gray", "brown", "blond","none"))
table(hair)
phys.act <- c("L", "H", "L", "L", "H", "M", "M")
phys.act <- factor(phys.act, levels = c("L", "M", "H"), ordered = TRUE)
is.ordered(phys.act)
phys.act[2] > phys.act[1]
phys.act[2] < phys.act[1]
phys.act <- factor(phys.act, levels = c("L", "M", "H"), ordered = TRUE)
phys.act <- factor(phys.act, levels = c("L", "M", "H"),
labels = c("Low", "Medium", "High"),
ordered = TRUE)
table(phys.act)
?read.table
ufc <- read.csv("../data/ufc.csv")
ufc <- read.csv("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/data")
ufc <- read.csv("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/data/ufc.csv")
ufc
head(ufc)
tail(ufc)
x <- ufc$height.m
x[1:6]
ufc[[5]]
ufc[["height.m"]]
ufc[1:5, 5]
ufc[1:5, 4]
head(ufc)
d.h <- ufc[4:5]
d.h[1:5,]
d.h <- ufc["dbh.cm", "height.m"]
d.h[1:5,]
d.h
d.h <- ufc[c("dbh.cm", "height.m")]
d.h[1:5,]
is.data.frame(d.h)
mode(ufc)
mode(ufc[5])
mode(ufc[[5]])
ufc$volume.m3 <- pi * (ufc$dbh.cm/200)^2 * ufc$height.m/2
mean(ufc$volume.m3)
ufc$volume.m3 <- with(ufc, pi * (ufc$dbh.cm/200)^2 * ufc$height.m/2)
mean(ufc$volume.m3)
(ufc.names <- names(ufc))
names(ufc) <- c("P", "T", "S", "D", "H", "V")
names(ufc)
names(ufc) <- ufc.names
names(ufc)
head(ufc)
t.height <- subset(ufc, subset = species %in% c("DF", "GF"),
select = c(plot, tree, height.m)
)
head(t.height)
ufc
?write.table
my.list <- list("one", TRUE, c("f", "h", "t"), c(1,5,9))
my.list
my.list[[2]]
my.list[2]
mode(my.list[[2]])
mode(my.list[2)
mode(my.list[2])
my.list[[4]][1]
my.list[[4]][2]
my.list[4][2]
my.list[4][1]
my.list <- list(first = "one", second = TRUE, third = 3, fourth = c(5,7,9))
names(my.list)
my.list$second
x <- list(1, c(4,5), c(4,12,16))
unlist(x)
lm.xy <- lm(y ~ x, data = data.frame(x = 1:5, y = 1:5))
mode(lm.xy)
names(lm.xy)
ghahraman <- list(
A = c(2000,2005),
B = c(2001, 2004, 2010)
C = c(2002,2003)
ghahraman <- list(
A = c(2000,2005),
B = c(2001, 2004, 2010),
C = c(2002,2003),
D = c(2006, 2007, 2008, 2009),
E = c()
)
str(ghahraman)
str(ghahraman)
year <- 2005
for (i in 1:length(ghahraman)) {
if (year %in% ghahraman[[i]]) {
winner <- names(ghahraman)[i]
}
}
winner
year <- 2009
winner
for (i in 1:length(ghahraman)) {
if (year %in% ghahraman[[i]]) {
winner <- names(ghahraman)[i]
}
}
winner
head(ufc)
tapply(ufc$height.m, ufc$species, mean)
tapply(ufc$species, ufc$species, length)
plot(ufc$dbh.cm, ufc$height.m)
plot(ufc$dbh.cm, ufc$height.m,
xlab = "Diameter (cm)", ylab = "Height(m)"
plot(ufc$dbh.cm, ufc$height.m,
xlab = "Diameter (cm)", ylab = "Height(m)")
?plot
?par
opar1 <- par(las = 1, mar = c(4,4,3,2))
plot(ufc$dbh.cm, ufc$height.m, axses = FALSE, xlab = "",
ylab = "", type = "n")
plot(ufc$dbh.cm, ufc$height.m, axes = FALSE, xlab = "",
ylab = "", type = "n")
plot(ufc$dbh.cm, ufc$height.m, axes = FALSE, xlab = " ",
ylab = " ", type = "n")
ylab = " ")
plot(ufc$dbh.cm, ufc$height.m, axes = FALSE, xlab = " ",
ylab = " ")
plot(ufc$dbh.cm, ufc$height.m, axes = FALSE, xlab = " ",
ylab = " ", type = "n")
points(ufc$dbh.cm, ufc$height.m,
col = ifelse(ufc$height.m > 4.9, "darkseagreen4", "red"),
pch = ifelse(ufc$height.m > 4.9, 1, 3))
points(ufc$dbh.cm, ufc$height.m,
col = ifelse(ufc$height.m > 15, "darkseagreen4", "red"),
pch = ifelse(ufc$height.m > 15, 1, 3))
points(ufc$dbh.cm, ufc$height.m,
col = ifelse(ufc$height.m > 20, "darkseagreen4", "red"),
pch = ifelse(ufc$height.m > 15, 1, 10))
axis(1)
axis(2)
opar2 <- par(las = 0)
mtext("Diameter(cm)", side1 = , line = 3)
mtext("Diameter(cm)", side = 1 , line = 3)
mtext("Diameter(cm)", side = 1 , line = 5)
mtext("Diameter(cm)", side = 1 , line = 20)
mtext("Diameter(cm)", side = 1 , line = -1)
mtext("Height(m)", side = 2 , line = 3)
box()
legend(x = 60, y = 15,
c("Normal tress", "weird tree"),
col = c("darkseagreen", "red"),
pch = c(1,3))
legend(x = 60, y = 15,
c("Normal tress", "weird tree"),
col = c("darkseagreen", "red"),
pch = c(1,3),
bty = "n")
par(opar1)
savehistory(file = "history4")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr18.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr18.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr18.r")
contourplot(vol.m3.ha ~ east * north,
main = expression(paste("Volume (", m^3, ha^{-1}, ")", sep = "")),
xlab = "East (m)", ylab = "North (m)",
region = TRUE,
aspect = "iso",
col.regions = gray((11:1)/11),
data = ufc.plots)
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch1/pr18.r")
contourplot(vol.m3.ha ~ east * north,
main = expression(paste("Volume (", m^3, ha^{-1}, ")", sep = "")),
xlab = "East (m)", ylab = "North (m)",
region = TRUE,
aspect = "iso",
col.regions = gray((11:1)/11),
data = ufc.plots)
contourplot(vol.m3.ha ~ east * north,
main = expression(paste("Volume (", m^3, ha^{-1}, ")", sep = "")),
xlab = "East (m)", ylab = "North (m)",
region = FALSE,
aspect = "iso",
col.regions = gray((11:1)/11),
data = ufc.plots)
contourplot(vol.m3.ha ~ east * north,
main = expression(paste("Volume (", m^3, ha^{-1}, ")", sep = "")),
xlab = "East (m)", ylab = "North (m)",
region = TRUE,
aspect = "iso",
col.regions = gray((11:1)/11),
data = ufc.plots)
contourplot(vol.m3.ha ~ east * north,
main = expression(paste("Volume (", m^3, ha^{-1}, ")", sep = "")),
xlab = "East (m)", ylab = "North (m)",
region = TRUE,
aspect = "iso",
col.regions = red((11:1)/11),
data = ufc.plots)
contourplot(vol.m3.ha ~ east * north,
main = expression(paste("Volume (", m^3, ha^{-1}, ")", sep = "")),
xlab = "East (m)", ylab = "North (m)",
region = TRUE,
aspect = "iso",
col.regions = blues9((11:1)/11),
data = ufc.plots)
wireframe(vol.m3.ha ~ east * north,
main = expression(paste("Volume (", m^3, ha^{-1}, ")", sep = "")),
xlab = "East (m)", ylab = "North (m)",
data = ufc.plots)
demo(persp)
demo(image)
demo(graphics)
savehistory(file = "history5")
rm(list = ls())
getwd()
setwd("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2")
getwd()
n <- 10000
x <- rep(0, n)
system.time(for (i in 1:n) x[i] <- i^2)
x <- c()
system.time(for (i in 1:n) x[i] <- i^2)
big.matrix <- matrix(1:1e6, nrow = 1000)
colsums <- rep(NA, dim(big.matrix)[2])
system.time({
for (i in 1:dim(bog.matrix)[2]) {
s <- 0
for (j in 1:dim(big.matrix)[2])
system.time({
for (i in 1:dim(big.matrix)[2]) {
s <- 0
for (j in 1:dim(big.matrix)[1]) {
s <- s + big.matrix[j, i]
}
colsums[i] <- s
}
})
system.time(
colsums <- apply(big.matrix, 2, sum)
)
options(digits.secs = 6)
system.time(
colsums <- apply(big.matrix, 2, sum)
)
options(digits.secs = 12)
system.time(
colsums <- apply(big.matrix, 2, sum)
)
options(digits.secs = 3)
system.time(
colsums <- apply(big.matrix, 2, sum)
)
system.time(
colsums <- appl(big.matrix)
system.time(
colsums <- colSums(big.matrix)
)
options(digits.secs = 6)
system.time(
colsums <- colSums(big.matrix)
0
options(digits.secs = 6)
system.time(
colsums <- colSums(big.matrix)
)
system.time(
for(i in 1:dim(big.matrix)[2]){
colsum\
system.time(
for(i in 1:dim(big.matrix)[2]){
colsums[i] <- sum(big.matrix[,i])
}
)
system.time(
colsums <- apply(big.matrix, 2, sum)
)
options(digits.secs = 6)
system.time(
system.time(
colsums <- apply(big.matrix, 2, sum)
)
system.time(
colsums <- apply(big.matrix, 2, sum)
)
big.matrix <- matrix(1:1e6, nrow = 1000)
colsums <- rep(NA, dim(big.marix)[2])
big.matrix <- matrix(1:1e6, nrow = 1000)
colsums <- rep(NA, dim(big.matrix)[2])
system.time({}
big.matrix <- matrix(1:1e6, nrow = 1000)
colsums <- rep(NA, dim(big.matrix)[2])
system.time({
for(i in 1:dim(big.matrix)[2]) {
s <- 0
for (j in 1:dim(big.matrix)[1]) {
s <- s + big.matrix[j, i]
}
colsums[i] <- s
}
})
system.time(
colsums <- apply(big.matrix, 2, sum)
)
system.time(
for(i in 1:dim(big.matrix)[2]) {
colsums[i] <- sum(big.matrix[,i])
}
)
system.time(
colsums <- colSums(big.matrix)
)
rm(list = ls())
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr00.r")
rm(list = ls())
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr00.r")
ftn1 <- function(x) return(exp(exp(-x)))
fixedpoint(ftn1, 2, tol = 1e-6)
ftn2 <- function(x) return(x-log(x)+exp(-x))
fixedpoint(ftn2, 2, tol = 1e-6)
ftn3 <- function(x) return(x+log(x)-exp(-x))
fixedpoint(ftn3, 2, tol = 1e-6)
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr01.r")
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr01.r")
ftn4 <- function(x) {
fx <- log(x) - exp(-x)
dfx <- 1/x + exp(-x)
return(c(fx, dfx))
}
newtonraphson(ftn4, 2, 1e-6)
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr02.r")
ftn5 <- function(x) return(log(x)-exp(-x))
secant(ftn5, 2, 1e-6)
secant(ftn5, 2, 1e-6)
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr02.r")
secant(ftn5, 2, 1e-6)
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr03.r")
ftn5 <- function(x) return(log(x)-exp(-x))
bisection(ftn5, 1, 2, 1e-6)
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr03.r")
bisection(ftn5, 1, 2, 1e-6)
bisection(ftn5, 1, 10, 1e-6)
bisection(ftn5, 2, 10, 1e-6)
bisection(ftn5, 1, 1.5, 1e-6)
bisection(ftn5, 1, 2, 1e-3)
source("D:/FaraDars.org/T1365/FVR97041/source/codes/ch2/pr04.r")
ftn6 <- function(x) return(4*x^3)
trapezoid(ftn6, 0, 1, n = 20)
trapezoid(ftn6, 0, 1, n = 50)
trapezoid(ftn6, 0, 1, n = 100)
rm(list = ls())
savehistory(file= "history1")
