forked from theofpa/datascience
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek3-videos.R
68 lines (53 loc) · 1.34 KB
/
week3-videos.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
x<-list(a=1:5, b=rnorm(10))
lapply(x,mean)
x<-list(a=1:4, b=rnorm(10), c=rnorm(20,1), d=rnorm(100,5))
lapply(x,mean)
x<-1:4
lapply(x,runif)
lapply(x,runif,min=0,max=10)
x<-list(a=matrix(1:4,2,2),b=matrix(1:6,3,2))
lapply(x,function(elt) elt[,1])
x<-list(a=1:4, b=rnorm(10), c=rnorm(20,1), d=rnorm(100,5))
sapply(x,mean)
x<-matrix(rnorm(200),20,10)
apply(x,2,mean)
apply(x,1,sum)
rowSums=apply(x,1,sum)
rowMeans=apply(x,2,mean)
colSums=apply(x,2,sum)
colMeans=apply(x,2,mean)
x<-matrix(rnorm(200),20,10)
apply(x,1,quantile,probs=c(0.25,0.75))
a<-array(rnorm(2*2*10),c(2,2,10))
apply(a,c(1,2),mean)
rowMeans(a,dims=2)
x<-c(rnorm(10),runif(10),rnorm(10,1))
f<-gl(3,10)
tapply(x,f,mean)
tapply(x,f,mean,simplify=FALSE)
tapply(x,f,range)
split(x,f)
lapply(split(x,f),mean)
library(datasets)
head(airquality)
s<-split(airquality,airquality$Month)
lapply(s,function(x) colMeans(x[,c("Ozone","Solar.R","Wind")]))
sapply(s,function(x) colMeans(x[,c("Ozone","Solar.R","Wind")]))
sapply(s,function(x) colMeans(x[,c("Ozone","Solar.R","Wind")],na.rm=TRUE))
x<-norm(10)
f1<-gl(2,5)
f2<-gl(5,2)
f1
f2
interaction(f1,f2)
str(split(x,list(f1,f2)))
str(split(x,list(f1,f2),drop=TRUE))
list(rep(1,4),rep(2,3),rep(3,2),rep(4,1))
mapply(rep,1:4,4:1)
# function vectorization
noise<-function(n,mean,sd){
rnorm(n,mean,sd)
}
noise(5,1,2)
noise(1:5,5:1,2)
mapply(noise,1:5,5:1,2)