# random number generators
x=rnorm(n=1,mean=100,sd=15)
y=runif(n=3,min=1,max=10)
N = 10000
# N = 1000
random.data = rnorm(N, mean=0, sd=1)
hist(random.data, nclass = 50, col = "navy", xlab = "Data",
probability = T, main = "Histogram of Random Data")
# density of generated data
dens = density(random.data)
lines(dens, col = "orange", lwd = 4)
# theoretical density
x = seq(-4,4,0.1)
true.norm = dnorm(x, mean = 0, sd = 1)
lines(x,true.norm, col = "green", lty = 3, lwd = 4)
legend("topleft",c("empirical", "theoretical"), lty = c(1,3),
col = c('orange','green'),lwd=4)
# random sampling
sample(1:10,3)
sample(c(“gu”,“choki”,“pa”),1)
sample(1:10)
sample(0:1, 10, replace=T)
sample(c("Head","Tail"), 10, replace=T)
sample(c("Head","Tail"), 10, replace=T, prob=c(0.9,0.1))
# flow control
for (i_loop in 1:5){print(i_loop)}
counter <- 1
while(counter<=10){
print(counter)
counter<-counter+1
}
counter <- 1
while(counter^2 <= 10){
print(c(counter, counter^2))
counter<-counter+1
}
affil<-"cogsci"
if (affil=="cogsci") {
print("you are wonderful")
}
affil<-"phil"
if (affil=="cogsci") {
print("you are wonderful")
} else {
print("still, you are wonderful")
}
v1=1633;v2=355;
repeat {
r=v1%%v2
print(paste('v1 =',v1,v2 = ',v2, remainder = ',r))
v1=v2;v2=r
if (r==0){ break}
}
counter=6
repeat{
print(counter)
counter = counter + 1
if(counter>5){break}
}
counter=6
repeat{
if(counter>5){break}
print(counter)
counter+counter+1
}
Related