Introduction to R

Statistical Laboratory

Alessandro Ortis - University of Catania

In this lesson we will introduce some basic R commands. They best way to learn a new programming language is to try out the commands.

The help() function provides details about a command. Is possible to run help(command).

In [1]:
help(solve)   #open a new help window with additional information about the function 'help'
?solve        #obtain the same result

Tip: everytime you see a new command, run the help() function to have a comprhensive knowledge of it.

Basic commands

R uses functions to perform operations. To run a function called funcname we type funcname(input1, input2), where input1 and input 2 are the inputs (or parameters) of the function.

A function can have any number of inputs. For example, to create a vector of numbers, we use the function c() (for concatenate). Any numbers inside the parentheses are joined together.

In [2]:
v <- c(1,3,1,2,5,3,1)  # <- (or =) denotes the assigment of a value to a variable name
z <- c(2,2,2,v)        # concatenate 2,2,2 with the existing vector v
In [3]:
v
z
  1. 1
  2. 3
  3. 1
  4. 2
  5. 5
  6. 3
  7. 1
  1. 2
  2. 2
  3. 2
  4. 1
  5. 3
  6. 1
  7. 2
  8. 5
  9. 3
  10. 1
In [4]:
?c
In [6]:
length(v)
7
In [4]:
v
z
# It will raise a warning message because v and z have different lengths
v + z
  1. 1
  2. 3
  3. 1
  4. 2
  5. 5
  6. 3
  7. 1
  1. 2
  2. 2
  3. 2
  4. 1
  5. 3
  6. 1
  7. 2
  8. 5
  9. 3
  10. 1
Warning message in v + z:
"longer object length is not a multiple of shorter object length"
  1. 3
  2. 5
  3. 3
  4. 3
  5. 8
  6. 4
  7. 3
  8. 6
  9. 6
  10. 2
In [7]:
t = c(v,0,0,0)  # concatenate v with 3 zeros to match the length of z
length(t)
length(z)
t + z
10
10
  1. 3
  2. 5
  3. 3
  4. 3
  5. 8
  6. 4
  7. 3
  8. 5
  9. 3
  10. 1
In [8]:
ls() # lists all the objects that we have saved so far.
  1. 't'
  2. 'v'
  3. 'z'
In [9]:
rm(v)  # remove 'v' from memory
ls()
  1. 't'
  2. 'z'
In [8]:
t
z
# We can apply standard mathematical operations to numeric vectors
t * z
t + z
t / z
t - z
  1. 1
  2. 3
  3. 1
  4. 2
  5. 5
  6. 3
  7. 1
  8. 0
  9. 0
  10. 0
  1. 2
  2. 2
  3. 2
  4. 1
  5. 3
  6. 1
  7. 2
  8. 5
  9. 3
  10. 1
  1. 2
  2. 6
  3. 2
  4. 2
  5. 15
  6. 3
  7. 2
  8. 0
  9. 0
  10. 0
  1. 3
  2. 5
  3. 3
  4. 3
  5. 8
  6. 4
  7. 3
  8. 5
  9. 3
  10. 1
  1. 0.5
  2. 1.5
  3. 0.5
  4. 2
  5. 1.66666666666667
  6. 3
  7. 0.5
  8. 0
  9. 0
  10. 0
  1. -1
  2. 1
  3. -1
  4. 1
  5. 2
  6. 2
  7. -1
  8. -5
  9. -3
  10. -1

The matrix() function can be used to create a matrix of numbers. Among all the numerous inputs of this function, let's focus on the first three:

  • data: the entries in the matrix
  • nrow: number of rows
  • ncol: number of columns
In [13]:
?matrix()
In [11]:
# Note that, the matrix func. will fill the matrix by column
x = matrix(data = c(1,2,3,4), 
           nrow = 2, 
           ncol = 2)
x

x = matrix(data = c(1,2,3,4,5,6,7,8),
           nrow = 2,
           ncol = 4)
x

x = matrix(data = c(1,2,3,4,5,6,7,8),
           nrow = 4,
           ncol = 2)
x
13
24
1357
2468
15
26
37
48
In [12]:
x = matrix(c(1,2,3,4), 2, 2, byrow=TRUE)
x

x = matrix(data = c(1,2,3,4,5,6,7,8),
           nrow = 4,
           ncol = 2,
           byrow=TRUE)
x
12
34
12
34
56
78
In [14]:
sqrt(x) # returns the square root of each element of a vector or matrix
1.0000001.414214
1.7320512.000000
2.2360682.449490
2.6457512.828427
In [15]:
x^2 # raises each element of x to the power 2
10^3
1 4
916
2536
4964
1000

Here we create two correlated sets of numbers by using the rnorm() function, and use the cor() function to compute the correlation between them.

In [17]:
?rnorm
In [16]:
random_normal = rnorm(50) # generates a vector of 50 random (normal) variables with first argument n the sample size
random_gaussian = random_normal + rnorm(50, mean = 100, sd = .1)
cor(random_normal, random_gaussian)
0.995853382644579
In [18]:
rnorm(5)
  1. -0.110303361222158
  2. -0.366872849000197
  3. 2.01403435600797
  4. -1.64527222491918
  5. -1.80286748231466

To generate the same sequences of (pseudo) random numbers, we can run seed() before any randomic function.

In [19]:
set.seed(24)
rnorm(5)
  1. -0.545880758366027
  2. 0.536585304107612
  3. 0.419623148618683
  4. -0.583627199210279
  5. 0.847460017311944
In [20]:
data = rnorm(100, mean = 24, sd = 1)
mean(data)
var(data)
sqrt(var(data)) # square root of var...
sd(data)  # ... is equal to the std dev!
23.8992497503819
0.97115861972343
0.985473804686573
0.985473804686573
In [21]:
summary(data)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  19.53   23.38   23.95   23.90   24.47   26.78 

Indexing Data

In [22]:
?seq
In [24]:
#seq(a,b,length=.) is a function that allows us to generate a sequence of numbers between a and b with a specified length
seq(5,13)
seq(5,13, length=10)
seq(13,5)
#help(seq)
  1. 5
  2. 6
  3. 7
  4. 8
  5. 9
  6. 10
  7. 11
  8. 12
  9. 13
  1. 5
  2. 5.88888888888889
  3. 6.77777777777778
  4. 7.66666666666667
  5. 8.55555555555556
  6. 9.44444444444444
  7. 10.3333333333333
  8. 11.2222222222222
  9. 12.1111111111111
  10. 13
  1. 13
  2. 12
  3. 11
  4. 10
  5. 9
  6. 8
  7. 7
  8. 6
  9. 5
In [24]:
seq(1,10,2)
seq(1,10,length = 3)
seq(3,15,length = 5)
  1. 1
  2. 3
  3. 5
  4. 7
  5. 9
  1. 1
  2. 5.5
  3. 10
  1. 3
  2. 6
  3. 9
  4. 12
  5. 15
In [25]:
seq(10) # Implicitly starts from 1
seq(1,10) # It's the same!
1:10 # again the same!
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
In [35]:
x <- 1:10 # a:b is shorthand for seq(1,10)
x
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

We often wish to examine part of a set of data. Suppose that our data is stored in a given matrix A.

In [26]:
# matrix(data, nrow, ncol)
A = matrix (1:16 ,4 ,4)
A
1 5 913
2 6 1014
3 7 1115
4 8 1216
In [27]:
A[2,3] # select a single element at row=2 and col= 3
10

The first number after the open-bracket symbol [ always refers to the row, and the second number always refers to the column. We can also select multiple rows and columns at a time, by providing vectors as the indices.

In [32]:
A
# rows: 1 and 3   cols: 2 and 4
# selected elements:
# [1,2] -> 5
# [3,2] -> 7
# [1,4] -> 13
# [3,4] -> 15
#A[c(1 ,3) ,c(2 ,4) ] -> ?
A[c(1 ,3),c(2 ,4)]
1 5 913
2 6 1014
3 7 1115
4 8 1216
5 13
7 15
In [34]:
# 1:3 means seq(1,3) -> [1,2,3]
A
A[1:3, 2:4] # rows: from 1 to 3, cols: from 2 to 4
1 5 913
2 6 1014
3 7 1115
4 8 1216
5 913
6 1014
7 1115
In [36]:
A
A[1:2,]
1 5 913
2 6 1014
3 7 1115
4 8 1216
1 5 913
2 6 1014
In [37]:
A[ ,1:2]
15
26
37
48
In [38]:
# R treats a single row/column of a matrix as a vector
A[1,]
  1. 1
  2. 5
  3. 9
  4. 13
In [43]:
# The dim() function outputs the number of rows followed by the number of columns of a given matrix
d <- dim(A)
d
  1. 4
  2. 4

Lists

In [44]:
# The $ operator allows you to extract elements by name from a named list
person <- list(name='Mikey', surname='Mouse', age=33)

person$name
person$surname
person$age
# The names of a list can be found using names
names(person)
'Mikey'
'Mouse'
33
  1. 'name'
  2. 'surname'
  3. 'age'

Loading Data

For most analyses, the first step involves importing a data set into R.

In [ ]:
# old version
#Auto = read.csv("../Datasets/Auto.csv")
#fix(Auto)
In [44]:
?read.csv
In [45]:
Auto= read.csv("../Datasets/Auto.csv", header =T,na.strings ="?")

The dim() function tells us that the data has 397 observations, or rows, and 9 variables, or columns.

In [46]:
dim(Auto)
  1. 397
  2. 9
In [47]:
Auto[1:4,]
mpgcylindersdisplacementhorsepowerweightaccelerationyearoriginname
18 8 307 130 3504 12.0 70 1 chevrolet chevelle malibu
15 8 350 165 3693 11.5 70 1 buick skylark 320
18 8 318 150 3436 11.0 70 1 plymouth satellite
16 8 304 150 3433 12.0 70 1 amc rebel sst

There are various ways to deal with the missing data. In this case, only 5 of the rows contain missing observations, and so we choose to use the na.omit() function to simply remove these rows.

In [49]:
?na.omit
In [48]:
Auto=na.omit(Auto)
dim(Auto)
  1. 392
  2. 9
In [62]:
data = matrix(1:15,5,3)
data[2,3] = NA
data[3,1] = NA
data[4,2] = NA
data
1 611
2 7NA
NA 813
4NA14
51015
In [63]:
na.omit(data)
1 611
5 1015

Once the data are loaded correctly, we can use names() to check the variable names.

In [53]:
names(Auto)

all_the_auto_names <- Auto$name
all_the_auto_names 

# The round brakets will output the assignment value
#(all_the_auto_names <- Auto$name)
  1. 'mpg'
  2. 'cylinders'
  3. 'displacement'
  4. 'horsepower'
  5. 'weight'
  6. 'acceleration'
  7. 'year'
  8. 'origin'
  9. 'name'
  1. chevrolet chevelle malibu
  2. buick skylark 320
  3. plymouth satellite
  4. amc rebel sst
  5. ford torino
  6. ford galaxie 500
  7. chevrolet impala
  8. plymouth fury iii
  9. pontiac catalina
  10. amc ambassador dpl
  11. dodge challenger se
  12. plymouth 'cuda 340
  13. chevrolet monte carlo
  14. buick estate wagon (sw)
  15. toyota corona mark ii
  16. plymouth duster
  17. amc hornet
  18. ford maverick
  19. datsun pl510
  20. volkswagen 1131 deluxe sedan
  21. peugeot 504
  22. audi 100 ls
  23. saab 99e
  24. bmw 2002
  25. amc gremlin
  26. ford f250
  27. chevy c20
  28. dodge d200
  29. hi 1200d
  30. datsun pl510
  31. chevrolet vega 2300
  32. toyota corona
  33. amc gremlin
  34. plymouth satellite custom
  35. chevrolet chevelle malibu
  36. ford torino 500
  37. amc matador
  38. chevrolet impala
  39. pontiac catalina brougham
  40. ford galaxie 500
  41. plymouth fury iii
  42. dodge monaco (sw)
  43. ford country squire (sw)
  44. pontiac safari (sw)
  45. amc hornet sportabout (sw)
  46. chevrolet vega (sw)
  47. pontiac firebird
  48. ford mustang
  49. mercury capri 2000
  50. opel 1900
  51. peugeot 304
  52. fiat 124b
  53. toyota corolla 1200
  54. datsun 1200
  55. volkswagen model 111
  56. plymouth cricket
  57. toyota corona hardtop
  58. dodge colt hardtop
  59. volkswagen type 3
  60. chevrolet vega
  61. ford pinto runabout
  62. chevrolet impala
  63. pontiac catalina
  64. plymouth fury iii
  65. ford galaxie 500
  66. amc ambassador sst
  67. mercury marquis
  68. buick lesabre custom
  69. oldsmobile delta 88 royale
  70. chrysler newport royal
  71. mazda rx2 coupe
  72. amc matador (sw)
  73. chevrolet chevelle concours (sw)
  74. ford gran torino (sw)
  75. plymouth satellite custom (sw)
  76. volvo 145e (sw)
  77. volkswagen 411 (sw)
  78. peugeot 504 (sw)
  79. renault 12 (sw)
  80. ford pinto (sw)
  81. datsun 510 (sw)
  82. toyouta corona mark ii (sw)
  83. dodge colt (sw)
  84. toyota corolla 1600 (sw)
  85. buick century 350
  86. amc matador
  87. chevrolet malibu
  88. ford gran torino
  89. dodge coronet custom
  90. mercury marquis brougham
  91. chevrolet caprice classic
  92. ford ltd
  93. plymouth fury gran sedan
  94. chrysler new yorker brougham
  95. buick electra 225 custom
  96. amc ambassador brougham
  97. plymouth valiant
  98. chevrolet nova custom
  99. amc hornet
  100. ford maverick
  101. plymouth duster
  102. volkswagen super beetle
  103. chevrolet impala
  104. ford country
  105. plymouth custom suburb
  106. oldsmobile vista cruiser
  107. amc gremlin
  108. toyota carina
  109. chevrolet vega
  110. datsun 610
  111. maxda rx3
  112. ford pinto
  113. mercury capri v6
  114. fiat 124 sport coupe
  115. chevrolet monte carlo s
  116. pontiac grand prix
  117. fiat 128
  118. opel manta
  119. audi 100ls
  120. volvo 144ea
  121. dodge dart custom
  122. saab 99le
  123. toyota mark ii
  124. oldsmobile omega
  125. plymouth duster
  126. amc hornet
  127. chevrolet nova
  128. datsun b210
  129. ford pinto
  130. toyota corolla 1200
  131. chevrolet vega
  132. chevrolet chevelle malibu classic
  133. amc matador
  134. plymouth satellite sebring
  135. ford gran torino
  136. buick century luxus (sw)
  137. dodge coronet custom (sw)
  138. ford gran torino (sw)
  139. amc matador (sw)
  140. audi fox
  141. volkswagen dasher
  142. opel manta
  143. toyota corona
  144. datsun 710
  145. dodge colt
  146. fiat 128
  147. fiat 124 tc
  148. honda civic
  149. subaru
  150. fiat x1.9
  151. plymouth valiant custom
  152. chevrolet nova
  153. mercury monarch
  154. ford maverick
  155. pontiac catalina
  156. chevrolet bel air
  157. plymouth grand fury
  158. ford ltd
  159. buick century
  160. chevroelt chevelle malibu
  161. amc matador
  162. plymouth fury
  163. buick skyhawk
  164. chevrolet monza 2+2
  165. ford mustang ii
  166. toyota corolla
  167. ford pinto
  168. amc gremlin
  169. pontiac astro
  170. toyota corona
  171. volkswagen dasher
  172. datsun 710
  173. ford pinto
  174. volkswagen rabbit
  175. amc pacer
  176. audi 100ls
  177. peugeot 504
  178. volvo 244dl
  179. saab 99le
  180. honda civic cvcc
  181. fiat 131
  182. opel 1900
  183. capri ii
  184. dodge colt
  185. renault 12tl
  186. chevrolet chevelle malibu classic
  187. dodge coronet brougham
  188. amc matador
  189. ford gran torino
  190. plymouth valiant
  191. chevrolet nova
  192. ford maverick
  193. amc hornet
  194. chevrolet chevette
  195. chevrolet woody
  196. vw rabbit
  197. honda civic
  198. dodge aspen se
  199. ford granada ghia
  200. pontiac ventura sj
  201. amc pacer d/l
  202. volkswagen rabbit
  203. datsun b-210
  204. toyota corolla
  205. ford pinto
  206. volvo 245
  207. plymouth volare premier v8
  208. peugeot 504
  209. toyota mark ii
  210. mercedes-benz 280s
  211. cadillac seville
  212. chevy c10
  213. ford f108
  214. dodge d100
  215. honda accord cvcc
  216. buick opel isuzu deluxe
  217. renault 5 gtl
  218. plymouth arrow gs
  219. datsun f-10 hatchback
  220. chevrolet caprice classic
  221. oldsmobile cutlass supreme
  222. dodge monaco brougham
  223. mercury cougar brougham
  224. chevrolet concours
  225. buick skylark
  226. plymouth volare custom
  227. ford granada
  228. pontiac grand prix lj
  229. chevrolet monte carlo landau
  230. chrysler cordoba
  231. ford thunderbird
  232. volkswagen rabbit custom
  233. pontiac sunbird coupe
  234. toyota corolla liftback
  235. ford mustang ii 2+2
  236. chevrolet chevette
  237. dodge colt m/m
  238. subaru dl
  239. volkswagen dasher
  240. datsun 810
  241. bmw 320i
  242. mazda rx-4
  243. volkswagen rabbit custom diesel
  244. ford fiesta
  245. mazda glc deluxe
  246. datsun b210 gx
  247. honda civic cvcc
  248. oldsmobile cutlass salon brougham
  249. dodge diplomat
  250. mercury monarch ghia
  251. pontiac phoenix lj
  252. chevrolet malibu
  253. ford fairmont (auto)
  254. ford fairmont (man)
  255. plymouth volare
  256. amc concord
  257. buick century special
  258. mercury zephyr
  259. dodge aspen
  260. amc concord d/l
  261. chevrolet monte carlo landau
  262. buick regal sport coupe (turbo)
  263. ford futura
  264. dodge magnum xe
  265. chevrolet chevette
  266. toyota corona
  267. datsun 510
  268. dodge omni
  269. toyota celica gt liftback
  270. plymouth sapporo
  271. oldsmobile starfire sx
  272. datsun 200-sx
  273. audi 5000
  274. volvo 264gl
  275. saab 99gle
  276. peugeot 604sl
  277. volkswagen scirocco
  278. honda accord lx
  279. pontiac lemans v6
  280. mercury zephyr 6
  281. ford fairmont 4
  282. amc concord dl 6
  283. dodge aspen 6
  284. chevrolet caprice classic
  285. ford ltd landau
  286. mercury grand marquis
  287. dodge st. regis
  288. buick estate wagon (sw)
  289. ford country squire (sw)
  290. chevrolet malibu classic (sw)
  291. chrysler lebaron town @ country (sw)
  292. vw rabbit custom
  293. maxda glc deluxe
  294. dodge colt hatchback custom
  295. amc spirit dl
  296. mercedes benz 300d
  297. cadillac eldorado
  298. peugeot 504
  299. oldsmobile cutlass salon brougham
  300. plymouth horizon
  301. plymouth horizon tc3
  302. datsun 210
  303. fiat strada custom
  304. buick skylark limited
  305. chevrolet citation
  306. oldsmobile omega brougham
  307. pontiac phoenix
  308. vw rabbit
  309. toyota corolla tercel
  310. chevrolet chevette
  311. datsun 310
  312. chevrolet citation
  313. ford fairmont
  314. amc concord
  315. dodge aspen
  316. audi 4000
  317. toyota corona liftback
  318. mazda 626
  319. datsun 510 hatchback
  320. toyota corolla
  321. mazda glc
  322. dodge colt
  323. datsun 210
  324. vw rabbit c (diesel)
  325. vw dasher (diesel)
  326. audi 5000s (diesel)
  327. mercedes-benz 240d
  328. honda civic 1500 gl
  329. subaru dl
  330. vokswagen rabbit
  331. datsun 280-zx
  332. mazda rx-7 gs
  333. triumph tr7 coupe
  334. honda accord
  335. plymouth reliant
  336. buick skylark
  337. dodge aries wagon (sw)
  338. chevrolet citation
  339. plymouth reliant
  340. toyota starlet
  341. plymouth champ
  342. honda civic 1300
  343. subaru
  344. datsun 210 mpg
  345. toyota tercel
  346. mazda glc 4
  347. plymouth horizon 4
  348. ford escort 4w
  349. ford escort 2h
  350. volkswagen jetta
  351. honda prelude
  352. toyota corolla
  353. datsun 200sx
  354. mazda 626
  355. peugeot 505s turbo diesel
  356. volvo diesel
  357. toyota cressida
  358. datsun 810 maxima
  359. buick century
  360. oldsmobile cutlass ls
  361. ford granada gl
  362. chrysler lebaron salon
  363. chevrolet cavalier
  364. chevrolet cavalier wagon
  365. chevrolet cavalier 2-door
  366. pontiac j2000 se hatchback
  367. dodge aries se
  368. pontiac phoenix
  369. ford fairmont futura
  370. volkswagen rabbit l
  371. mazda glc custom l
  372. mazda glc custom
  373. plymouth horizon miser
  374. mercury lynx l
  375. nissan stanza xe
  376. honda accord
  377. toyota corolla
  378. honda civic
  379. honda civic (auto)
  380. datsun 310 gx
  381. buick century limited
  382. oldsmobile cutlass ciera (diesel)
  383. chrysler lebaron medallion
  384. ford granada l
  385. toyota celica gt
  386. dodge charger 2.2
  387. chevrolet camaro
  388. ford mustang gl
  389. vw pickup
  390. dodge rampage
  391. ford ranger
  392. chevy s-10
Levels:
  1. 'amc ambassador brougham'
  2. 'amc ambassador dpl'
  3. 'amc ambassador sst'
  4. 'amc concord'
  5. 'amc concord d/l'
  6. 'amc concord dl 6'
  7. 'amc gremlin'
  8. 'amc hornet'
  9. 'amc hornet sportabout (sw)'
  10. 'amc matador'
  11. 'amc matador (sw)'
  12. 'amc pacer'
  13. 'amc pacer d/l'
  14. 'amc rebel sst'
  15. 'amc spirit dl'
  16. 'audi 100 ls'
  17. 'audi 100ls'
  18. 'audi 4000'
  19. 'audi 5000'
  20. 'audi 5000s (diesel)'
  21. 'audi fox'
  22. 'bmw 2002'
  23. 'bmw 320i'
  24. 'buick century'
  25. 'buick century 350'
  26. 'buick century limited'
  27. 'buick century luxus (sw)'
  28. 'buick century special'
  29. 'buick electra 225 custom'
  30. 'buick estate wagon (sw)'
  31. 'buick lesabre custom'
  32. 'buick opel isuzu deluxe'
  33. 'buick regal sport coupe (turbo)'
  34. 'buick skyhawk'
  35. 'buick skylark'
  36. 'buick skylark 320'
  37. 'buick skylark limited'
  38. 'cadillac eldorado'
  39. 'cadillac seville'
  40. 'capri ii'
  41. 'chevroelt chevelle malibu'
  42. 'chevrolet bel air'
  43. 'chevrolet camaro'
  44. 'chevrolet caprice classic'
  45. 'chevrolet cavalier'
  46. 'chevrolet cavalier 2-door'
  47. 'chevrolet cavalier wagon'
  48. 'chevrolet chevelle concours (sw)'
  49. 'chevrolet chevelle malibu'
  50. 'chevrolet chevelle malibu classic'
  51. 'chevrolet chevette'
  52. 'chevrolet citation'
  53. 'chevrolet concours'
  54. 'chevrolet impala'
  55. 'chevrolet malibu'
  56. 'chevrolet malibu classic (sw)'
  57. 'chevrolet monte carlo'
  58. 'chevrolet monte carlo landau'
  59. 'chevrolet monte carlo s'
  60. 'chevrolet monza 2+2'
  61. 'chevrolet nova'
  62. 'chevrolet nova custom'
  63. 'chevrolet vega'
  64. 'chevrolet vega (sw)'
  65. 'chevrolet vega 2300'
  66. 'chevrolet woody'
  67. 'chevy c10'
  68. 'chevy c20'
  69. 'chevy s-10'
  70. 'chrysler cordoba'
  71. 'chrysler lebaron medallion'
  72. 'chrysler lebaron salon'
  73. 'chrysler lebaron town @ country (sw)'
  74. 'chrysler new yorker brougham'
  75. 'chrysler newport royal'
  76. 'datsun 1200'
  77. 'datsun 200-sx'
  78. 'datsun 200sx'
  79. 'datsun 210'
  80. 'datsun 210 mpg'
  81. 'datsun 280-zx'
  82. 'datsun 310'
  83. 'datsun 310 gx'
  84. 'datsun 510'
  85. 'datsun 510 (sw)'
  86. 'datsun 510 hatchback'
  87. 'datsun 610'
  88. 'datsun 710'
  89. 'datsun 810'
  90. 'datsun 810 maxima'
  91. 'datsun b-210'
  92. 'datsun b210'
  93. 'datsun b210 gx'
  94. 'datsun f-10 hatchback'
  95. 'datsun pl510'
  96. 'dodge aries se'
  97. 'dodge aries wagon (sw)'
  98. 'dodge aspen'
  99. 'dodge aspen 6'
  100. 'dodge aspen se'
  101. 'dodge challenger se'
  102. 'dodge charger 2.2'
  103. 'dodge colt'
  104. 'dodge colt (sw)'
  105. 'dodge colt hardtop'
  106. 'dodge colt hatchback custom'
  107. 'dodge colt m/m'
  108. 'dodge coronet brougham'
  109. 'dodge coronet custom'
  110. 'dodge coronet custom (sw)'
  111. 'dodge d100'
  112. 'dodge d200'
  113. 'dodge dart custom'
  114. 'dodge diplomat'
  115. 'dodge magnum xe'
  116. 'dodge monaco (sw)'
  117. 'dodge monaco brougham'
  118. 'dodge omni'
  119. 'dodge rampage'
  120. 'dodge st. regis'
  121. 'fiat 124 sport coupe'
  122. 'fiat 124 tc'
  123. 'fiat 124b'
  124. 'fiat 128'
  125. 'fiat 131'
  126. 'fiat strada custom'
  127. 'fiat x1.9'
  128. 'ford country'
  129. 'ford country squire (sw)'
  130. 'ford escort 2h'
  131. 'ford escort 4w'
  132. 'ford f108'
  133. 'ford f250'
  134. 'ford fairmont'
  135. 'ford fairmont (auto)'
  136. 'ford fairmont (man)'
  137. 'ford fairmont 4'
  138. 'ford fairmont futura'
  139. 'ford fiesta'
  140. 'ford futura'
  141. 'ford galaxie 500'
  142. 'ford gran torino'
  143. 'ford gran torino (sw)'
  144. 'ford granada'
  145. 'ford granada ghia'
  146. 'ford granada gl'
  147. 'ford granada l'
  148. 'ford ltd'
  149. 'ford ltd landau'
  150. 'ford maverick'
  151. 'ford mustang'
  152. 'ford mustang cobra'
  153. 'ford mustang gl'
  154. 'ford mustang ii'
  155. 'ford mustang ii 2+2'
  156. 'ford pinto'
  157. 'ford pinto (sw)'
  158. 'ford pinto runabout'
  159. 'ford ranger'
  160. 'ford thunderbird'
  161. 'ford torino'
  162. 'ford torino 500'
  163. 'hi 1200d'
  164. 'honda accord'
  165. 'honda accord cvcc'
  166. 'honda accord lx'
  167. 'honda civic'
  168. 'honda civic (auto)'
  169. 'honda civic 1300'
  170. 'honda civic 1500 gl'
  171. 'honda civic cvcc'
  172. 'honda prelude'
  173. 'maxda glc deluxe'
  174. 'maxda rx3'
  175. 'mazda 626'
  176. 'mazda glc'
  177. 'mazda glc 4'
  178. 'mazda glc custom'
  179. 'mazda glc custom l'
  180. 'mazda glc deluxe'
  181. 'mazda rx-4'
  182. 'mazda rx-7 gs'
  183. 'mazda rx2 coupe'
  184. 'mercedes-benz 240d'
  185. 'mercedes-benz 280s'
  186. 'mercedes benz 300d'
  187. 'mercury capri 2000'
  188. 'mercury capri v6'
  189. 'mercury cougar brougham'
  190. 'mercury grand marquis'
  191. 'mercury lynx l'
  192. 'mercury marquis'
  193. 'mercury marquis brougham'
  194. 'mercury monarch'
  195. 'mercury monarch ghia'
  196. 'mercury zephyr'
  197. 'mercury zephyr 6'
  198. 'nissan stanza xe'
  199. 'oldsmobile cutlass ciera (diesel)'
  200. 'oldsmobile cutlass ls'
  201. 'oldsmobile cutlass salon brougham'
  202. 'oldsmobile cutlass supreme'
  203. 'oldsmobile delta 88 royale'
  204. 'oldsmobile omega'
  205. 'oldsmobile omega brougham'
  206. 'oldsmobile starfire sx'
  207. 'oldsmobile vista cruiser'
  208. 'opel 1900'
  209. 'opel manta'
  210. 'peugeot 304'
  211. 'peugeot 504'
  212. 'peugeot 504 (sw)'
  213. 'peugeot 505s turbo diesel'
  214. 'peugeot 604sl'
  215. 'plymouth \'cuda 340'
  216. 'plymouth arrow gs'
  217. 'plymouth champ'
  218. 'plymouth cricket'
  219. 'plymouth custom suburb'
  220. 'plymouth duster'
  221. 'plymouth fury'
  222. 'plymouth fury gran sedan'
  223. 'plymouth fury iii'
  224. 'plymouth grand fury'
  225. 'plymouth horizon'
  226. 'plymouth horizon 4'
  227. 'plymouth horizon miser'
  228. 'plymouth horizon tc3'
  229. 'plymouth reliant'
  230. 'plymouth sapporo'
  231. 'plymouth satellite'
  232. 'plymouth satellite custom'
  233. 'plymouth satellite custom (sw)'
  234. 'plymouth satellite sebring'
  235. 'plymouth valiant'
  236. 'plymouth valiant custom'
  237. 'plymouth volare'
  238. 'plymouth volare custom'
  239. 'plymouth volare premier v8'
  240. 'pontiac astro'
  241. 'pontiac catalina'
  242. 'pontiac catalina brougham'
  243. 'pontiac firebird'
  244. 'pontiac grand prix'
  245. 'pontiac grand prix lj'
  246. 'pontiac j2000 se hatchback'
  247. 'pontiac lemans v6'
  248. 'pontiac phoenix'
  249. 'pontiac phoenix lj'
  250. 'pontiac safari (sw)'
  251. 'pontiac sunbird coupe'
  252. 'pontiac ventura sj'
  253. 'renault 12 (sw)'
  254. 'renault 12tl'
  255. 'renault 18i'
  256. 'renault 5 gtl'
  257. 'renault lecar deluxe'
  258. 'saab 99e'
  259. 'saab 99gle'
  260. 'saab 99le'
  261. 'subaru'
  262. 'subaru dl'
  263. 'toyota carina'
  264. 'toyota celica gt'
  265. 'toyota celica gt liftback'
  266. 'toyota corolla'
  267. 'toyota corolla 1200'
  268. 'toyota corolla 1600 (sw)'
  269. 'toyota corolla liftback'
  270. 'toyota corolla tercel'
  271. 'toyota corona'
  272. 'toyota corona hardtop'
  273. 'toyota corona liftback'
  274. 'toyota corona mark ii'
  275. 'toyota cressida'
  276. 'toyota mark ii'
  277. 'toyota starlet'
  278. 'toyota tercel'
  279. 'toyouta corona mark ii (sw)'
  280. 'triumph tr7 coupe'
  281. 'vokswagen rabbit'
  282. 'volkswagen 1131 deluxe sedan'
  283. 'volkswagen 411 (sw)'
  284. 'volkswagen dasher'
  285. 'volkswagen jetta'
  286. 'volkswagen model 111'
  287. 'volkswagen rabbit'
  288. 'volkswagen rabbit custom'
  289. 'volkswagen rabbit custom diesel'
  290. 'volkswagen rabbit l'
  291. 'volkswagen scirocco'
  292. 'volkswagen super beetle'
  293. 'volkswagen type 3'
  294. 'volvo 144ea'
  295. 'volvo 145e (sw)'
  296. 'volvo 244dl'
  297. 'volvo 245'
  298. 'volvo 264gl'
  299. 'volvo diesel'
  300. 'vw dasher (diesel)'
  301. 'vw pickup'
  302. 'vw rabbit'
  303. 'vw rabbit c (diesel)'
  304. 'vw rabbit custom'

Graphics

plot(x,y) produces a scatterplot of the numbers in x versus the ones in y.

In [85]:
?plot
In [54]:
x = rnorm(10000)
y = rnorm(10000)
plot(x,y)
In [87]:
# add labels
plot(x, y, xlab='x data', ylab= 'y data', main= 'Plot of x versus y')

We will often want to save the otuput of a plot to a file.

In [11]:
?pdf
In [12]:
pdf("Figure.pdf")         # Start creating a new PDF file
plot(x,y,col="green")     # Plot x vs. y using green circles
dev.off()                 # Stop creating the file, and save it
png: 2
In [55]:
x = seq(100)
y = x^3
plot(x,y, xlab = "x", ylab="y = x^3", main = "x^3 function")
In [56]:
x = seq(-pi*2, pi, length=1000)
#x
y = sin(x)
plot(x,y, main="sin(x) with x in [-2*pi,pi]")
y2= cos(x)
plot(x,y2,col="green")

The contour() function produces a contour plot in order to represent three-dimensional data; it is like a topographical map and takes three arguments:

  1. a vector of the x values (first dimension)
  2. a vector of the y values (second dimension)
  3. a matrix whose elements corresponds to the z values (third dimension) for each pair of (x,y) coordinates

There are many other inputs that can be used to fine-tune the output of the contour() function. To learn more about these, take a look at the help file by typing ?contour.

In [15]:
?outer
In [57]:
x = seq(-pi, pi, length=50)
y <- x
f <- outer(x,y,
          
           function(x,y) 
               cos(y)/(1+x^2)
           
          ) #We write a cosine function whose domain is x,y and range is f
contour(x,y,f)
#contour(x,y,f,nlevels=45,add=T)
# t(x) returns the transpose of x
#fa = (f-t(f))/2
#contour(x,y,fa,nlevels=15)
In [58]:
image(x,y,f)
In [95]:
persp(x,y,f , theta =30, phi =40)

Exercise

Create a 5x5 matrix M containing elements randomly selected from a Gaussian distribution with mean 5 and stdev 0.1. Then, extract the central 3x3 sub-matrix from M, let's call this submatrix N, then create the matrix Q = N - 5 and place Q within M, in the same position where N has been extracted. Then, run the following command:

image(1:5,1:5,M)

can you explain the result?

In [4]:
M = matrix(data=rnorm(25,5,0.1), nrow=5, ncol=5)
N = M[2:4,2:4]
Q = N - 5
M[2:4,2:4] = Q
image(1:5,1:5,M)
In [8]:
# Step by step ....
M = matrix(rnorm(25, mean=5, sd=0.1), nrow = 5, ncol =5)
M
5.1473154.9694394.9344724.9729945.035105
5.1271315.0985305.0699505.0222574.929652
4.9202545.1195624.9987535.0383594.994592
4.8944864.7913305.1721534.9855874.972451
5.0264234.9600724.8608995.1412605.048628
In [9]:
N = M[2:4, 2:4]
N
5.0985305.0699505.022257
5.1195624.9987535.038359
4.7913305.1721534.985587
In [10]:
Q = N - 5
Q
0.09853026 0.06994970 0.02225712
0.11956189-0.00124724 0.03835918
-0.20866964 0.17215254-0.01441297
In [11]:
M[2:4, 2:4] = Q
M
5.147315 4.96943850 4.93447159 4.972993605.035105
5.127131 0.09853026 0.06994970 0.022257124.929652
4.920254 0.11956189-0.00124724 0.038359184.994592
4.894486 -0.20866964 0.17215254-0.014412974.972451
5.026423 4.96007209 4.86089947 5.141259695.048628
In [12]:
#?image
In [13]:
image(1:5,1:5,M)

Exercise

Define the code to obtain the following output by creating a 8x8 matrix.

In [14]:

In [7]:
# Solution by M. Andronaco
M = matrix(c(0,1),nrow=9,ncol=8)
M = M[1:8,]
image(1:8,1:8,M)
In [83]:
# Solution by C. Cimino

S = matrix(data = rnorm(64, mean = 5, sd = 0.1), nrow = 8, ncol = 8)
#print(S)

W = S[c(2,4,6,8), c(1,3,5,7)]
#print(W)
J = W - 6
#print(J)
S[c(2,4,6,8), c(1,3,5,7)] = J
#print(S)
K = S[c(1,3,5,7), c(2,4,6,8)]
#print(K)
L = K - 6
#print(L)
S[c(1,3,5,7), c(2,4,6,8)] = L
print(S)
image(1:8, 1:8, S)
           [,1]       [,2]      [,3]       [,4]       [,5]       [,6]
[1,]  5.0923640 -1.0074475  4.926258 -0.9952692  4.9239959 -0.9412714
[2,] -1.0102043  4.9800116 -1.107290  5.1005884 -0.8097100  5.0019442
[3,]  5.0694689 -1.1052663  4.921278 -0.9442782  4.8231532 -0.8999689
[4,] -1.1223695  4.8786211 -0.937773  5.0335695 -0.8997635  5.0612045
[5,]  5.0263061 -0.9381853  4.904463 -1.0399000  5.1082862 -0.8460279
[6,] -0.8922884  5.0237857 -1.028276  5.0616895 -0.9541770  4.8776684
[7,]  4.9195279 -0.9407290  5.080724 -0.9388278  4.9446825 -1.0311310
[8,] -1.1434426  4.9970836 -1.093246  4.8295138 -1.1797255  4.9657781
           [,7]       [,8]
[1,]  5.0359482 -1.1625396
[2,] -1.0948587  4.8461840
[3,]  5.1268769 -0.9780646
[4,] -1.0456980  4.9690766
[5,]  4.8686386 -1.1219732
[6,] -0.8350475  4.8295284
[7,]  4.8494063 -0.9301597
[8,] -1.1893865  5.1478797
In [16]:
M = matrix(rnorm(64, mean=5, sd=0.01), nrow = 8, ncol =8)
M[seq(1,8,2),seq(2,8,2)] = 0
M[seq(2,8,2),seq(1,8,2)] = 0
image(1:8,1:8,M)
In [ ]: