# Chapter 2 Lab: Introduction to R
# Basic Commands
x <- c(1,3,2,5)
x
x = c(1,6,2)
x
y = c(1,4,3)
length(x)
length(y)
x+y
ls()
rm(x,y)
ls()
rm(list=ls())
?matrix
matrix {base} | R Documentation |
matrix
creates a matrix from the given set of values.
as.matrix
attempts to turn its argument into a matrix.
is.matrix
tests if its argument is a (strict) matrix.
matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) as.matrix(x, ...) ## S3 method for class 'data.frame' as.matrix(x, rownames.force = NA, ...) is.matrix(x)
data |
an optional data vector (including a list or
|
nrow |
the desired number of rows. |
ncol |
the desired number of columns. |
byrow |
logical. If |
dimnames |
A |
x |
an R object. |
... |
additional arguments to be passed to or from methods. |
rownames.force |
logical indicating if the resulting matrix
should have character (rather than |
If one of nrow
or ncol
is not given, an attempt is
made to infer it from the length of data
and the other
parameter. If neither is given, a one-column matrix is returned.
If there are too few elements in data
to fill the matrix,
then the elements in data
are recycled. If data
has
length zero, NA
of an appropriate type is used for atomic
vectors (0
for raw vectors) and NULL
for lists.
is.matrix
returns TRUE
if x
is a vector and has a
"dim"
attribute of length 2) and FALSE
otherwise.
Note that a data.frame
is not a matrix by this
test. The function is generic: you can write methods to handle
specific classes of objects, see InternalMethods.
as.matrix
is a generic function. The method for data frames
will return a character matrix if there is only atomic columns and any
non-(numeric/logical/complex) column, applying as.vector
to factors and format
to other non-character columns.
Otherwise, the usual coercion hierarchy (logical < integer < double <
complex) will be used, e.g., all-logical data frames will be coerced
to a logical matrix, mixed logical-integer will give a integer matrix,
etc.
The default method for as.matrix
calls as.vector(x)
, and
hence e.g. coerces factors to character vectors.
When coercing a vector, it produces a one-column matrix, and promotes the names (if any) of the vector to the rownames of the matrix.
is.matrix
is a primitive function.
The print
method for a matrix gives a rectangular layout with
dimnames or indices. For a list matrix, the entries of length not
one are printed in the form integer,7 indicating the type
and length.
If you just want to convert a vector to a matrix, something like
dim(x) <- c(nx, ny) dimnames(x) <- list(row_names, col_names)
will avoid duplicating x
.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
data.matrix
, which attempts to convert to a numeric
matrix.
A matrix is the special case of a two-dimensional array
.
is.matrix(as.matrix(1:10)) !is.matrix(warpbreaks) # data.frame, NOT matrix! warpbreaks[1:10,] as.matrix(warpbreaks[1:10,]) # using as.matrix.data.frame(.) method ## Example of setting row and column names mdat <- matrix(c(1,2,3, 11,12,13), nrow = 2, ncol = 3, byrow = TRUE, dimnames = list(c("row1", "row2"), c("C.1", "C.2", "C.3"))) mdat
x=matrix(data=c(1,2,3,4), nrow=2, ncol=2)
x
1 | 3 |
2 | 4 |
x=matrix(c(1,2,3,4),2,2)
matrix(c(1,2,3,4),2,2,byrow=TRUE)
sqrt(x)
x^2
1 | 2 |
3 | 4 |
1.000000 | 1.732051 |
1.414214 | 2.000000 |
1 | 9 |
4 | 16 |
x=rnorm(50)
y=x+rnorm(50,mean=50,sd=.1)
cor(x,y)
set.seed(1303)
rnorm(50)
set.seed(3)
y=rnorm(100)
mean(y)
var(y)
sqrt(var(y))
sd(y)
# Graphics
x=rnorm(100)
y=rnorm(100)
plot(x,y)
plot(x,y,xlab="this is the x-axis",ylab="this is the y-axis",main="Plot of X vs Y")
pdf("Figure.pdf")
plot(x,y,col="green")
dev.off()
x=seq(1,10)
x
x=1:10
x
x=seq(-pi,pi,length=50)
y=x
f=outer(x,y,function(x,y)cos(y)/(1+x^2))
contour(x,y,f)
contour(x,y,f,nlevels=45,add=T)
fa=(f-t(f))/2
contour(x,y,fa,nlevels=15)
image(x,y,fa)
persp(x,y,fa)
persp(x,y,fa,theta=30)
persp(x,y,fa,theta=30,phi=20)
persp(x,y,fa,theta=30,phi=70)
persp(x,y,fa,theta=30,phi=40)
# Indexing Data
A=matrix(1:16,4,4)
A
1 | 5 | 9 | 13 |
2 | 6 | 10 | 14 |
3 | 7 | 11 | 15 |
4 | 8 | 12 | 16 |
A[2,3]
A[c(1,3),c(2,4)]
5 | 13 |
7 | 15 |
A[1:3,2:4]
5 | 9 | 13 |
6 | 10 | 14 |
7 | 11 | 15 |
A[1:2,]
1 | 5 | 9 | 13 |
2 | 6 | 10 | 14 |
A[,1:2]
1 | 5 |
2 | 6 |
3 | 7 |
4 | 8 |
A[1,]
A[-c(1,3),]
2 | 6 | 10 | 14 |
4 | 8 | 12 | 16 |
A[-c(1,3),-c(1,3,4)]
dim(A)
# Creating Data Frame
x = rnorm(5)
y = rnorm(4)
df = data.frame(x=x, y=y)
Error in data.frame(x = x, y = y): arguments imply differing number of rows: 5, 4 Traceback: 1. data.frame(x = x, y = y) 2. stop(gettextf("arguments imply differing number of rows: %s", . paste(unique(nrows), collapse = ", ")), domain = NA)
y = rnorm(5)
y[3]=NA
df = data.frame(x=x, y=y)
df
x | y | |
---|---|---|
1 | -1.2593788 | -0.8784914 |
2 | 0.7629873 | 0.2604359 |
3 | -0.8034535 | NA |
4 | -0.3793839 | 0.7657965 |
5 | -1.366494 | -2.235361 |
# Saving sata
write.table(df,file = "test.data")
write.csv(df,file = "test.csv", row.names=F)
# Loading Data
Auto=read.table("Auto.data")
Auto
mpg | cylinders | displacement | horsepower | weight | acceleration | year | origin | name | |
---|---|---|---|---|---|---|---|---|---|
1 | 18 | 8 | 307 | 130 | 3504 | 12 | 70 | 1 | chevrolet chevelle malibu |
2 | 15 | 8 | 350 | 165 | 3693 | 11.5 | 70 | 1 | buick skylark 320 |
3 | 18 | 8 | 318 | 150 | 3436 | 11 | 70 | 1 | plymouth satellite |
4 | 16 | 8 | 304 | 150 | 3433 | 12 | 70 | 1 | amc rebel sst |
5 | 17 | 8 | 302 | 140 | 3449 | 10.5 | 70 | 1 | ford torino |
6 | 15 | 8 | 429 | 198 | 4341 | 10 | 70 | 1 | ford galaxie 500 |
7 | 14 | 8 | 454 | 220 | 4354 | 9 | 70 | 1 | chevrolet impala |
8 | 14 | 8 | 440 | 215 | 4312 | 8.5 | 70 | 1 | plymouth fury iii |
9 | 14 | 8 | 455 | 225 | 4425 | 10 | 70 | 1 | pontiac catalina |
10 | 15 | 8 | 390 | 190 | 3850 | 8.5 | 70 | 1 | amc ambassador dpl |
11 | 15 | 8 | 383 | 170 | 3563 | 10 | 70 | 1 | dodge challenger se |
12 | 14 | 8 | 340 | 160 | 3609 | 8 | 70 | 1 | plymouth 'cuda 340 |
13 | 15 | 8 | 400 | 150 | 3761 | 9.5 | 70 | 1 | chevrolet monte carlo |
14 | 14 | 8 | 455 | 225 | 3086 | 10 | 70 | 1 | buick estate wagon (sw) |
15 | 24 | 4 | 113 | 95 | 2372 | 15 | 70 | 3 | toyota corona mark ii |
16 | 22 | 6 | 198 | 95 | 2833 | 15.5 | 70 | 1 | plymouth duster |
17 | 18 | 6 | 199 | 97 | 2774 | 15.5 | 70 | 1 | amc hornet |
18 | 21 | 6 | 200 | 85 | 2587 | 16 | 70 | 1 | ford maverick |
19 | 27 | 4 | 97 | 88 | 2130 | 14.5 | 70 | 3 | datsun pl510 |
20 | 26 | 4 | 97 | 46 | 1835 | 20.5 | 70 | 2 | volkswagen 1131 deluxe sedan |
21 | 25 | 4 | 110 | 87 | 2672 | 17.5 | 70 | 2 | peugeot 504 |
22 | 24 | 4 | 107 | 90 | 2430 | 14.5 | 70 | 2 | audi 100 ls |
23 | 25 | 4 | 104 | 95 | 2375 | 17.5 | 70 | 2 | saab 99e |
24 | 26 | 4 | 121 | 113 | 2234 | 12.5 | 70 | 2 | bmw 2002 |
25 | 21 | 6 | 199 | 90 | 2648 | 15 | 70 | 1 | amc gremlin |
26 | 10 | 8 | 360 | 215 | 4615 | 14 | 70 | 1 | ford f250 |
27 | 10 | 8 | 307 | 200 | 4376 | 15 | 70 | 1 | chevy c20 |
28 | 11 | 8 | 318 | 210 | 4382 | 13.5 | 70 | 1 | dodge d200 |
29 | 9 | 8 | 304 | 193 | 4732 | 18.5 | 70 | 1 | hi 1200d |
30 | 27 | 4 | 97 | 88 | 2130 | 14.5 | 71 | 3 | datsun pl510 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
368 | 28 | 4 | 112 | 88 | 2605 | 19.6 | 82 | 1 | chevrolet cavalier |
369 | 27 | 4 | 112 | 88 | 2640 | 18.6 | 82 | 1 | chevrolet cavalier wagon |
370 | 34 | 4 | 112 | 88 | 2395 | 18 | 82 | 1 | chevrolet cavalier 2-door |
371 | 31 | 4 | 112 | 85 | 2575 | 16.2 | 82 | 1 | pontiac j2000 se hatchback |
372 | 29 | 4 | 135 | 84 | 2525 | 16 | 82 | 1 | dodge aries se |
373 | 27 | 4 | 151 | 90 | 2735 | 18 | 82 | 1 | pontiac phoenix |
374 | 24 | 4 | 140 | 92 | 2865 | 16.4 | 82 | 1 | ford fairmont futura |
375 | 36 | 4 | 105 | 74 | 1980 | 15.3 | 82 | 2 | volkswagen rabbit l |
376 | 37 | 4 | 91 | 68 | 2025 | 18.2 | 82 | 3 | mazda glc custom l |
377 | 31 | 4 | 91 | 68 | 1970 | 17.6 | 82 | 3 | mazda glc custom |
378 | 38 | 4 | 105 | 63 | 2125 | 14.7 | 82 | 1 | plymouth horizon miser |
379 | 36 | 4 | 98 | 70 | 2125 | 17.3 | 82 | 1 | mercury lynx l |
380 | 36 | 4 | 120 | 88 | 2160 | 14.5 | 82 | 3 | nissan stanza xe |
381 | 36 | 4 | 107 | 75 | 2205 | 14.5 | 82 | 3 | honda accord |
382 | 34 | 4 | 108 | 70 | 2245 | 16.9 | 82 | 3 | toyota corolla |
383 | 38 | 4 | 91 | 67 | 1965 | 15 | 82 | 3 | honda civic |
384 | 32 | 4 | 91 | 67 | 1965 | 15.7 | 82 | 3 | honda civic (auto) |
385 | 38 | 4 | 91 | 67 | 1995 | 16.2 | 82 | 3 | datsun 310 gx |
386 | 25 | 6 | 181 | 110 | 2945 | 16.4 | 82 | 1 | buick century limited |
387 | 38 | 6 | 262 | 85 | 3015 | 17 | 82 | 1 | oldsmobile cutlass ciera (diesel) |
388 | 26 | 4 | 156 | 92 | 2585 | 14.5 | 82 | 1 | chrysler lebaron medallion |
389 | 22 | 6 | 232 | 112 | 2835 | 14.7 | 82 | 1 | ford granada l |
390 | 32 | 4 | 144 | 96 | 2665 | 13.9 | 82 | 3 | toyota celica gt |
391 | 36 | 4 | 135 | 84 | 2370 | 13 | 82 | 1 | dodge charger 2.2 |
392 | 27 | 4 | 151 | 90 | 2950 | 17.3 | 82 | 1 | chevrolet camaro |
393 | 27 | 4 | 140 | 86 | 2790 | 15.6 | 82 | 1 | ford mustang gl |
394 | 44 | 4 | 97 | 52 | 2130 | 24.6 | 82 | 2 | vw pickup |
395 | 32 | 4 | 135 | 84 | 2295 | 11.6 | 82 | 1 | dodge rampage |
396 | 28 | 4 | 120 | 79 | 2625 | 18.6 | 82 | 1 | ford ranger |
397 | 31 | 4 | 119 | 82 | 2720 | 19.4 | 82 | 1 | chevy s-10 |
Auto=read.table("Auto.data",header=T,na.strings="?")
Auto
mpg | cylinders | displacement | horsepower | weight | acceleration | year | origin | name | |
---|---|---|---|---|---|---|---|---|---|
1 | 18 | 8 | 307 | 130 | 3504 | 12 | 70 | 1 | chevrolet chevelle malibu |
2 | 15 | 8 | 350 | 165 | 3693 | 11.5 | 70 | 1 | buick skylark 320 |
3 | 18 | 8 | 318 | 150 | 3436 | 11 | 70 | 1 | plymouth satellite |
4 | 16 | 8 | 304 | 150 | 3433 | 12 | 70 | 1 | amc rebel sst |
5 | 17 | 8 | 302 | 140 | 3449 | 10.5 | 70 | 1 | ford torino |
6 | 15 | 8 | 429 | 198 | 4341 | 10 | 70 | 1 | ford galaxie 500 |
7 | 14 | 8 | 454 | 220 | 4354 | 9 | 70 | 1 | chevrolet impala |
8 | 14 | 8 | 440 | 215 | 4312 | 8.5 | 70 | 1 | plymouth fury iii |
9 | 14 | 8 | 455 | 225 | 4425 | 10 | 70 | 1 | pontiac catalina |
10 | 15 | 8 | 390 | 190 | 3850 | 8.5 | 70 | 1 | amc ambassador dpl |
11 | 15 | 8 | 383 | 170 | 3563 | 10 | 70 | 1 | dodge challenger se |
12 | 14 | 8 | 340 | 160 | 3609 | 8 | 70 | 1 | plymouth 'cuda 340 |
13 | 15 | 8 | 400 | 150 | 3761 | 9.5 | 70 | 1 | chevrolet monte carlo |
14 | 14 | 8 | 455 | 225 | 3086 | 10 | 70 | 1 | buick estate wagon (sw) |
15 | 24 | 4 | 113 | 95 | 2372 | 15 | 70 | 3 | toyota corona mark ii |
16 | 22 | 6 | 198 | 95 | 2833 | 15.5 | 70 | 1 | plymouth duster |
17 | 18 | 6 | 199 | 97 | 2774 | 15.5 | 70 | 1 | amc hornet |
18 | 21 | 6 | 200 | 85 | 2587 | 16 | 70 | 1 | ford maverick |
19 | 27 | 4 | 97 | 88 | 2130 | 14.5 | 70 | 3 | datsun pl510 |
20 | 26 | 4 | 97 | 46 | 1835 | 20.5 | 70 | 2 | volkswagen 1131 deluxe sedan |
21 | 25 | 4 | 110 | 87 | 2672 | 17.5 | 70 | 2 | peugeot 504 |
22 | 24 | 4 | 107 | 90 | 2430 | 14.5 | 70 | 2 | audi 100 ls |
23 | 25 | 4 | 104 | 95 | 2375 | 17.5 | 70 | 2 | saab 99e |
24 | 26 | 4 | 121 | 113 | 2234 | 12.5 | 70 | 2 | bmw 2002 |
25 | 21 | 6 | 199 | 90 | 2648 | 15 | 70 | 1 | amc gremlin |
26 | 10 | 8 | 360 | 215 | 4615 | 14 | 70 | 1 | ford f250 |
27 | 10 | 8 | 307 | 200 | 4376 | 15 | 70 | 1 | chevy c20 |
28 | 11 | 8 | 318 | 210 | 4382 | 13.5 | 70 | 1 | dodge d200 |
29 | 9 | 8 | 304 | 193 | 4732 | 18.5 | 70 | 1 | hi 1200d |
30 | 27 | 4 | 97 | 88 | 2130 | 14.5 | 71 | 3 | datsun pl510 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
368 | 28 | 4 | 112 | 88 | 2605 | 19.6 | 82 | 1 | chevrolet cavalier |
369 | 27 | 4 | 112 | 88 | 2640 | 18.6 | 82 | 1 | chevrolet cavalier wagon |
370 | 34 | 4 | 112 | 88 | 2395 | 18 | 82 | 1 | chevrolet cavalier 2-door |
371 | 31 | 4 | 112 | 85 | 2575 | 16.2 | 82 | 1 | pontiac j2000 se hatchback |
372 | 29 | 4 | 135 | 84 | 2525 | 16 | 82 | 1 | dodge aries se |
373 | 27 | 4 | 151 | 90 | 2735 | 18 | 82 | 1 | pontiac phoenix |
374 | 24 | 4 | 140 | 92 | 2865 | 16.4 | 82 | 1 | ford fairmont futura |
375 | 36 | 4 | 105 | 74 | 1980 | 15.3 | 82 | 2 | volkswagen rabbit l |
376 | 37 | 4 | 91 | 68 | 2025 | 18.2 | 82 | 3 | mazda glc custom l |
377 | 31 | 4 | 91 | 68 | 1970 | 17.6 | 82 | 3 | mazda glc custom |
378 | 38 | 4 | 105 | 63 | 2125 | 14.7 | 82 | 1 | plymouth horizon miser |
379 | 36 | 4 | 98 | 70 | 2125 | 17.3 | 82 | 1 | mercury lynx l |
380 | 36 | 4 | 120 | 88 | 2160 | 14.5 | 82 | 3 | nissan stanza xe |
381 | 36 | 4 | 107 | 75 | 2205 | 14.5 | 82 | 3 | honda accord |
382 | 34 | 4 | 108 | 70 | 2245 | 16.9 | 82 | 3 | toyota corolla |
383 | 38 | 4 | 91 | 67 | 1965 | 15 | 82 | 3 | honda civic |
384 | 32 | 4 | 91 | 67 | 1965 | 15.7 | 82 | 3 | honda civic (auto) |
385 | 38 | 4 | 91 | 67 | 1995 | 16.2 | 82 | 3 | datsun 310 gx |
386 | 25 | 6 | 181 | 110 | 2945 | 16.4 | 82 | 1 | buick century limited |
387 | 38 | 6 | 262 | 85 | 3015 | 17 | 82 | 1 | oldsmobile cutlass ciera (diesel) |
388 | 26 | 4 | 156 | 92 | 2585 | 14.5 | 82 | 1 | chrysler lebaron medallion |
389 | 22 | 6 | 232 | 112 | 2835 | 14.7 | 82 | 1 | ford granada l |
390 | 32 | 4 | 144 | 96 | 2665 | 13.9 | 82 | 3 | toyota celica gt |
391 | 36 | 4 | 135 | 84 | 2370 | 13 | 82 | 1 | dodge charger 2.2 |
392 | 27 | 4 | 151 | 90 | 2950 | 17.3 | 82 | 1 | chevrolet camaro |
393 | 27 | 4 | 140 | 86 | 2790 | 15.6 | 82 | 1 | ford mustang gl |
394 | 44 | 4 | 97 | 52 | 2130 | 24.6 | 82 | 2 | vw pickup |
395 | 32 | 4 | 135 | 84 | 2295 | 11.6 | 82 | 1 | dodge rampage |
396 | 28 | 4 | 120 | 79 | 2625 | 18.6 | 82 | 1 | ford ranger |
397 | 31 | 4 | 119 | 82 | 2720 | 19.4 | 82 | 1 | chevy s-10 |
Auto=read.csv("Auto.csv",header=T,na.strings="?")
Auto
mpg | cylinders | displacement | horsepower | weight | acceleration | year | origin | name | |
---|---|---|---|---|---|---|---|---|---|
1 | 18 | 8 | 307 | 130 | 3504 | 12 | 70 | 1 | chevrolet chevelle malibu |
2 | 15 | 8 | 350 | 165 | 3693 | 11.5 | 70 | 1 | buick skylark 320 |
3 | 18 | 8 | 318 | 150 | 3436 | 11 | 70 | 1 | plymouth satellite |
4 | 16 | 8 | 304 | 150 | 3433 | 12 | 70 | 1 | amc rebel sst |
5 | 17 | 8 | 302 | 140 | 3449 | 10.5 | 70 | 1 | ford torino |
6 | 15 | 8 | 429 | 198 | 4341 | 10 | 70 | 1 | ford galaxie 500 |
7 | 14 | 8 | 454 | 220 | 4354 | 9 | 70 | 1 | chevrolet impala |
8 | 14 | 8 | 440 | 215 | 4312 | 8.5 | 70 | 1 | plymouth fury iii |
9 | 14 | 8 | 455 | 225 | 4425 | 10 | 70 | 1 | pontiac catalina |
10 | 15 | 8 | 390 | 190 | 3850 | 8.5 | 70 | 1 | amc ambassador dpl |
11 | 15 | 8 | 383 | 170 | 3563 | 10 | 70 | 1 | dodge challenger se |
12 | 14 | 8 | 340 | 160 | 3609 | 8 | 70 | 1 | plymouth 'cuda 340 |
13 | 15 | 8 | 400 | 150 | 3761 | 9.5 | 70 | 1 | chevrolet monte carlo |
14 | 14 | 8 | 455 | 225 | 3086 | 10 | 70 | 1 | buick estate wagon (sw) |
15 | 24 | 4 | 113 | 95 | 2372 | 15 | 70 | 3 | toyota corona mark ii |
16 | 22 | 6 | 198 | 95 | 2833 | 15.5 | 70 | 1 | plymouth duster |
17 | 18 | 6 | 199 | 97 | 2774 | 15.5 | 70 | 1 | amc hornet |
18 | 21 | 6 | 200 | 85 | 2587 | 16 | 70 | 1 | ford maverick |
19 | 27 | 4 | 97 | 88 | 2130 | 14.5 | 70 | 3 | datsun pl510 |
20 | 26 | 4 | 97 | 46 | 1835 | 20.5 | 70 | 2 | volkswagen 1131 deluxe sedan |
21 | 25 | 4 | 110 | 87 | 2672 | 17.5 | 70 | 2 | peugeot 504 |
22 | 24 | 4 | 107 | 90 | 2430 | 14.5 | 70 | 2 | audi 100 ls |
23 | 25 | 4 | 104 | 95 | 2375 | 17.5 | 70 | 2 | saab 99e |
24 | 26 | 4 | 121 | 113 | 2234 | 12.5 | 70 | 2 | bmw 2002 |
25 | 21 | 6 | 199 | 90 | 2648 | 15 | 70 | 1 | amc gremlin |
26 | 10 | 8 | 360 | 215 | 4615 | 14 | 70 | 1 | ford f250 |
27 | 10 | 8 | 307 | 200 | 4376 | 15 | 70 | 1 | chevy c20 |
28 | 11 | 8 | 318 | 210 | 4382 | 13.5 | 70 | 1 | dodge d200 |
29 | 9 | 8 | 304 | 193 | 4732 | 18.5 | 70 | 1 | hi 1200d |
30 | 27 | 4 | 97 | 88 | 2130 | 14.5 | 71 | 3 | datsun pl510 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
363 | 28 | 4 | 112 | 88 | 2605 | 19.6 | 82 | 1 | chevrolet cavalier |
364 | 27 | 4 | 112 | 88 | 2640 | 18.6 | 82 | 1 | chevrolet cavalier wagon |
365 | 34 | 4 | 112 | 88 | 2395 | 18 | 82 | 1 | chevrolet cavalier 2-door |
366 | 31 | 4 | 112 | 85 | 2575 | 16.2 | 82 | 1 | pontiac j2000 se hatchback |
367 | 29 | 4 | 135 | 84 | 2525 | 16 | 82 | 1 | dodge aries se |
368 | 27 | 4 | 151 | 90 | 2735 | 18 | 82 | 1 | pontiac phoenix |
369 | 24 | 4 | 140 | 92 | 2865 | 16.4 | 82 | 1 | ford fairmont futura |
370 | 36 | 4 | 105 | 74 | 1980 | 15.3 | 82 | 2 | volkswagen rabbit l |
371 | 37 | 4 | 91 | 68 | 2025 | 18.2 | 82 | 3 | mazda glc custom l |
372 | 31 | 4 | 91 | 68 | 1970 | 17.6 | 82 | 3 | mazda glc custom |
373 | 38 | 4 | 105 | 63 | 2125 | 14.7 | 82 | 1 | plymouth horizon miser |
374 | 36 | 4 | 98 | 70 | 2125 | 17.3 | 82 | 1 | mercury lynx l |
375 | 36 | 4 | 120 | 88 | 2160 | 14.5 | 82 | 3 | nissan stanza xe |
376 | 36 | 4 | 107 | 75 | 2205 | 14.5 | 82 | 3 | honda accord |
377 | 34 | 4 | 108 | 70 | 2245 | 16.9 | 82 | 3 | toyota corolla |
378 | 38 | 4 | 91 | 67 | 1965 | 15 | 82 | 3 | honda civic |
379 | 32 | 4 | 91 | 67 | 1965 | 15.7 | 82 | 3 | honda civic (auto) |
380 | 38 | 4 | 91 | 67 | 1995 | 16.2 | 82 | 3 | datsun 310 gx |
381 | 25 | 6 | 181 | 110 | 2945 | 16.4 | 82 | 1 | buick century limited |
382 | 38 | 6 | 262 | 85 | 3015 | 17 | 82 | 1 | oldsmobile cutlass ciera (diesel) |
383 | 26 | 4 | 156 | 92 | 2585 | 14.5 | 82 | 1 | chrysler lebaron medallion |
384 | 22 | 6 | 232 | 112 | 2835 | 14.7 | 82 | 1 | ford granada l |
385 | 32 | 4 | 144 | 96 | 2665 | 13.9 | 82 | 3 | toyota celica gt |
386 | 36 | 4 | 135 | 84 | 2370 | 13 | 82 | 1 | dodge charger 2.2 |
387 | 27 | 4 | 151 | 90 | 2950 | 17.3 | 82 | 1 | chevrolet camaro |
388 | 27 | 4 | 140 | 86 | 2790 | 15.6 | 82 | 1 | ford mustang gl |
389 | 44 | 4 | 97 | 52 | 2130 | 24.6 | 82 | 2 | vw pickup |
390 | 32 | 4 | 135 | 84 | 2295 | 11.6 | 82 | 1 | dodge rampage |
391 | 28 | 4 | 120 | 79 | 2625 | 18.6 | 82 | 1 | ford ranger |
392 | 31 | 4 | 119 | 82 | 2720 | 19.4 | 82 | 1 | chevy s-10 |
dim(Auto)
Auto[1:4,]
mpg | cylinders | displacement | horsepower | weight | acceleration | year | origin | name | |
---|---|---|---|---|---|---|---|---|---|
1 | 18 | 8 | 307 | 130 | 3504 | 12 | 70 | 1 | chevrolet chevelle malibu |
2 | 15 | 8 | 350 | 165 | 3693 | 11.5 | 70 | 1 | buick skylark 320 |
3 | 18 | 8 | 318 | 150 | 3436 | 11 | 70 | 1 | plymouth satellite |
4 | 16 | 8 | 304 | 150 | 3433 | 12 | 70 | 1 | amc rebel sst |
Auto=na.omit(Auto)
dim(Auto)
names(Auto)
# Additional Graphical and Numerical Summaries
plot(cylinders, mpg)
plot(Auto$cylinders, Auto$mpg)
attach(Auto)
plot(cylinders, mpg)
The following objects are masked from Auto (pos = 3): acceleration, cylinders, displacement, horsepower, mpg, name, origin, weight, year The following objects are masked from Auto (pos = 4): acceleration, cylinders, displacement, horsepower, mpg, name, origin, weight, year
cylinders=as.factor(cylinders)
plot(cylinders, mpg)
plot(cylinders, mpg, col="red")
plot(cylinders, mpg, col="red", varwidth=T)
plot(cylinders, mpg, col="red", varwidth=T,horizontal=T)
plot(cylinders, mpg, col="red", varwidth=T, xlab="cylinders", ylab="MPG")
hist(mpg)
hist(mpg,col=2)
hist(mpg,col=2,breaks=15)
pairs(Auto)
pairs(~ mpg + displacement + horsepower + weight + acceleration, Auto)
plot(horsepower,mpg)
identify(horsepower,mpg,name)
Error in identify.default(horsepower, mpg, name): plot.new has not been called yet Traceback: 1. identify(horsepower, mpg, name) 2. identify.default(horsepower, mpg, name)
summary(Auto)
mpg cylinders displacement horsepower weight Min. : 9.00 Min. :3.000 Min. : 68.0 Min. : 46.0 Min. :1613 1st Qu.:17.00 1st Qu.:4.000 1st Qu.:105.0 1st Qu.: 75.0 1st Qu.:2225 Median :22.75 Median :4.000 Median :151.0 Median : 93.5 Median :2804 Mean :23.45 Mean :5.472 Mean :194.4 Mean :104.5 Mean :2978 3rd Qu.:29.00 3rd Qu.:8.000 3rd Qu.:275.8 3rd Qu.:126.0 3rd Qu.:3615 Max. :46.60 Max. :8.000 Max. :455.0 Max. :230.0 Max. :5140 acceleration year origin name Min. : 8.00 Min. :70.00 Min. :1.000 amc matador : 5 1st Qu.:13.78 1st Qu.:73.00 1st Qu.:1.000 ford pinto : 5 Median :15.50 Median :76.00 Median :1.000 toyota corolla : 5 Mean :15.54 Mean :75.98 Mean :1.577 amc gremlin : 4 3rd Qu.:17.02 3rd Qu.:79.00 3rd Qu.:2.000 amc hornet : 4 Max. :24.80 Max. :82.00 Max. :3.000 chevrolet chevette: 4 (Other) :365
summary(mpg)
Min. 1st Qu. Median Mean 3rd Qu. Max. 9.00 17.00 22.75 23.45 29.00 46.60