Está en la página 1de 27

> # MATRICES

>
> m1 = matrix(data=1:9, ncol=3)
> m1
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
> m2 = matrix(data=1:9, ncol=3, byrow=TRUE)
> m2
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
[3,]
7
8
9
>
> # matrix glueing
> cbind(m1,m2)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]
1
4
7
1
2
3
[2,]
2
5
8
4
5
6
[3,]
3
6
9
7
8
9
> rbind(m1,m2)
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
[4,]
1
2
3
[5,]
4
5
6
[6,]
7
8
9
>
> # matrices are treated just like vectors
> m1
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
> m2
[,1] [,2] [,3]
[1,]
1
2
3
[2,]
4
5
6
[3,]
7
8
9
> m1+m2
[,1] [,2] [,3]
[1,]
2
6 10
[2,]
6 10 14
[3,] 10 14 18
> m1-m2
[,1] [,2] [,3]
[1,]
0
2
4
[2,] -2
0
2
[3,] -4 -2
0
> m1*m2
[,1] [,2] [,3]
[1,]
1
8 21
[2,]
8 25 48
[3,] 21 48 81
> m1/m2
[,1] [,2]
[,3]
[1,] 1.0000000 2.00 2.333333
[2,] 0.5000000 1.00 1.333333

[3,] 0.4285714 0.75 1.000000


> m1^2
[,1] [,2] [,3]
[1,]
1 16 49
[2,]
4 25 64
[3,]
9 36 81
> 1/m1
[,1]
[,2]
[,3]
[1,] 1.0000000 0.2500000 0.1428571
[2,] 0.5000000 0.2000000 0.1250000
[3,] 0.3333333 0.1666667 0.1111111
> exp(-m1)
[,1]
[,2]
[,3]
[1,] 0.36787944 0.018315639 0.0009118820
[2,] 0.13533528 0.006737947 0.0003354626
[3,] 0.04978707 0.002478752 0.0001234098
>
> # matricial product
> m1 %*% m2
[,1] [,2] [,3]
[1,] 66 78 90
[2,] 78 93 108
[3,] 90 108 126
>
> # rows and columns can be indexed by names too
> m1
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
> dimnames(m1) = list( c('a', 'b', 'c'),
+
c('x', 'y', 'z') )
> m1
x y z
a 1 4 7
b 2 5 8
c 3 6 9
> dimnames(m1) = NULL
> m1
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
>
> # which entries do hold a condition?
> which( m1 > 5 )
[1] 6 7 8 9
> which( m1 > 5, arr.ind=TRUE )
row col
[1,] 3 2
[2,] 1 3
[3,] 2 3
[4,] 3 3
>
>
> # apply a function to rows or columns of a matrix
> m1
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8

[3,]
3
6
9
> sum(m1) # treats like vector
[1] 45
> apply( X=m1, MARGIN=1, FUN='max' )
[1] 7 8 9
> apply( X=m1, MARGIN=2, FUN='max' )
[1] 3 6 9
>
> # EXERCICE: put the seed
> set.seed(20120228)
> # If the matrix
> m3 = matrix(data=sample(x=0:1, size=50*10,
+
replace=TRUE, prob=c(4,6)),
+
ncol=10)
> # stores the score (success/fail) of 50 students (rows)
> # under 10 questions (columns). Compute:
> # (1) vector with the score of all students (score = sum of succeeded questi
ons / number of question * 100)
> # (2) vector with % of students succeeding each question
> m3
apply( X=m3, MARGIN=1, FUN='sum' )
> apply( X=m3, MARGIN=1, FUN='sum' )
[1] 8 4 7 7 7 8 7 8 6 5 9 4 7 9 4 8 6 2 5 8 5 8 7 6 6 6 5 4 7 6 6 9 5 5 3 7 4 7
9 7 7 6 6 5 5 7 4 4 4 6
> v=apply( X=m3, MARGIN=1, FUN='sum' )
> v
[1] 8 4 7 7 7 8 7 8 6 5 9 4 7 9 4 8 6 2 5 8 5 8 7 6 6 6 5 4 7 6 6 9 5 5 3 7 4 7
9 7 7 6 6 5 5 7 4 4 4 6
>

> # vectors can be indexed by names


> point = c(x=3, y=5)
> point
x y
3 5
> point[1]
x
3
> point['x']
x
3
> names(point)
[1] "x" "y"
> names(point) = c('u', 'v')
> point
u v
3 5
>
> # ORDERING VECTORS
>
> v = c(5, pi, exp(1), -4, -Inf, 0)
> sort(x=v)
[1]
-Inf -4.000000 0.000000 2.718282 3.141593 5.000000

> order(x=v)
[1] 5 4 6 3 2 1
> sort(x=v, decreasing=TRUE)
[1] 5.000000 3.141593 2.718282 0.000000 -4.000000
-Inf
> order(x=v, decreasing=TRUE)
[1] 1 2 3 6 4 5
>
> # which components do hold a condition?
> v
[1] 5.000000 3.141593 2.718282 -4.000000
-Inf 0.000000
> which( v < 0 )
[1] 4 5
>
> # EXERCICE: Run the code
> a = rnorm(1000,5,2)
> # and create a new vector by truncating values
> # so that all negative values become "0" and
> # numbers larger than "10" become "10"
>
> # PLOTTING VECTORS
>
> # show some graphics capabilities of R
> demo(graphics)
demo(graphics)
---- ~~~~~~~~
Type <Return>

to start :

> # Copyright (C) 1997-2009 The R Core Team


>
> require(datasets)
> require(grDevices); require(graphics)
>
>
>
>
>
>
>
>
>

##
##
##
##
##
##

Here is some code which illustrates some of the differences between


R and S graphics capabilities. Note that colors are generally specified
by a character string name (taken from the X11 rgb.txt file) and that line
textures are given similarly. The parameter "bg" sets the background
parameter for the plot and there is also an "fg" parameter which sets
the foreground color.

x <- stats::rnorm(50)

> opar <- par(bg = "white")


> plot(x, ann = FALSE, type = "n")
Esperando para confirmar cambio de pgina...
> abline(h = 0, col = gray(.90))
> lines(x, col = "green4", lty = "dotted")
> points(x, bg = "limegreen", pch = 21)
> title(main = "Simple Use of Color In a Plot",
+
xlab = "Just a Whisper of a Label",
+
col.main = "blue", col.lab = gray(.8),

+
>
>
>
>
>
>
>
>

cex.main = 1.2, cex.lab = 1.0, font.main = 4, font.lab = 3)


##
##
##
##
##
##

A little color wheel.


This code just plots equally spaced hues in
a pie chart.
If you have a cheap SVGA monitor (like me) you will
probably find that numerically equispaced does not mean visually
equispaced. On my display at home, these colors tend to cluster at
the RGB primaries. On the other hand on the SGI Indy at work the
effect is near perfect.

par(bg = "gray")

> pie(rep(1,24), col = rainbow(24), radius = 0.9)


Esperando para confirmar cambio de pgina...
> title(main = "A Sample Color Wheel", cex.main = 1.4, font.main = 3)
> title(xlab = "(Use this as a test of monitor linearity)",
+
cex.lab = 0.8, font.lab = 3)
> ## We have already confessed to having these. This is just showing off X11
> ## color names (and the example (from the postscript manual) is pretty "cute".
>
> pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
> names(pie.sales) <- c("Blueberry", "Cherry",
+
"Apple", "Boston Cream", "Other", "Vanilla Cream")
> pie(pie.sales,
+
col = c("purple","violetred1","green3","cornsilk","cyan","white"))
Esperando para confirmar cambio de pgina...
> title(main = "January Pie Sales", cex.main = 1.8, font.main = 1)
> title(xlab = "(Don't try this at home kids)", cex.lab = 0.8, font.lab = 3)
>
>
>
>
>

## Boxplots: I couldn't resist the capability for filling the "box".


## The use of color seems like a useful addition, it focuses attention
## on the central bulk of the data.
par(bg="cornsilk")

> n <- 10
> g <- gl(n, 100, n*100)
> x <- rnorm(n*100) + sqrt(as.numeric(g))
> boxplot(split(x,g), col="lavender", notch=TRUE)
Esperando para confirmar cambio de pgina...
> title(main="Notched Boxplots", xlab="Group", font.main=4, font.lab=1)
> ## An example showing how to fill between curves.
>
> par(bg="white")
> n <- 100
> x <- c(0,cumsum(rnorm(n)))

> y <- c(0,cumsum(rnorm(n)))


> xx <- c(0:n, n:0)
> yy <- c(x, rev(y))
> plot(xx, yy, type="n", xlab="Time", ylab="Distance")
Esperando para confirmar cambio de pgina...
> polygon(xx, yy, col="gray")
> title("Distance Between Brownian Motions")
>
>
>
>
>
>

##
##
##
##

Colored plot
careful with
over the top
On the other

margins, axis labels and titles.


You do need to be
these kinds of effects.
It's easy to go completely
and you can end up with your lunch all over the keyboard.
hand, my market research clients love it.

x <- c(0.00, 0.40, 0.86, 0.85, 0.69, 0.48, 0.54, 1.09, 1.11, 1.73, 2.05, 2.02)

> par(bg="lightgray")
> plot(x, type="n", axes=FALSE, ann=FALSE)
Esperando para confirmar cambio de pgina...
> usr <- par("usr")
> rect(usr[1], usr[3], usr[2], usr[4], col="cornsilk", border="black")
> lines(x, col="blue")
> points(x, pch=21, bg="lightcyan", cex=1.25)
> axis(2, col.axis="blue", las=1)
> axis(1, at=1:12, lab=month.abb, col.axis="blue")
> box()
> title(main= "The Level of Interest in R", font.main=4, col.main="red")
> title(xlab= "1996", col.lab="red")
> ## A filled histogram, showing how to change the font used for the
> ## main title without changing the other annotation.
>
> par(bg="cornsilk")
> x <- rnorm(1000)
> hist(x, xlim=range(-4, 4, x), col="lavender", main="")
Esperando para confirmar cambio de pgina...
> title(main="1000 Normal Random Variates", font.main=3)
> ## A scatterplot matrix
> ## The good old Iris data (yet again)
>
> pairs(iris[1:4], main="Edgar Anderson's Iris Data", font.main=4, pch=19)
Esperando para confirmar cambio de pgina...

> pairs(iris[1:4], main="Edgar Anderson's Iris Data", pch=21,


+
bg = c("red", "green3", "blue")[unclass(iris$Species)])
Esperando para confirmar cambio de pgina...
> ## Contour plotting
> ## This produces a topographic map of one of Auckland's many volcanic "peaks".
>
> x <- 10*1:nrow(volcano)
> y <- 10*1:ncol(volcano)
> lev <- pretty(range(volcano), 10)
> par(bg = "lightcyan")
> pin <- par("pin")
> xdelta <- diff(range(x))
> ydelta <- diff(range(y))
> xscale <- pin[1]/xdelta
> yscale <- pin[2]/ydelta
> scale <- min(xscale, yscale)
> xadd <- 0.5*(pin[1]/scale - xdelta)
> yadd <- 0.5*(pin[2]/scale - ydelta)
> plot(numeric(0), numeric(0),
+
xlim = range(x)+c(-1,1)*xadd, ylim = range(y)+c(-1,1)*yadd,
+
type = "n", ann = FALSE)
Esperando para confirmar cambio de pgina...
> usr <- par("usr")
> rect(usr[1], usr[3], usr[2], usr[4], col="green3")
> contour(x, y, volcano, levels = lev, col="yellow", lty="solid", add=TRUE)
> box()
> title("A Topographic Map of Maunga Whau", font= 4)
> title(xlab = "Meters North", ylab = "Meters West", font= 3)
> mtext("10 Meter Contour Spacing", side=3, line=0.35, outer=FALSE,
+
at = mean(par("usr")[1:2]), cex=0.7, font=3)
> ## Conditioning plots
>
> par(bg="cornsilk")
> coplot(lat ~ long | depth, data = quakes, pch = 21, bg = "green3")
Esperando para confirmar cambio de pgina...
> par(opar)

> # now simple vector plotting


> v1 = 1:10
> v2 = 100 - v1^2
> plot(x=v1, y=v2)
Esperando para confirmar cambio de pgina...
> plot(x=v1, y=v2, type='l')
Esperando para confirmar cambio de pgina...
> plot(x=v1, y=v2, type='h')
Esperando para confirmar cambio de pgina...
> plot(x=v1, y=v2, type='s')
Esperando para confirmar cambio de pgina...
> help(plot) # for other arguments
starting httpd help server ... done
>
> # plot() opens a new window
> # points() adds points to an open window
> points(x=v1, y=v1^2, type='l', col='red')
>
>
> # EXERCICES:
> # (1) Compute the sum 1 + 2 + 3 + ... + 12345
> # (2) Compute the sum 1 + 1/3 + 1/5 + ... + 1/101
> # (3) Make a good plot of f(x) = sin(x) in the interval [0, pi]
> # (4) Compute the area under the previous function
> #
in the interval [0, pi], approximating the integral
> #
with a Riemann sum of 50 nodes
> # (5) Create a vector of 5000 continuous random numbers
> #
and show the largest 10 values and their positions
> #
within the vector
>

> # LISTS: GENERAL STRUCTURES


>
> l1 = list(1:10, m1, point)
> l1
[[1]]
[1] 1 2 3 4 5 6 7 8 9 10
[[2]]
[,1] [,2] [,3]
1
4
7
2
5
8
3
6
9

[1,]
[2,]
[3,]
[[3]]
u v
3 5

> l2 = list(numbers=1:10, system=m1, coord=point)


> l2
$numbers
[1] 1 2 3 4 5 6 7 8 9 10
$system
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9

$coord
u v
3 5
> names(l2)
[1] "numbers" "system" "coord"
> length(l2)
[1] 3
> str(l2)
List of 3
$ numbers: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ system : int [1:3, 1:3] 1 2 3 4 5 6 7 8 9
$ coord : Named num [1:2] 3 5
..- attr(*, "names")= chr [1:2] "u" "v"
>
> # sublists
>
> l2[1] # this is a sublist of 1 member
$numbers
[1] 1 2 3 4 5 6 7 8 9 10
> l2[ c(1,2) ] # this is a sublist of 2 members
$numbers
[1] 1 2 3 4 5 6 7 8 9 10
$system
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
> l2[ c('numbers', 'coord') ] # another sublist
$numbers
[1] 1 2 3 4 5 6 7 8 9 10
$coord
u v
3 5
>
> # accessing members of a list
>
> l2[[1]] # this is the 1st member: a vector
[1] 1 2 3 4 5 6 7 8 9 10
Con el doble corchete entro dentro de la lista y cojo el vector, as que es un vec
tor
> l2$system # this is the 'system' member: a matrix
[,1] [,2] [,3]
[1,]
1
4
7
[2,]
2
5
8
[3,]
3
6
9
Si pongo un corchete es una lista con el vector o la matriz

>
HOJA DE DATOS
> # AND FINALLY DATA FRAMES !!!!!
> # VARIABLE TYPE SUITABLE FOR MULTIVARIATE DATA
>
> d1 = data.frame(num=5:7, is=c(T,T,F), let=c('a','b','a'))
> d1
num
is let
1 5 TRUE a
2 6 TRUE b
3 7 FALSE a
si no le pongo etiquetas el r pone v1, v2, etc.

> names(d1)
[1] "num" "is" "let"
Nombres de las etiquetas
> row.names(d1)
[1] "1" "2" "3"
Nombre de las filas, se llaman 1, 2 y 3 porque nadie le ha dado nombres.
> str(d1)
'data.frame': 3 obs. of 3 variables:
$ num: int 5 6 7
$ is : logi TRUE TRUE FALSE
$ let: Factor w/ 2 levels "a","b": 1 2 1
Factor: Ordena las palabras por orden alfabtico y los pasa a nmeros
>
> # subscripting like matrices
>
> d1[1,]
num is let
1 5 TRUE a
> d1[,1]
[1] 5 6 7
> d1[ 2:3,2 ]
[1] TRUE FALSE
Quiero la 2 y 3 de filas y la segunda columna
>
>
>
>
>
1
2
3
4
5
6

data() # built-in data in R


data() # built-in data in R
data(ChickWeight)
ChickWeight
weight Time Chick Diet
42
0
1
1
51
2
1
1
59
4
1
1
64
6
1
1
76
8
1
1
93 10
1
1

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

106
125
149
171
199
205
40
49
58
72
84
103
122
138
162
187
209
215
43
39
55
67
84
99
115
138
163
187
198
202
42
49
56
67
74
87
102
108
136
154
160
157
41
42
48
60
79
106
141
164
197
199
220
223
41
49
59
74
97
124

12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10

1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
4
4
5
5
5
5
5
5
5
5
5
5
5
5
6
6
6
6
6
6

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

141
148
155
160
160
157
41
49
57
71
89
112
146
174
218
250
288
305
42
50
61
71
84
93
110
116
126
134
125
42
51
59
68
85
96
90
92
93
100
100
98
41
44
52
63
74
81
89
96
101
112
120
124
43
51
63
84
112
139
168

12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12

6
6
6
6
6
6
7
7
7
7
7
7
7
7
7
7
7
7
8
8
8
8
8
8
8
8
8
8
8
9
9
9
9
9
9
9
9
9
9
9
9
10
10
10
10
10
10
10
10
10
10
10
10
11
11
11
11
11
11
11

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

177
182
184
181
175
41
49
56
62
72
88
119
135
162
185
195
205
41
48
53
60
65
67
71
70
71
81
91
96
41
49
62
79
101
128
164
192
227
248
259
266
41
49
56
64
68
68
67
68
41
45
49
51
57
51
54
42
51
61
72

14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
0
2
4
6
8
10
12
0
2
4
6

11
11
11
11
11
12
12
12
12
12
12
12
12
12
12
12
12
13
13
13
13
13
13
13
13
13
13
13
13
14
14
14
14
14
14
14
14
14
14
14
14
15
15
15
15
15
15
15
15
16
16
16
16
16
16
16
17
17
17
17

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

83
89
98
103
113
123
133
142
39
35
43
48
55
62
65
71
82
88
106
120
144
157
41
47
54
58
65
73
77
89
98
107
115
117
40
50
62
86
125
163
217
240
275
307
318
331
41
55
64
77
90
95
108
111
131
148
164
167
43
52

8
10
12
14
16
18
20
21
0
2
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2

17
17
17
17
17
17
17
17
18
18
19
19
19
19
19
19
19
19
19
19
19
19
20
20
20
20
20
20
20
20
20
20
20
20
21
21
21
21
21
21
21
21
21
21
21
21
22
22
22
22
22
22
22
22
22
22
22
22
23
23

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

61
73
90
103
127
135
145
163
170
175
42
52
58
74
66
68
70
71
72
72
76
74
40
49
62
78
102
124
146
164
197
231
259
265
42
48
57
74
93
114
136
147
169
205
236
251
39
46
58
73
87
100
115
123
144
163
185
192
39
46

4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2

23
23
23
23
23
23
23
23
23
23
24
24
24
24
24
24
24
24
24
24
24
24
25
25
25
25
25
25
25
25
25
25
25
25
26
26
26
26
26
26
26
26
26
26
26
26
27
27
27
27
27
27
27
27
27
27
27
27
28
28

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366

58
73
92
114
145
156
184
207
212
233
39
48
59
74
87
106
134
150
187
230
279
309
42
48
59
72
85
98
115
122
143
151
157
150
42
53
62
73
85
102
123
138
170
204
235
256
41
49
65
82
107
129
159
179
221
263
291
305
39
50

4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2

28
28
28
28
28
28
28
28
28
28
29
29
29
29
29
29
29
29
29
29
29
29
30
30
30
30
30
30
30
30
30
30
30
30
31
31
31
31
31
31
31
31
31
31
31
31
32
32
32
32
32
32
32
32
32
32
32
32
33
33

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3

367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

63
77
96
111
137
144
151
146
156
147
41
49
63
85
107
134
164
186
235
294
327
341
41
53
64
87
123
158
201
238
287
332
361
373
39
48
61
76
98
116
145
166
198
227
225
220
41
48
56
68
80
83
103
112
135
157
169
178
41
49

4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2

33
33
33
33
33
33
33
33
33
33
34
34
34
34
34
34
34
34
34
34
34
34
35
35
35
35
35
35
35
35
35
35
35
35
36
36
36
36
36
36
36
36
36
36
36
36
37
37
37
37
37
37
37
37
37
37
37
37
38
38

3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3

427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486

61
74
98
109
128
154
192
232
280
290
42
50
61
78
89
109
130
146
170
214
250
272
41
55
66
79
101
120
154
182
215
262
295
321
42
51
66
85
103
124
155
153
175
184
199
204
42
49
63
84
103
126
160
174
204
234
269
281
42
55

4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2

38
38
38
38
38
38
38
38
38
38
39
39
39
39
39
39
39
39
39
39
39
39
40
40
40
40
40
40
40
40
40
40
40
40
41
41
41
41
41
41
41
41
41
41
41
41
42
42
42
42
42
42
42
42
42
42
42
42
43
43

3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4

487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546

69
96
131
157
184
188
197
198
199
200
42
51
65
86
103
118
127
138
145
146
41
50
61
78
98
117
135
141
147
174
197
196
40
52
62
82
101
120
144
156
173
210
231
238
41
53
66
79
100
123
148
157
168
185
210
205
39
50
62
80

4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6

43
43
43
43
43
43
43
43
43
43
44
44
44
44
44
44
44
44
44
44
45
45
45
45
45
45
45
45
45
45
45
45
46
46
46
46
46
46
46
46
46
46
46
46
47
47
47
47
47
47
47
47
47
47
47
47
48
48
48
48

4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4

547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578

104
125
154
170
222
261
303
322
40
53
64
85
108
128
152
166
184
203
233
237
41
54
67
84
105
122
155
175
205
234
264
264

8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21
0
2
4
6
8
10
12
14
16
18
20
21

48
48
48
48
48
48
48
48
49
49
49
49
49
49
49
49
49
49
49
49
50
50
50
50
50
50
50
50
50
50
50
50

4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4

El pollo 1 hasta el da 21

> help(ChickWeight)
> x = ChickWeight # just rename for short
> names(x) # labels of columns
[1] "weight" "Time" "Chick" "Diet"
> dim(x) # dimension: c(rows, columns)
[1] 578 4
> str(x) # summary of the structure of the variable
Classes nfnGroupedData , nfGroupedData , groupedData and 'data.frame':
578 obs. of
4 variables:
$ weight: num 42 51 59 64 76 93 106 125 149 171 ...
$ Time : num 0 2 4 6 8 10 12 14 16 18 ...
$ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 15 15 15 15 15 15 15 15 15
15 ...
$ Diet : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "formula")=Class 'formula' length 3 weight ~ Time | Chick
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "outer")=Class 'formula' length 2 ~Diet
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "labels")=List of 2
..$ x: chr "Time"
..$ y: chr "Body weight"

- attr(*, "units")=List of 2
..$ x: chr "(days)"
..$ y: chr "(gm)"

> # access to each column


> x$weight # equivalent to x[[1]]
[1] 42 51 59 64 76 93 106 125 149
2 138 162 187 209 215 43 39 55 67
[29] 84 99 115 138 163 187 198 202 42
0 157 41 42 48 60 79 106 141 164
[57] 197 199 220 223 41 49 59 74 97
7 71 89 112 146 174 218 250 288 305
[85] 42 50 61 71 84 93 110 116 126
2 93 100 100 98 41 44 52 63 74
[113] 81 89 96 101 112 120 124 43 51
5 41 49 56 62 72 88 119 135 162
[141] 185 195 205 41 48 53 60 65 67
9 101 128 164 192 227 248 259 266 41
[169] 49 56 64 68 68 67 68 41 45
3 89 98 103 113 123 133 142 39 35
[197] 43 48 55 62 65 71 82 88 106
7 89 98 107 115 117 40 50 62 86
[225] 125 163 217 240 275 307 318 331 41
4 167 43 52 61 73 90 103 127 135
[253] 145 163 170 175 42 52 58 74 66
2 78 102 124 146 164 197 231 259 265
[281] 42 48 57 74 93 114 136 147 169
5 123 144 163 185 192 39 46 58 73
[309] 92 114 145 156 184 207 212 233 39
9 309 42 48 59 72 85 98 115 122
[337] 143 151 157 150 42 53 62 73 85
5 82 107 129 159 179 221 263 291 305
[365] 39 50 63 77 96 111 137 144 151
4 186 235 294 327 341 41 53 64 87
[393] 123 158 201 238 287 332 361 373 39
5 220 41 48 56 68 80 83 103 112
[421] 135 157 169 178 41 49 61 74 98
1 78 89 109 130 146 170 214 250 272
[449] 41 55 66 79 101 120 154 182 215
5 153 175 184 199 204 42 49 63 84
[477] 103 126 160 174 204 234 269 281 42
9 200 42 51 65 86 103 118 127 138
[505] 145 146 41 50 61 78 98 117 135
1 120 144 156 173 210 231 238 41 53
[533] 66 79 100 123 148 157 168 185 210
2 261 303 322 40 53 64 85 108 128
[561] 152 166 184 203 233 237 41 54 67
> x$Time # equivalent to x[[2]]
[1] 0 2 4 6 8 10 12 14 16 18 20 21
2 4 6 8 10 12 14 16 18 20 21 0
[38] 2 4 6 8 10 12 14 16 18 20 21 0
4 6 8 10 12 14 16 18 20 21 0 2
[75] 4 6 8 10 12 14 16 18 20 21 0 2
8 10 12 14 16 18 20 21 0 2 4 6
[112] 8 10 12 14 16 18 20 21 0 2 4 6
10 12 14 16 18 20 21 0 2 4 6 8
[149] 10 12 14 16 18 20 21 0 2 4 6 8

171 199 205 40 49 58 72 84 103 12


49 56 67 74 87 102 108 136 154 16
124 141 148 155 160 160 157 41 49 5
134 125 42 51 59 68 85 96 90 9
63 84 112 139 168 177 182 184 181 17
71 70 71 81 91 96 41 49 62 7
49 51 57 51 54 42 51 61 72 8
120 144 157 41 47 54 58 65 73 7
55 64 77 90 95 108 111 131 148 16
68 70 71 72 72 76 74 40 49 6
205 236 251 39 46 58 73 87 100 11
48 59 74 87 106 134 150 187 230 27
102 123 138 170 204 235 256 41 49 6
146 156 147 41 49 63 85 107 134 16
48 61 76 98 116 145 166 198 227 22
109 128 154 192 232 280 290 42 50 6
262 295 321 42 51 66 85 103 124 15
55 69 96 131 157 184 188 197 198 19
141 147 174 197 196 40 52 62 82 10
205 39 50 62 80 104 125 154 170 22
84 105 122 155 175 205 234 264 264
0 2 4 6 8 10 12 14 16 18 20 21 0
2 4 6 8 10 12 14 16 18 20 21 0 2
4 6 8 10 12 14 16 18 20 0 2 4 6
8 10 12 14 16 18 20 21 0 2 4 6 8
10 12 14 16 18 20 21 0 2 4 6 8 10

12 14 0 2 4 6 8 10 12 0 2 4
[186] 6 8 10 12 14 16 18 20 21 0 2 0 2 4 6 8
4 6 8 10 12 14 16 18 20 21 0 2
[223] 4 6 8 10 12 14 16 18 20 21 0 2 4 6 8 10
6 8 10 12 14 16 18 20 21 0 2 4
[260] 6 8 10 12 14 16 18 20 21 0 2 4 6 8 10 12
8 10 12 14 16 18 20 21 0 2 4 6
[297] 8 10 12 14 16 18 20 21 0 2 4 6 8 10 12 14
10 12 14 16 18 20 21 0 2 4 6 8
[334] 10 12 14 16 18 20 21 0 2 4 6 8 10 12 14 16
12 14 16 18 20 21 0 2 4 6 8 10
[371] 12 14 16 18 20 21 0 2 4 6 8 10 12 14 16 18
14 16 18 20 21 0 2 4 6 8 10 12
[408] 14 16 18 20 21 0 2 4 6 8 10 12 14 16 18 20
16 18 20 21 0 2 4 6 8 10 12 14
[445] 16 18 20 21 0 2 4 6 8 10 12 14 16 18 20 21
18 20 21 0 2 4 6 8 10 12 14 16
[482] 18 20 21 0 2 4 6 8 10 12 14 16 18 20 21 0
0 2 4 6 8 10 12 14 16 18 20 21
[519] 0 2 4 6 8 10 12 14 16 18 20 21 0 2 4 6
2 4 6 8 10 12 14 16 18 20 21 0
[556] 2 4 6 8 10 12 14 16 18 20 21 0 2 4 6 8
> x$Chick # equivalent to x[[3]]
[1] 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 4
[38] 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 7 7
[75] 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8
9 9 9 9 9 9 9 9 10 10 10 10
[112] 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11 11
12 12 12 12 12 12 12 13 13 13 13 13
[149] 13 13 13 13 13 13 13 14 14 14 14 14 14 14 14 14
15 15 16 16 16 16 16 16 16 17 17 17
[186] 17 17 17 17 17 17 17 17 17 18 18 19 19 19 19 19
20 20 20 20 20 20 20 20 20 20 21 21
[223] 21 21 21 21 21 21 21 21 21 21 22 22 22 22 22 22
23 23 23 23 23 23 23 23 23 24 24 24
[260] 24 24 24 24 24 24 24 24 24 25 25 25 25 25 25 25
26 26 26 26 26 26 26 26 27 27 27 27
[297] 27 27 27 27 27 27 27 27 28 28 28 28 28 28 28 28
29 29 29 29 29 29 29 30 30 30 30 30
[334] 30 30 30 30 30 30 30 31 31 31 31 31 31 31 31 31
32 32 32 32 32 32 33 33 33 33 33 33
[371] 33 33 33 33 33 33 34 34 34 34 34 34 34 34 34 34
35 35 35 35 35 36 36 36 36 36 36 36
[408] 36 36 36 36 36 37 37 37 37 37 37 37 37 37 37 37
38 38 38 38 39 39 39 39 39 39 39 39
[445] 39 39 39 39 40 40 40 40 40 40 40 40 40 40 40 40
41 41 41 42 42 42 42 42 42 42 42 42
[482] 42 42 42 43 43 43 43 43 43 43 43 43 43 43 43 44
45 45 45 45 45 45 45 45 45 45 45 45
[519] 46 46 46 46 46 46 46 46 46 46 46 46 47 47 47 47
48 48 48 48 48 48 48 48 48 48 48 49
[556] 49 49 49 49 49 49 49 49 49 49 49 50 50 50 50 50
50 Levels: 18 < 16 < 15 < 13 < 9 < 20 < 10 < 8 < 17 <
12 < 2 < 5 < 14 < 7 < 24 < ... < 48
> x$Diet # equivalent to x[[4]]
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[57] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

10 12 14 16 18 20 21 0 2
12 14 16 18 20 21 0 2 4
14 16 18 20 21 0 2 4 6
16 18 20 21 0 2 4 6 8
18 20 21 0 2 4 6 8 10
20 21 0 2 4 6 8 10 12
21 0 2 4 6 8 10 12 14
0 2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16 18
8 10 12 14 16 18 20 21 0
10 12 14 16 18 20 21
2 2 2 2 2 2 2 2 3
5 5 5 5 5 5 5 6 6
8 8 8 8 8 9 9 9 9
11 11 11 11 12 12 12 12 12
14 14 14 15 15 15 15 15 15
19 19 19 19 19 19 19 20 20
22 22 22 22 22 22 23 23 23
25 25 25 25 25 26 26 26 26
28 28 28 28 29 29 29 29 29
31 31 31 32 32 32 32 32 32
34 34 35 35 35 35 35 35 35
37 38 38 38 38 38 38 38 38
41 41 41 41 41 41 41 41 41
44 44 44 44 44 44 44 44 44
47 47 47 47 47 47 47 47 48
50 50 50 50 50 50 50
19 < 4 < 6 < 11 < 3 < 1 <
1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1

1 1 1 1
[113] 1
1 1 1 1
[169] 1
1 1 1 1
[225] 2
2 2 2 2
[281] 2
2 2 2 2
[337] 2
3 3 3 3
[393] 3
3 3 3 3
[449] 3
4 4 4 4
[505] 4
4 4 4 4
[561] 4
Levels:
>

1
1
1
1
1
2
2
2
2
2
3
3
3
3
4
4
4
4
1

1
1
1
1
1
2
2
2
2
2
3
3
3
3
4
4
4
4
2

1
1
1
1
1
2
2
2
2
2
3
3
3
3
4
4
4
4
3

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4
4

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4

1
1
1
1
1
2
2
2
2
3
3
3
3
3
4
4
4
4

1
1
1
1
2
2
2
2
2
3
3
3
3
4
4
4
4
4

1
1
1
1
2
2
2
2
2
3
3
3
3
4
4
4
4
4

1
1
1
1
2
2
2
2
2
3
3
3
3
4
4
4
4
4

1
1
1
1
2
2
2
2
2
3
3
3
3
4
4
4
4
4

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
4 4

> # select a sub-data frame


> # you can use conditions for subscripting rows
>
> # for instance, observations under Diet 1
> xdiet1 = x[ x$Diet=='1' , ]
Si no pongo nada detrs de la coma cojo todas las columnas
La condicin es la columna dieta es igual a 1
Nos dice que hay 220 observaciones con 4 variables
> str(xdiet1)
Classes nfnGroupedData , nfGroupedData , groupedData and 'data.frame':
220 obs. of
4 variables:
$ weight: num 42 51 59 64 76 93 106 125 149 171 ...
$ Time : num 0 2 4 6 8 10 12 14 16 18 ...
$ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 15 15 15 15 15 15 15 15 15
15 ...
$ Diet : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "formula")=Class 'formula' length 3 weight ~ Time | Chick
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "outer")=Class 'formula' length 2 ~Diet
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "labels")=List of 2
..$ x: chr "Time"
..$ y: chr "Body weight"
- attr(*, "units")=List of 2
..$ x: chr "(days)"
..$ y: chr "(gm)"
>
> # observations of 20-day old chickens
> xday20 = x[ x$Time==20, ]
Nos dice que hay 46 observaciones
> str(xday20)
Classes nfnGroupedData , nfGroupedData , groupedData
4 variables:

and 'data.frame':

46 obs. of

$ weight: num 199 209 198 160 220 160 288 125 100 120 ...
$ Time : num 20 20 20 20 20 20 20 20 20 20 ...
$ Chick : Ord.factor w/ 50 levels "18"<"16"<"15"<..: 15 17 14 11 18 12 20 8 5 7
...
$ Diet : Factor w/ 4 levels "1","2","3","4": 1 1 1 1 1 1 1 1 1 1 ...
- attr(*, "formula")=Class 'formula' length 3 weight ~ Time | Chick
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "outer")=Class 'formula' length 2 ~Diet
.. ..- attr(*, ".Environment")=<environment: R_EmptyEnv>
- attr(*, "labels")=List of 2
..$ x: chr "Time"
..$ y: chr "Body weight"
- attr(*, "units")=List of 2
..$ x: chr "(days)"
..$ y: chr "(gm)"
>
Solo la columna peso
> pesos20= xday20 = x[ x$Time==20, 'weight']
> pesos20= xday20 = x[ x$Time==20, 1]
> hist(pesos20)
Esperando para confirmar cambio de pgina...
>

pesos20.1 = x[ x$Time==20 & x$Diet==1, 1]


> pesos20.1 = x[ x$Time==20 & x$Diet==1, 1]
> hist(pesos20.1)
Esperando para confirmar cambio de pgina...
>
> pesos20.2 = x[ x$Time==20 & x$Diet==2, 1]
> pesos20.3 = x[ x$Time==20 & x$Diet==3, 1]
> pesos20.4 = x[ x$Time==20 & x$Diet==4, 1]
Diagrama de cajas
boxplot(pesos20.1, pesos20.2, pesos20.3, pesos20.4)
Los mejores resultados nos los da la dieta 3
> boxplot(pesos20.1, pesos20.2, pesos20.3, pesos20.4)
Esperando para confirmar cambio de pgina...

> # only variables 'weight' and 'Time'


> xwt = x[ , c('weight', 'Time') ]
> str(xwt)
Classes nfnGroupedData , nfGroupedData , groupedData
2 variables:
$ weight: num 42 51 59 64 76 93 106 125 149 171 ...
$ Time : num 0 2 4 6 8 10 12 14 16 18 ...
>

and 'data.frame':

578 obs. of

>
>
>
>
>
>

# NOW... BASIC STATISTICS !!!


# look up file 'people.dat'
# simulated data of people from 5 continents
# on their procedence, sex, height and weight

Por defecto R entiende que no hay cabeceras


> x2 = read.table(file='people.dat')
> str(x2)
'data.frame': 488 obs. of 4 variables:
$ V1: Factor w/ 6 levels "C1","C2","C3",..: 6 5 5 4 5 1 1 2 4 2 ...
$ V2: Factor w/ 3 levels "H","M","sex": 3 1 1 1 1 1 1 1 1 1 ...
$ V3: Factor w/ 32 levels "152","156","157",..: 32 23 17 12 23 13 20 7 6 11 ...
$ V4: Factor w/ 214 levels "53.2","54.5",..: 214 135 98 39 172 123 195 64 1 83
...
Ahora le digo que la primera fila son las cabeceras
> x2 = read.table(file='people.dat', header=TRUE)
> str(x2)
'data.frame': 487 obs. of 4 variables:
$ continent: Factor w/ 5 levels "C1","C2","C3",..: 5 5 4 5 1 1 2 4 2 2 ...
$ sex
: Factor w/ 2 levels "H","M": 1 1 1 1 1 1 1 1 1 1 ...
$ height : int 177 171 166 177 167 174 161 160 165 165 ...
$ weight : num 72.2 68.5 61.8 76.2 71 78.8 64.7 53.2 66.9 67.6 ...
>
> # UNIVARIATE categorical
>
Tabla de frecuencias de continente, que es una variable palabra
> table(x2$continent)
C1 C2 C3 C4 C5
129 142 4 74 138
> barplot(height=table(x2$continent))
Esperando para confirmar cambio de pgina...
Grafico de tartas
> pie(x=table(x2$continent))
Esperando para confirmar cambio de pgina...
>
> # UNIVARIATE numeric
>
Resumen de los pesos
> summary(x2$weight)
Min. 1st Qu. Median
53.20 65.95 70.90
> min(x2$weight)
[1] 53.2
> max(x2$weight)
[1] 86.8
> mean(x2$weight)
[1] 70.2963
> median(x2$weight)

Mean 3rd Qu.


70.30 74.70

Max.
86.80

[1] 70.9
Desviacin tpica
> sd(x2$weight) # n-1 version (sample)
[1] 6.087712
varianza
> var(x2$weight) # n-1 version (sample)
[1] 37.06023
Me calcula los cuartiles, le digo el rden de los cuartiles
> quantile(x=x2$weight, prob=c(0.05, 0.25, 0.5, 0.75, 0.95))
5% 25% 50% 75% 95%
59.66 65.95 70.90 74.70 78.80
Me dibuja la distribucn de valores con puntos en una lnea
> stripchart(x=x2$weight)
Esperando para confirmar cambio de pgina...
> hist(x=x2$weight)
Esperando para confirmar cambio de pgina...
> boxplot(x=x2$weight)
Esperando para confirmar cambio de pgina...
> boxplot(x=x2$weight, horizontal=TRUE)
Esperando para confirmar cambio de pgina...
Este grfico no tiene sentido
> boxplot(x2$weight, x2$height)
Esperando para confirmar cambio de pgina...
>
> # BIVARIATE
>
> table(x2[ ,
sex
continent H
C1 107
C2 109
C3 3
C4 56
C5 108

c('continent', 'sex') ])
M
22
33
1
18
30

Relacin entre continente y sexo,


distribucin marginal de la variable continente, segn sea es mas gordita la columna
o menos
1 es el 100%
porcentajes de hombre y mujer
Es la distribucon del sexo independiente del continente
En este caso si son independientes porque la altura es parecida en todos, si hub
iera alguna altura muy distinta no
seria independiente porque dependera de la otra variable.
> plot(x2[ , c('continent', 'sex') ])
Esperando para confirmar cambio de pgina...

> plot(x2[ , c('sex', 'continent') ])


Esperando para confirmar cambio de pgina...
>

Intereses relacionados