This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
2+2
[1] 4
plot(cars)
# 年复利
t=0:10
r=0.05
n=1000*(1+r)^t
n
[1] 1000.000 1050.000 1102.500 1157.625 1215.506 1276.282 1340.096 1407.100 1477.455 1551.328 1628.895
plot(t, n, type="l")
#驼鹿密度
moose.density = c(.17, .23, .23, .26, .37, .42, .66, .80, 1.11, 1.30, 1.37, 1.41, 1.73, 2.49)
kill.rate = c(.37, .47, 1.90, 2.04, 1.12, 1.74, 2.78, 1.85, 1.88, 1.96, 1.80, 2.44, 2.81, 3.75)
#plot(moose.density, kill.rate, type="p")
m=2.5*(0:100)/100
m
[1] 0.000 0.025 0.050 0.075 0.100 0.125 0.150 0.175 0.200 0.225 0.250 0.275 0.300 0.325 0.350 0.375 0.400 0.425 0.450 0.475 0.500 0.525 0.550 0.575
[25] 0.600 0.625 0.650 0.675 0.700 0.725 0.750 0.775 0.800 0.825 0.850 0.875 0.900 0.925 0.950 0.975 1.000 1.025 1.050 1.075 1.100 1.125 1.150 1.175
[49] 1.200 1.225 1.250 1.275 1.300 1.325 1.350 1.375 1.400 1.425 1.450 1.475 1.500 1.525 1.550 1.575 1.600 1.625 1.650 1.675 1.700 1.725 1.750 1.775
[73] 1.800 1.825 1.850 1.875 1.900 1.925 1.950 1.975 2.000 2.025 2.050 2.075 2.100 2.125 2.150 2.175 2.200 2.225 2.250 2.275 2.300 2.325 2.350 2.375
[97] 2.400 2.425 2.450 2.475 2.500
a=3.37
b=0.47
k=a*m/(b+m)
k
[1] 0.0000000 0.1702020 0.3240385 0.4637615 0.5912281 0.7079832 0.8153226 0.9143411 1.0059701 1.0910072 1.1701389 1.2439597 1.3129870 1.3776730
[15] 1.4384146 1.4955621 1.5494253 1.6002793 1.6483696 1.6939153 1.7371134 1.7781407 1.8171569 1.8543062 1.8897196 1.9235160 1.9558036 1.9866812
[29] 2.0162393 2.0445607 2.0717213 2.0977912 2.1228346 2.1469112 2.1700758 2.1923792 2.2138686 2.2345878 2.2545775 2.2738754 2.2925170 2.3105351
[43] 2.3279605 2.3448220 2.3611465 2.3769592 2.3922840 2.4071429 2.4215569 2.4355457 2.4491279 2.4623209 2.4751412 2.4876045 2.4997253 2.5115176
[57] 2.5229947 2.5341689 2.5450521 2.5556555 2.5659898 2.5760652 2.5858911 2.5954768 2.6048309 2.6139618 2.6228774 2.6315851 2.6400922 2.6484055
[71] 2.6565315 2.6644766 2.6722467 2.6798475 2.6872845 2.6945629 2.7016878 2.7086639 2.7154959 2.7221881 2.7287449 2.7351703 2.7414683 2.7476424
[85] 2.7536965 2.7596339 2.7654580 2.7711720 2.7767790 2.7822820 2.7876838 2.7929872 2.7981949 2.8033095 2.8083333 2.8132689 2.8181185 2.8228843
[99] 2.8275685 2.8321732 2.8367003
plot(moose.density, kill.rate, type="p")
points(m, k, type="l")
#America Population
length=(2000-1790)/10
year = 1790 + 10*c(0:length)
population=c(39, 53, 72, 96, 128, 170, 231, 314, 385, 501, 629, 762, 922, 1060, 1232, 1421, 1613, 1893, 2133, 2365, 2587, 2914)
plot(year, population, type="p")
plot(year, population, type="b")
plot(year, population, type="c")
plot(year, population, type="o")
plot(year, population, type="h")
plot(year, population, type="l")
NA
NA
NA
t=0:4.09
x=27.12*t
y=1.524+19.71*t-4.905*t^2
plot(x, y, type="o")
NA
NA
NA
#Function
length.hyp=function(x){
h=sqrt(sum(x^2))
return(h)
}
sides = c(3,4)
length.hyp(sides)
[1] 5
#----------
num.atoms = c(2,1)
atomic.weights=c(1.008,16.00)
molar.mass=function(x,y){
mm=sum(x*y)
return(mm)
}
molar.mass(num.atoms,atomic.weights)
[1] 18.016
source(file = "economics data.R")
stripchart(unemploy, xlab = "Percentage civilian unemployment 1960-2010", main = "Unemploy Rate", method = "stack", pch = 1, cex = 3)
hist(unemploy, main="Unemploy Rate", xlab = "Percentage civilian unemployment 1960-2010", ylab = "denisty", breaks = c(3,5,7,9,10))
hist(unemploy, main="Unemploy Rate", xlab = "Percentage civilian unemployment 1960-2010", ylab = "denisty")
stem(unemploy)
The decimal point is at the |
3 | 5688
4 | 0255667999
5 | 12345555666678889
6 | 0112789
7 | 01125567
8 | 5
9 | 3667
boxplot(unemploy, main="Main Title", xlab = "xlab description", ylab="ylab description")
plot(year, unemploy, type = "o", xlab = "xlab description", ylab = "ylab description", main = "main title")
plot(surplus, unemploy, type = "p", ylab = "ylab description")
boxplot(unemploy~party, range=0, names=c("Democratic", "Republican"), boxwex=.5, ylab="ylab description")
barplot(unemploy[party=="D"])
percentage=c(length(unemploy[party=="D"]),length(unemploy[party=="R"]))
pie(percentage, labels = c("D", "R"))
NA
NA
#loops
num.fibs=50
r=numeric(num.fibs)
r[1]=1
r[2]=1
for(i in 2:(num.fibs-1)){
r[i+1]=r[i]+r[i-1]
}
r
[1] 1 1 2 3 5 8 13 21 34 55 89 144
[13] 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368
[25] 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352
[37] 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976
[49] 7778742049 12586269025
num.times=20
p0=.11
p1=.71
p2=.94
f=.24
J.t=numeric(num.times)
S.t=J.t
A.t=J.t
J.t[1]=1200
S.t[1]=800
A.t[1]=2000
for (i in 1:(num.times-1)){
J.t[i+1]=f*A.t[i]
S.t[i+1]=p0*J.t[i]
A.t[i+1]=p1*S.t[i]+p2*A.t[i]
}
time.t=0:(num.times-1)
plot(time.t, J.t, type="o", lty=2,main="Forecasting", xlab="time in years", ylab="population size", ylim=c(0,2600))
points(time.t, S.t, type="o", lty=5)
points(time.t, A.t, type="o", lty=1)
NA
NA
outcomes=function(n,p){
u=runif(n)
x=numeric(n)
for (i in 1:n) {
if(u[i]<=p) x[i]=1 else x[i]=0
}
return(x)
}
n=30
p=0.26
num.sets=100
bat.ave=numeric(num.sets)
for (i in 1:num.sets) {
bat.ave[i]=sum(outcomes(n,p))/n
}
bat.ave
[1] 0.1666667 0.2333333 0.3000000 0.1333333 0.3000000 0.3000000 0.2333333 0.2333333 0.2333333 0.1333333 0.2666667 0.3000000 0.2333333 0.3666667
[15] 0.2333333 0.3000000 0.2333333 0.4333333 0.2000000 0.2000000 0.2000000 0.2333333 0.2666667 0.2333333 0.2000000 0.2000000 0.3000000 0.4000000
[29] 0.4333333 0.1666667 0.2000000 0.2000000 0.1333333 0.2000000 0.4333333 0.4000000 0.2333333 0.2666667 0.1666667 0.2333333 0.2666667 0.2000000
[43] 0.3000000 0.1333333 0.2333333 0.1666667 0.2333333 0.1666667 0.1666667 0.3666667 0.2666667 0.3333333 0.2333333 0.3000000 0.2666667 0.2000000
[57] 0.2333333 0.3000000 0.2666667 0.1333333 0.2000000 0.2333333 0.3333333 0.3333333 0.3000000 0.2666667 0.1333333 0.2333333 0.2000000 0.2000000
[71] 0.3666667 0.3000000 0.3000000 0.3333333 0.2666667 0.2666667 0.2666667 0.4000000 0.4333333 0.1000000 0.3000000 0.1666667 0.2333333 0.3333333
[85] 0.3000000 0.2333333 0.1666667 0.3000000 0.2333333 0.2666667 0.2000000 0.2666667 0.2333333 0.3333333 0.2333333 0.3333333 0.3333333 0.1333333
[99] 0.2000000 0.2666667
hist(bat.ave)
stem(bat.ave)
The decimal point is 1 digit(s) to the left of the |
1 | 03333333
1 | 77777777
2 | 0000000000000003333333333333333333333
2 | 77777777777777
3 | 00000000000000033333333
3 | 777
4 | 0003333
xlo1=-1
xhi1=2
x1=xlo1+(xhi1-xlo1)*(0:100)/100
y1=x1^2-x1-1
plot(x1,y1,type="l")
y21=numeric(length(x))
points(x1,y21, type = "l", lty="dashed")
xlo=1.61803
xhi=1.61804
x=xlo+(xhi-xlo)*(0:100)/100
y=x^2-x-1
plot(x,y,type="l")
y2=numeric(length(x))
points(x,y2, type = "l", lty="dashed")
theta=c(0, (1/4)*pi, (2/4)*pi, (3/4)*pi, pi, (5/4)*pi, (6/4)*pi, (7/4)*pi, 2*pi)
sin(theta)
[1] 0.000000e+00 7.071068e-01 1.000000e+00 7.071068e-01 1.224606e-16 -7.071068e-01 -1.000000e+00 -7.071068e-01 -2.449213e-16
cos(theta)
[1] 1.000000e+00 7.071068e-01 6.123032e-17 -7.071068e-01 -1.000000e+00 -7.071068e-01 -1.836910e-16 7.071068e-01 1.000000e+00
tan(theta)
[1] 0.000000e+00 1.000000e+00 1.633124e+16 -1.000000e+00 -1.224647e-16 1.000000e+00 5.443746e+15 -1.000000e+00 -2.449294e-16
plot(sin(theta), type = "o")
y=numeric(length(theta))
points(y,type = "l", lty="dashed")
th.lo=-4*pi
th.hi=4*pi
theta1=th.lo+(th.hi-th.lo)*(0:1000)/1000
y1=sin(theta1)
y2=cos(theta1)
plot(theta1, y1, type="l", lty=1, ylim=c(-2,2), xlab="theta", ylab="Sineand Cosine")
points(theta1, y2, type="l", lty=2)
y3=tan(theta1)
plot(theta1, y3, type = "p", ylim = c(-2,2))
NA
NA
NA
thetha=2*pi*(0:100)/100
r=1
x=r*cos(thetha)
y=r*sin(thetha)
par(pin=c(4,4))
plot(x,y, type="l")
NA
NA
b=sum(x1*x2)
b
[1] 212
A=matrix(scan("data.txt"), nrow = 3, ncol = 5, byrow = TRUE)
Read 15 items
A
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
[3,] 11 12 13 14 15
B=matrix(scan("data.txt"), nrow = 5, ncol = 3)
Read 15 items
B
[,1] [,2] [,3]
[1,] 1 6 11
[2,] 2 7 12
[3,] 3 8 13
[4,] 4 9 14
[5,] 5 10 15
A=rbind(c(-1,4),c(3,6))
c=c(8,30)
x=solve(A,c)
Ainv=solve(A)
x
[1] 4 3
Ainv
[,1] [,2]
[1,] -0.3333333 0.22222222
[2,] 0.1666667 0.05555556
Ainv%*%c
[,1]
[1,] 4
[2,] 3
Ainv%*%A
[,1] [,2]
[1,] 1 2.220446e-16
[2,] 0 1.000000e+00
x=(0:100)*2*pi/100
y1=sin(x)
y2=cos(x)
y=cbind(y1,y2)
matplot(x,y)
matplot(x,y,type="l")
matplot(x,y,type="l",col = "black")
matplot(x,y,type="h")
legend(3.5, 0.75, c("sine","cosine"),lty = c(1,2))
matplot(x,y,type="l",lty=c(1,5))
matplot(x,y,type="s")
matplot(x,y,pch = 20,axes=FALSE,ann=FALSE, col = c("red","blue"))
legend(3.5, 0.75, c("sine","cosine"),pch = 20, col = c("red","blue"))
NA
NA
x0=c(2,2,3,4,4)
y0=c(2,3,2,2,3.75)
x1=c(2,3,3,4,4)
y1=c(4,3,4,3,4)
x=c(0,3)
y=c(0,3)
plot(x,y, type = "l", lty=1, xlim = c(0,5), ylim = c(0,5))
segments(x0,y0,x1,y1,col = "blue")
abline(h=1, lty=2,col="red")
abline(v=1, lty=2,col="red")
abline(2,1)
abline(-2,1)
text(x[2],y[2],c("I am here"), pos=3)
title(main = "WoW What a plot", sub="and the lables rock too", cex.main=2, cex.sub=.8)
plot(x0,y0, type = "o", lty=1, col="blue", xlim = c(0,10), ylim = c(0,5))
legend(5,4, "Interesting Legend",lty=1, col = "blue")
xhi=2
xlo=-1
x=xlo+(xhi-xlo)*(0:1000)/1000
layout(matrix(c(1,2,3,4),2,2))
#plot 1
y=x^2-x-1
plot(x,y,type="l",ylim = c(-2,2))
text(-.75,1.75,"a")
y2=numeric(length(x))
points(x,y2,type = "l",lty=2)
#plot 2
y=-x^2 +x +1
plot(x,y,type="l",ylim = c(-2,2))
text(-.75,1.75,"b")
y2=numeric(length(x))
points(x,y2,type = "l",lty=2)
#plot 3
y=x^2-x +1
plot(x,y,type="l",ylim = c(-2,2))
text(-.75,1.75,"c")
y2=numeric(length(x))
points(x,y2,type = "l",lty=2)
#plot 4
y=-x^2+x-1
plot(x,y,type="l",ylim = c(-2,2))
text(-.75,1.75,"d")
y2=numeric(length(x))
points(x,y2,type = "l",lty=2)