Chapter 3 Introdução relâmpago de R
Como a seção já sugere, segue alguns comandos básicos para conseguirmos acompanhar o curso.
# -----------------------------------------------------------------------------
# Como instalar pacotes
# -----------------------------------------------------------------------------
#install.packages('descr')
# -----------------------------------------------------------------------------
# Como remover pacotes
# -----------------------------------------------------------------------------
#remove.packages('descr')
# -----------------------------------------------------------------------------
# Carregar e listar os conteúdos dos pacotes
# -----------------------------------------------------------------------------
library(fdth)
# -----------------------------------------------------------------------------
# Ajuda (socorro)
# -----------------------------------------------------------------------------
help(round)#ou de modo mais simplificado
?round
# -----------------------------------------------------------------------------
# Operadores de atribuição
# -----------------------------------------------------------------------------
# <-
# ->
# =
<- 1
a 2 -> b
= 3
c
# -----------------------------------------------------------------------------
# Imprimir no console uma mensagem ou o valor de uma variável:
# -----------------------------------------------------------------------------
print(a) # ou simplesmente
## [1] 1
a
## [1] 1
# -----------------------------------------------------------------------------
# Operadores matemáticos
# -----------------------------------------------------------------------------
# + soma
# - subtração
# * multiplicação
# / divisão
# ^ potenciação
# %% operador modular
2 + 1
## [1] 3
3 - 5
## [1] -2
3 * 9
## [1] 27
4/8
## [1] 0.5
5^(1 + 1)
## [1] 25
5^1 + 1
## [1] 6
6 * (5 + 3)
## [1] 48
3%%2
## [1] 1
4%%2
## [1] 0
5%%2
## [1] 1
# -----------------------------------------------------------------------------
# Vetores
# -----------------------------------------------------------------------------
# Algumas das diversas formas de criar:
#c() - é uma função
# vetores numéricos
c(1.54, 1.75, 1.60, 1.60, 1.73, 1.63, 1.52, 1.79, 1.64, 1.60, 1.60, 1.61, 1.59, 1.77)
## [1] 1.54 1.75 1.60 1.60 1.73 1.63 1.52 1.79 1.64 1.60 1.60 1.61 1.59
## [14] 1.77
= c(1.54, 1.75, 1.60, 1.60, 1.73, 1.63, 1.52, 1.79, 1.64, 1.60, 1.60, 1.61, 1.59, 1.77)
altura altura
## [1] 1.54 1.75 1.60 1.60 1.73 1.63 1.52 1.79 1.64 1.60 1.60 1.61 1.59
## [14] 1.77
= c(56, 73, 50, 70, 57, 55, 78, 50)
peso peso
## [1] 56 73 50 70 57 55 78 50
mean(peso)
## [1] 61.125
var(peso)
## [1] 118.9821
sd(peso)
## [1] 10.90789
hist(peso)
# vetores de strings
c('coca', 'pepsi') # pode criar um vetor com strings
## [1] "coca" "pepsi"
= c('solt','solt','casad','solt','casad',"solt")
estcivil estcivil
## [1] "solt" "solt" "casad" "solt" "casad" "solt"
# Algumas formas de indexar (índices):
= seq(1,20,by = 2)
x x
## [1] 1 3 5 7 9 11 13 15 17 19
2] x[
## [1] 3
# -----------------------------------------------------------------------------
# Data Frames
# -----------------------------------------------------------------------------
= rnorm(10,
x 10,
2)
x
## [1] 10.659016 8.359063 10.974858 11.476649 11.151563 9.389223
## [7] 13.023562 10.779686 8.757519 5.570600
class(x)
## [1] "numeric"
= letters[1:10]
y y
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
class(y)
## [1] "character"
mean(y)
## Warning in mean.default(y): argumento não é numérico nem lógico:
## retornando NA
## [1] NA
= c('f','f','m','f','m','m','m','m','m','f')
sexo
#data.frame()
= data.frame(x,
dF
y,
sexo) dF
## x y sexo
## 1 10.659016 a f
## 2 8.359063 b f
## 3 10.974858 c m
## 4 11.476649 d f
## 5 11.151563 e m
## 6 9.389223 f m
## 7 13.023562 g m
## 8 10.779686 h m
## 9 8.757519 i m
## 10 5.570600 j f
class(dF)
## [1] "data.frame"
# A instrução 'summary' sumariza um objeto (aqui, um frame, mas vai bem com
# quase todos objetos):
summary(dF)
## x y sexo
## Min. : 5.571 Length:10 Length:10
## 1st Qu.: 8.915 Class :character Class :character
## Median :10.719 Mode :character Mode :character
## Mean :10.014
## 3rd Qu.:11.107
## Max. :13.024
## Indexação!
# Opção 1
$x dF
## [1] 10.659016 8.359063 10.974858 11.476649 11.151563 9.389223
## [7] 13.023562 10.779686 8.757519 5.570600
$y dF
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
# Opção 2
1] dF[ ,
## [1] 10.659016 8.359063 10.974858 11.476649 11.151563 9.389223
## [7] 13.023562 10.779686 8.757519 5.570600
2] dF[ ,
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
3:5,1] dF[
## [1] 10.97486 11.47665 11.15156
# Pode-se mudar o nome das linhas ou das colunas:
names(dF) = c('ivan',
'joao',
'leila')
dF
## ivan joao leila
## 1 10.659016 a f
## 2 8.359063 b f
## 3 10.974858 c m
## 4 11.476649 d f
## 5 11.151563 e m
## 6 9.389223 f m
## 7 13.023562 g m
## 8 10.779686 h m
## 9 8.757519 i m
## 10 5.570600 j f
# Opção melhor
with(dF,
mean(x))
## [1] 10.01417
with(dF,
sort(x))
## [1] 5.570600 8.359063 8.757519 9.389223 10.659016 10.779686
## [7] 10.974858 11.151563 11.476649 13.023562
# -----------------------------------------------------------------------------
# Outras funções
# -----------------------------------------------------------------------------
= c(1.73, 1.43, 2.1, 4.3, 5.1)
x x
## [1] 1.73 1.43 2.10 4.30 5.10
length(x) # Número de elementos
## [1] 5
min(x)
## [1] 1.43
max(x)
## [1] 5.1
sort(x) # Ordena os valores dentro de um vetor
## [1] 1.43 1.73 2.10 4.30 5.10
sort(x,
decreasing = TRUE)
## [1] 5.10 4.30 2.10 1.73 1.43
median(x) # Mediana
## [1] 2.1
mean(x) # Média
## [1] 2.932
var(x) # Variância
## [1] 2.74117
sd(x) # Desvio padrão (standard deviation)
## [1] 1.655648
sqrt(var(x)) # raiz quadrada
## [1] 1.655648
sum(x) # Somatório
## [1] 14.66
round(x) # Arrendondar
## [1] 2 1 2 4 5
round(x,
digits = 1)
## [1] 1.7 1.4 2.1 4.3 5.1
round(x,
1)
## [1] 1.7 1.4 2.1 4.3 5.1
# -----------------------------------------------------------------------------
# Gráficos
# -----------------------------------------------------------------------------
# Será utilizado a library default do R, a 'Graphics'. Existem outras
# bibliotecas, como: lattice, ggplot2, etc.
= seq(1, 10)
x x
## [1] 1 2 3 4 5 6 7 8 9 10
= 3 + x
y y
## [1] 4 5 6 7 8 9 10 11 12 13
plot(y ~ x)# ou
plot(x, y)
# Fazendo um gráfico um pouquinho mais elaborado!
plot(y ~ x,
main = 'Exemplo 1',
xlab = 'Variável x',
ylab = 'Variável y',
col = 'red',
type = 'p',
sub = 'VASCÃO')
# Listando todas as cores dispníveis para uso em gráficos.
colors() # Descobrir as cores disponíveis
## [1] "white" "aliceblue"
## [3] "antiquewhite" "antiquewhite1"
## [5] "antiquewhite2" "antiquewhite3"
## [7] "antiquewhite4" "aquamarine"
## [9] "aquamarine1" "aquamarine2"
## [11] "aquamarine3" "aquamarine4"
## [13] "azure" "azure1"
## [15] "azure2" "azure3"
## [17] "azure4" "beige"
## [19] "bisque" "bisque1"
## [21] "bisque2" "bisque3"
## [23] "bisque4" "black"
## [25] "blanchedalmond" "blue"
## [27] "blue1" "blue2"
## [29] "blue3" "blue4"
## [31] "blueviolet" "brown"
## [33] "brown1" "brown2"
## [35] "brown3" "brown4"
## [37] "burlywood" "burlywood1"
## [39] "burlywood2" "burlywood3"
## [41] "burlywood4" "cadetblue"
## [43] "cadetblue1" "cadetblue2"
## [45] "cadetblue3" "cadetblue4"
## [47] "chartreuse" "chartreuse1"
## [49] "chartreuse2" "chartreuse3"
## [51] "chartreuse4" "chocolate"
## [53] "chocolate1" "chocolate2"
## [55] "chocolate3" "chocolate4"
## [57] "coral" "coral1"
## [59] "coral2" "coral3"
## [61] "coral4" "cornflowerblue"
## [63] "cornsilk" "cornsilk1"
## [65] "cornsilk2" "cornsilk3"
## [67] "cornsilk4" "cyan"
## [69] "cyan1" "cyan2"
## [71] "cyan3" "cyan4"
## [73] "darkblue" "darkcyan"
## [75] "darkgoldenrod" "darkgoldenrod1"
## [77] "darkgoldenrod2" "darkgoldenrod3"
## [79] "darkgoldenrod4" "darkgray"
## [81] "darkgreen" "darkgrey"
## [83] "darkkhaki" "darkmagenta"
## [85] "darkolivegreen" "darkolivegreen1"
## [87] "darkolivegreen2" "darkolivegreen3"
## [89] "darkolivegreen4" "darkorange"
## [91] "darkorange1" "darkorange2"
## [93] "darkorange3" "darkorange4"
## [95] "darkorchid" "darkorchid1"
## [97] "darkorchid2" "darkorchid3"
## [99] "darkorchid4" "darkred"
## [101] "darksalmon" "darkseagreen"
## [103] "darkseagreen1" "darkseagreen2"
## [105] "darkseagreen3" "darkseagreen4"
## [107] "darkslateblue" "darkslategray"
## [109] "darkslategray1" "darkslategray2"
## [111] "darkslategray3" "darkslategray4"
## [113] "darkslategrey" "darkturquoise"
## [115] "darkviolet" "deeppink"
## [117] "deeppink1" "deeppink2"
## [119] "deeppink3" "deeppink4"
## [121] "deepskyblue" "deepskyblue1"
## [123] "deepskyblue2" "deepskyblue3"
## [125] "deepskyblue4" "dimgray"
## [127] "dimgrey" "dodgerblue"
## [129] "dodgerblue1" "dodgerblue2"
## [131] "dodgerblue3" "dodgerblue4"
## [133] "firebrick" "firebrick1"
## [135] "firebrick2" "firebrick3"
## [137] "firebrick4" "floralwhite"
## [139] "forestgreen" "gainsboro"
## [141] "ghostwhite" "gold"
## [143] "gold1" "gold2"
## [145] "gold3" "gold4"
## [147] "goldenrod" "goldenrod1"
## [149] "goldenrod2" "goldenrod3"
## [151] "goldenrod4" "gray"
## [153] "gray0" "gray1"
## [155] "gray2" "gray3"
## [157] "gray4" "gray5"
## [159] "gray6" "gray7"
## [161] "gray8" "gray9"
## [163] "gray10" "gray11"
## [165] "gray12" "gray13"
## [167] "gray14" "gray15"
## [169] "gray16" "gray17"
## [171] "gray18" "gray19"
## [173] "gray20" "gray21"
## [175] "gray22" "gray23"
## [177] "gray24" "gray25"
## [179] "gray26" "gray27"
## [181] "gray28" "gray29"
## [183] "gray30" "gray31"
## [185] "gray32" "gray33"
## [187] "gray34" "gray35"
## [189] "gray36" "gray37"
## [191] "gray38" "gray39"
## [193] "gray40" "gray41"
## [195] "gray42" "gray43"
## [197] "gray44" "gray45"
## [199] "gray46" "gray47"
## [201] "gray48" "gray49"
## [203] "gray50" "gray51"
## [205] "gray52" "gray53"
## [207] "gray54" "gray55"
## [209] "gray56" "gray57"
## [211] "gray58" "gray59"
## [213] "gray60" "gray61"
## [215] "gray62" "gray63"
## [217] "gray64" "gray65"
## [219] "gray66" "gray67"
## [221] "gray68" "gray69"
## [223] "gray70" "gray71"
## [225] "gray72" "gray73"
## [227] "gray74" "gray75"
## [229] "gray76" "gray77"
## [231] "gray78" "gray79"
## [233] "gray80" "gray81"
## [235] "gray82" "gray83"
## [237] "gray84" "gray85"
## [239] "gray86" "gray87"
## [241] "gray88" "gray89"
## [243] "gray90" "gray91"
## [245] "gray92" "gray93"
## [247] "gray94" "gray95"
## [249] "gray96" "gray97"
## [251] "gray98" "gray99"
## [253] "gray100" "green"
## [255] "green1" "green2"
## [257] "green3" "green4"
## [259] "greenyellow" "grey"
## [261] "grey0" "grey1"
## [263] "grey2" "grey3"
## [265] "grey4" "grey5"
## [267] "grey6" "grey7"
## [269] "grey8" "grey9"
## [271] "grey10" "grey11"
## [273] "grey12" "grey13"
## [275] "grey14" "grey15"
## [277] "grey16" "grey17"
## [279] "grey18" "grey19"
## [281] "grey20" "grey21"
## [283] "grey22" "grey23"
## [285] "grey24" "grey25"
## [287] "grey26" "grey27"
## [289] "grey28" "grey29"
## [291] "grey30" "grey31"
## [293] "grey32" "grey33"
## [295] "grey34" "grey35"
## [297] "grey36" "grey37"
## [299] "grey38" "grey39"
## [301] "grey40" "grey41"
## [303] "grey42" "grey43"
## [305] "grey44" "grey45"
## [307] "grey46" "grey47"
## [309] "grey48" "grey49"
## [311] "grey50" "grey51"
## [313] "grey52" "grey53"
## [315] "grey54" "grey55"
## [317] "grey56" "grey57"
## [319] "grey58" "grey59"
## [321] "grey60" "grey61"
## [323] "grey62" "grey63"
## [325] "grey64" "grey65"
## [327] "grey66" "grey67"
## [329] "grey68" "grey69"
## [331] "grey70" "grey71"
## [333] "grey72" "grey73"
## [335] "grey74" "grey75"
## [337] "grey76" "grey77"
## [339] "grey78" "grey79"
## [341] "grey80" "grey81"
## [343] "grey82" "grey83"
## [345] "grey84" "grey85"
## [347] "grey86" "grey87"
## [349] "grey88" "grey89"
## [351] "grey90" "grey91"
## [353] "grey92" "grey93"
## [355] "grey94" "grey95"
## [357] "grey96" "grey97"
## [359] "grey98" "grey99"
## [361] "grey100" "honeydew"
## [363] "honeydew1" "honeydew2"
## [365] "honeydew3" "honeydew4"
## [367] "hotpink" "hotpink1"
## [369] "hotpink2" "hotpink3"
## [371] "hotpink4" "indianred"
## [373] "indianred1" "indianred2"
## [375] "indianred3" "indianred4"
## [377] "ivory" "ivory1"
## [379] "ivory2" "ivory3"
## [381] "ivory4" "khaki"
## [383] "khaki1" "khaki2"
## [385] "khaki3" "khaki4"
## [387] "lavender" "lavenderblush"
## [389] "lavenderblush1" "lavenderblush2"
## [391] "lavenderblush3" "lavenderblush4"
## [393] "lawngreen" "lemonchiffon"
## [395] "lemonchiffon1" "lemonchiffon2"
## [397] "lemonchiffon3" "lemonchiffon4"
## [399] "lightblue" "lightblue1"
## [401] "lightblue2" "lightblue3"
## [403] "lightblue4" "lightcoral"
## [405] "lightcyan" "lightcyan1"
## [407] "lightcyan2" "lightcyan3"
## [409] "lightcyan4" "lightgoldenrod"
## [411] "lightgoldenrod1" "lightgoldenrod2"
## [413] "lightgoldenrod3" "lightgoldenrod4"
## [415] "lightgoldenrodyellow" "lightgray"
## [417] "lightgreen" "lightgrey"
## [419] "lightpink" "lightpink1"
## [421] "lightpink2" "lightpink3"
## [423] "lightpink4" "lightsalmon"
## [425] "lightsalmon1" "lightsalmon2"
## [427] "lightsalmon3" "lightsalmon4"
## [429] "lightseagreen" "lightskyblue"
## [431] "lightskyblue1" "lightskyblue2"
## [433] "lightskyblue3" "lightskyblue4"
## [435] "lightslateblue" "lightslategray"
## [437] "lightslategrey" "lightsteelblue"
## [439] "lightsteelblue1" "lightsteelblue2"
## [441] "lightsteelblue3" "lightsteelblue4"
## [443] "lightyellow" "lightyellow1"
## [445] "lightyellow2" "lightyellow3"
## [447] "lightyellow4" "limegreen"
## [449] "linen" "magenta"
## [451] "magenta1" "magenta2"
## [453] "magenta3" "magenta4"
## [455] "maroon" "maroon1"
## [457] "maroon2" "maroon3"
## [459] "maroon4" "mediumaquamarine"
## [461] "mediumblue" "mediumorchid"
## [463] "mediumorchid1" "mediumorchid2"
## [465] "mediumorchid3" "mediumorchid4"
## [467] "mediumpurple" "mediumpurple1"
## [469] "mediumpurple2" "mediumpurple3"
## [471] "mediumpurple4" "mediumseagreen"
## [473] "mediumslateblue" "mediumspringgreen"
## [475] "mediumturquoise" "mediumvioletred"
## [477] "midnightblue" "mintcream"
## [479] "mistyrose" "mistyrose1"
## [481] "mistyrose2" "mistyrose3"
## [483] "mistyrose4" "moccasin"
## [485] "navajowhite" "navajowhite1"
## [487] "navajowhite2" "navajowhite3"
## [489] "navajowhite4" "navy"
## [491] "navyblue" "oldlace"
## [493] "olivedrab" "olivedrab1"
## [495] "olivedrab2" "olivedrab3"
## [497] "olivedrab4" "orange"
## [499] "orange1" "orange2"
## [501] "orange3" "orange4"
## [503] "orangered" "orangered1"
## [505] "orangered2" "orangered3"
## [507] "orangered4" "orchid"
## [509] "orchid1" "orchid2"
## [511] "orchid3" "orchid4"
## [513] "palegoldenrod" "palegreen"
## [515] "palegreen1" "palegreen2"
## [517] "palegreen3" "palegreen4"
## [519] "paleturquoise" "paleturquoise1"
## [521] "paleturquoise2" "paleturquoise3"
## [523] "paleturquoise4" "palevioletred"
## [525] "palevioletred1" "palevioletred2"
## [527] "palevioletred3" "palevioletred4"
## [529] "papayawhip" "peachpuff"
## [531] "peachpuff1" "peachpuff2"
## [533] "peachpuff3" "peachpuff4"
## [535] "peru" "pink"
## [537] "pink1" "pink2"
## [539] "pink3" "pink4"
## [541] "plum" "plum1"
## [543] "plum2" "plum3"
## [545] "plum4" "powderblue"
## [547] "purple" "purple1"
## [549] "purple2" "purple3"
## [551] "purple4" "red"
## [553] "red1" "red2"
## [555] "red3" "red4"
## [557] "rosybrown" "rosybrown1"
## [559] "rosybrown2" "rosybrown3"
## [561] "rosybrown4" "royalblue"
## [563] "royalblue1" "royalblue2"
## [565] "royalblue3" "royalblue4"
## [567] "saddlebrown" "salmon"
## [569] "salmon1" "salmon2"
## [571] "salmon3" "salmon4"
## [573] "sandybrown" "seagreen"
## [575] "seagreen1" "seagreen2"
## [577] "seagreen3" "seagreen4"
## [579] "seashell" "seashell1"
## [581] "seashell2" "seashell3"
## [583] "seashell4" "sienna"
## [585] "sienna1" "sienna2"
## [587] "sienna3" "sienna4"
## [589] "skyblue" "skyblue1"
## [591] "skyblue2" "skyblue3"
## [593] "skyblue4" "slateblue"
## [595] "slateblue1" "slateblue2"
## [597] "slateblue3" "slateblue4"
## [599] "slategray" "slategray1"
## [601] "slategray2" "slategray3"
## [603] "slategray4" "slategrey"
## [605] "snow" "snow1"
## [607] "snow2" "snow3"
## [609] "snow4" "springgreen"
## [611] "springgreen1" "springgreen2"
## [613] "springgreen3" "springgreen4"
## [615] "steelblue" "steelblue1"
## [617] "steelblue2" "steelblue3"
## [619] "steelblue4" "tan"
## [621] "tan1" "tan2"
## [623] "tan3" "tan4"
## [625] "thistle" "thistle1"
## [627] "thistle2" "thistle3"
## [629] "thistle4" "tomato"
## [631] "tomato1" "tomato2"
## [633] "tomato3" "tomato4"
## [635] "turquoise" "turquoise1"
## [637] "turquoise2" "turquoise3"
## [639] "turquoise4" "violet"
## [641] "violetred" "violetred1"
## [643] "violetred2" "violetred3"
## [645] "violetred4" "wheat"
## [647] "wheat1" "wheat2"
## [649] "wheat3" "wheat4"
## [651] "whitesmoke" "yellow"
## [653] "yellow1" "yellow2"
## [655] "yellow3" "yellow4"
## [657] "yellowgreen"