▶️ Tematyka

  • Eksport danych
    • Eksport do pliku txt
    • Eksport do pliku w formacie csv
  • Import danych
    • Import danych z pliku txt
    • Import danych z pliku csv (separatorem dziesiętnym jest ,)
    • Import danych z pliku csv (separatorem dziesiętnym jest .)
    • Wklejanie ze schowka
  • Katalog roboczy
  • Podstawowe wykresy
  • Elementy rachunku prawdopodobieństwa


📘 Eksport danych

# Uwaga. Przy eksporcie i później imporcie danych należy dostosować ścieżki dostępu 
# do plików do własnego komputera.

dane=head(swiss)    #Dane do eksportu.
dane
##              Fertility Agriculture Examination Education Catholic
## Courtelary        80.2        17.0          15        12     9.96
## Delemont          83.1        45.1           6         9    84.84
## Franches-Mnt      92.5        39.7           5         5    93.40
## Moutier           85.8        36.5          12         7    33.77
## Neuveville        76.9        43.5          17        15     5.16
## Porrentruy        76.1        35.3           9         7    90.57
##              Infant.Mortality
## Courtelary               22.2
## Delemont                 22.2
## Franches-Mnt             20.2
## Moutier                  20.3
## Neuveville               20.6
## Porrentruy               26.6

Eksport do pliku txt

write.table(dane,file="swiss_dane.txt",quote=F, row.names=T,col.names=T,sep="\t")

# Dla zapisania pliku w ustonym katalogu przykładowa komenda może być następująca:
# write.table(dane,file="C:/Users/Ania/Desktop/swiss_dane.txt",quote=F,row.names=T,col.names=T,sep="\t")

Eksport do pliku w formacie csv

write.csv(dane,"swiss_dane2.csv")
write.csv2(dane,"swiss_dane3.csv")

📘 Import danych

Import danych z pliku txt

im1=read.table("swiss_dane.txt",header=T,sep="\t")
# Dla odczytu pliku a ustalonego katalogu przykładowa komenda może być następująca:
# im1=read.table("C:/Users/Ania/Desktop/swiss_dane.txt",header=T,sep="\t")
im1
##              Fertility Agriculture Examination Education Catholic
## Courtelary        80.2        17.0          15        12     9.96
## Delemont          83.1        45.1           6         9    84.84
## Franches-Mnt      92.5        39.7           5         5    93.40
## Moutier           85.8        36.5          12         7    33.77
## Neuveville        76.9        43.5          17        15     5.16
## Porrentruy        76.1        35.3           9         7    90.57
##              Infant.Mortality
## Courtelary               22.2
## Delemont                 22.2
## Franches-Mnt             20.2
## Moutier                  20.3
## Neuveville               20.6
## Porrentruy               26.6

Import danych z pliku csv (separatorem dziesiętnym jest ,)

im2=read.csv2("swiss_dane3.csv")
head(im2)
##              X Fertility Agriculture Examination Education Catholic
## 1   Courtelary      80.2        17.0          15        12     9.96
## 2     Delemont      83.1        45.1           6         9    84.84
## 3 Franches-Mnt      92.5        39.7           5         5    93.40
## 4      Moutier      85.8        36.5          12         7    33.77
## 5   Neuveville      76.9        43.5          17        15     5.16
## 6   Porrentruy      76.1        35.3           9         7    90.57
##   Infant.Mortality
## 1             22.2
## 2             22.2
## 3             20.2
## 4             20.3
## 5             20.6
## 6             26.6

Import danych z pliku csv (separatorem dziesiętnym jest .)

im3=read.csv("swiss_dane2.csv")
head(im3)
##              X Fertility Agriculture Examination Education Catholic
## 1   Courtelary      80.2        17.0          15        12     9.96
## 2     Delemont      83.1        45.1           6         9    84.84
## 3 Franches-Mnt      92.5        39.7           5         5    93.40
## 4      Moutier      85.8        36.5          12         7    33.77
## 5   Neuveville      76.9        43.5          17        15     5.16
## 6   Porrentruy      76.1        35.3           9         7    90.57
##   Infant.Mortality
## 1             22.2
## 2             22.2
## 3             20.2
## 4             20.3
## 5             20.6
## 6             26.6

Wklejanie ze schowka

# Import danych za pomocą wklejenia danych ze schowka. 
# Na początku pisze się kod, a następnie kopiuje dane, a potem wykonuje kod. 
# Dane można skopiować również na początku.
im4=read.delim2("clipboard",header=T)
im4
##   cena wartość ilość
## 1 5.64  231.30  41.0
## 2 5.94  246.93  41.6
## 3 5.79  237.34  41.0
## 4 5.49  225.09  41.0
## 5 5.58  223.26  40.0
## 6 5.68  227.20  40.0
## 7 5.09  193.42  38.0
## 8 5.16  206.40  40.0

📘 Katalog roboczy

#Sprawdzenie jaki jest katalog roboczy - gdzie się znajduje.

getwd()
## [1] "E:/Doc_UE/RStudio/Samouczek"
# Zmiana katalogu roboczego.
# Zamieniamy obecny katalog roboczy, żeby pracować w innym katalogu i nie musieć podawać ścieżki dostępu do plików przy imporcie czy eksporcie danych.

setwd("C:/Users/Ania/Desktop")
## Error in setwd("C:/Users/Ania/Desktop"): nie można zmienić katalogu roboczego
getwd() #Faktycznie katalog roboczy został zmieniony.
## [1] "E:/Doc_UE/RStudio/Samouczek"
# Eksport pliku do folderu o domyślnej lokalizacji. 
write.csv2(dane,"iris1.csv")  #Nie podaje się już ścieżki dostępu, tylko jak ma się nazywać plik zz danymi i jego rozszerzenie (w cudzysłowie). 

# Import pliku z folderu o domyślnej lokalizacji.
read.csv2("iris1.csv")  #Nie podaje się już ścieżki dostępu do pliku, tylko w cudzysłowie zamieszcza się nazwę i rozszerzenie pliku, który chcesię  zaimportować do R.
##              X Fertility Agriculture Examination Education Catholic
## 1   Courtelary      80.2        17.0          15        12     9.96
## 2     Delemont      83.1        45.1           6         9    84.84
## 3 Franches-Mnt      92.5        39.7           5         5    93.40
## 4      Moutier      85.8        36.5          12         7    33.77
## 5   Neuveville      76.9        43.5          17        15     5.16
## 6   Porrentruy      76.1        35.3           9         7    90.57
##   Infant.Mortality
## 1             22.2
## 2             22.2
## 3             20.2
## 4             20.3
## 5             20.6
## 6             26.6

📘 Podstawowe wykresy

x=c(65,43,21,43,57)
y=c(0,0.5,3,2.2,5)

plot(x)

plot(x,type="l")

plot(x,type="p")

plot(x,type="b")

plot(x,type="h")

plot(x,type="s")

plot(x,type="l",col=6)

plot(x,type="l",col=6,lwd=3)

plot(x,type="l",col=6,lwd=3,lty=5)

plot(x,type="l",col=6,lwd=3,lty=2)

plot(x,y)

plot(x,y,lwd=2)

plot(x,y,lwd=2,main="Czas sprzątania względem wieku")

plot(x,y,lwd=2,main="Czas sprzątania względem wieku",xlab="wiek")

plot(x,y,lwd=2,main="Czas sprzątania względem wieku",xlab="wiek",ylab="czas sprzątania")

?AirPassengers
## uruchamianie serwera httpd dla pomocy ... wykonano
plot(AirPassengers)

class(AirPassengers)
## [1] "ts"

Histogram

trees
##    Girth Height Volume
## 1    8.3     70   10.3
## 2    8.6     65   10.3
## 3    8.8     63   10.2
## 4   10.5     72   16.4
## 5   10.7     81   18.8
## 6   10.8     83   19.7
## 7   11.0     66   15.6
## 8   11.0     75   18.2
## 9   11.1     80   22.6
## 10  11.2     75   19.9
## 11  11.3     79   24.2
## 12  11.4     76   21.0
## 13  11.4     76   21.4
## 14  11.7     69   21.3
## 15  12.0     75   19.1
## 16  12.9     74   22.2
## 17  12.9     85   33.8
## 18  13.3     86   27.4
## 19  13.7     71   25.7
## 20  13.8     64   24.9
## 21  14.0     78   34.5
## 22  14.2     80   31.7
## 23  14.5     74   36.3
## 24  16.0     72   38.3
## 25  16.3     77   42.6
## 26  17.3     81   55.4
## 27  17.5     82   55.7
## 28  17.9     80   58.3
## 29  18.0     80   51.5
## 30  18.0     80   51.0
## 31  20.6     87   77.0
hist(trees$Height)

hist(trees$Height,prob=T)
hist(trees$Height,freq=F)

par(mfrow=c(2,1))
hist(trees$Height)
hist(trees$Height,prob=T)

par(mfrow=c(1,1))
hist(trees$Height)

hist(trees$Height,breaks=10)

hist(trees$Height,breaks=c(60,70,80,90,100))

hist(trees$Height,col="pink")

colors()
##   [1] "white"                "aliceblue"            "antiquewhite"        
##   [4] "antiquewhite1"        "antiquewhite2"        "antiquewhite3"       
##   [7] "antiquewhite4"        "aquamarine"           "aquamarine1"         
##  [10] "aquamarine2"          "aquamarine3"          "aquamarine4"         
##  [13] "azure"                "azure1"               "azure2"              
##  [16] "azure3"               "azure4"               "beige"               
##  [19] "bisque"               "bisque1"              "bisque2"             
##  [22] "bisque3"              "bisque4"              "black"               
##  [25] "blanchedalmond"       "blue"                 "blue1"               
##  [28] "blue2"                "blue3"                "blue4"               
##  [31] "blueviolet"           "brown"                "brown1"              
##  [34] "brown2"               "brown3"               "brown4"              
##  [37] "burlywood"            "burlywood1"           "burlywood2"          
##  [40] "burlywood3"           "burlywood4"           "cadetblue"           
##  [43] "cadetblue1"           "cadetblue2"           "cadetblue3"          
##  [46] "cadetblue4"           "chartreuse"           "chartreuse1"         
##  [49] "chartreuse2"          "chartreuse3"          "chartreuse4"         
##  [52] "chocolate"            "chocolate1"           "chocolate2"          
##  [55] "chocolate3"           "chocolate4"           "coral"               
##  [58] "coral1"               "coral2"               "coral3"              
##  [61] "coral4"               "cornflowerblue"       "cornsilk"            
##  [64] "cornsilk1"            "cornsilk2"            "cornsilk3"           
##  [67] "cornsilk4"            "cyan"                 "cyan1"               
##  [70] "cyan2"                "cyan3"                "cyan4"               
##  [73] "darkblue"             "darkcyan"             "darkgoldenrod"       
##  [76] "darkgoldenrod1"       "darkgoldenrod2"       "darkgoldenrod3"      
##  [79] "darkgoldenrod4"       "darkgray"             "darkgreen"           
##  [82] "darkgrey"             "darkkhaki"            "darkmagenta"         
##  [85] "darkolivegreen"       "darkolivegreen1"      "darkolivegreen2"     
##  [88] "darkolivegreen3"      "darkolivegreen4"      "darkorange"          
##  [91] "darkorange1"          "darkorange2"          "darkorange3"         
##  [94] "darkorange4"          "darkorchid"           "darkorchid1"         
##  [97] "darkorchid2"          "darkorchid3"          "darkorchid4"         
## [100] "darkred"              "darksalmon"           "darkseagreen"        
## [103] "darkseagreen1"        "darkseagreen2"        "darkseagreen3"       
## [106] "darkseagreen4"        "darkslateblue"        "darkslategray"       
## [109] "darkslategray1"       "darkslategray2"       "darkslategray3"      
## [112] "darkslategray4"       "darkslategrey"        "darkturquoise"       
## [115] "darkviolet"           "deeppink"             "deeppink1"           
## [118] "deeppink2"            "deeppink3"            "deeppink4"           
## [121] "deepskyblue"          "deepskyblue1"         "deepskyblue2"        
## [124] "deepskyblue3"         "deepskyblue4"         "dimgray"             
## [127] "dimgrey"              "dodgerblue"           "dodgerblue1"         
## [130] "dodgerblue2"          "dodgerblue3"          "dodgerblue4"         
## [133] "firebrick"            "firebrick1"           "firebrick2"          
## [136] "firebrick3"           "firebrick4"           "floralwhite"         
## [139] "forestgreen"          "gainsboro"            "ghostwhite"          
## [142] "gold"                 "gold1"                "gold2"               
## [145] "gold3"                "gold4"                "goldenrod"           
## [148] "goldenrod1"           "goldenrod2"           "goldenrod3"          
## [151] "goldenrod4"           "gray"                 "gray0"               
## [154] "gray1"                "gray2"                "gray3"               
## [157] "gray4"                "gray5"                "gray6"               
## [160] "gray7"                "gray8"                "gray9"               
## [163] "gray10"               "gray11"               "gray12"              
## [166] "gray13"               "gray14"               "gray15"              
## [169] "gray16"               "gray17"               "gray18"              
## [172] "gray19"               "gray20"               "gray21"              
## [175] "gray22"               "gray23"               "gray24"              
## [178] "gray25"               "gray26"               "gray27"              
## [181] "gray28"               "gray29"               "gray30"              
## [184] "gray31"               "gray32"               "gray33"              
## [187] "gray34"               "gray35"               "gray36"              
## [190] "gray37"               "gray38"               "gray39"              
## [193] "gray40"               "gray41"               "gray42"              
## [196] "gray43"               "gray44"               "gray45"              
## [199] "gray46"               "gray47"               "gray48"              
## [202] "gray49"               "gray50"               "gray51"              
## [205] "gray52"               "gray53"               "gray54"              
## [208] "gray55"               "gray56"               "gray57"              
## [211] "gray58"               "gray59"               "gray60"              
## [214] "gray61"               "gray62"               "gray63"              
## [217] "gray64"               "gray65"               "gray66"              
## [220] "gray67"               "gray68"               "gray69"              
## [223] "gray70"               "gray71"               "gray72"              
## [226] "gray73"               "gray74"               "gray75"              
## [229] "gray76"               "gray77"               "gray78"              
## [232] "gray79"               "gray80"               "gray81"              
## [235] "gray82"               "gray83"               "gray84"              
## [238] "gray85"               "gray86"               "gray87"              
## [241] "gray88"               "gray89"               "gray90"              
## [244] "gray91"               "gray92"               "gray93"              
## [247] "gray94"               "gray95"               "gray96"              
## [250] "gray97"               "gray98"               "gray99"              
## [253] "gray100"              "green"                "green1"              
## [256] "green2"               "green3"               "green4"              
## [259] "greenyellow"          "grey"                 "grey0"               
## [262] "grey1"                "grey2"                "grey3"               
## [265] "grey4"                "grey5"                "grey6"               
## [268] "grey7"                "grey8"                "grey9"               
## [271] "grey10"               "grey11"               "grey12"              
## [274] "grey13"               "grey14"               "grey15"              
## [277] "grey16"               "grey17"               "grey18"              
## [280] "grey19"               "grey20"               "grey21"              
## [283] "grey22"               "grey23"               "grey24"              
## [286] "grey25"               "grey26"               "grey27"              
## [289] "grey28"               "grey29"               "grey30"              
## [292] "grey31"               "grey32"               "grey33"              
## [295] "grey34"               "grey35"               "grey36"              
## [298] "grey37"               "grey38"               "grey39"              
## [301] "grey40"               "grey41"               "grey42"              
## [304] "grey43"               "grey44"               "grey45"              
## [307] "grey46"               "grey47"               "grey48"              
## [310] "grey49"               "grey50"               "grey51"              
## [313] "grey52"               "grey53"               "grey54"              
## [316] "grey55"               "grey56"               "grey57"              
## [319] "grey58"               "grey59"               "grey60"              
## [322] "grey61"               "grey62"               "grey63"              
## [325] "grey64"               "grey65"               "grey66"              
## [328] "grey67"               "grey68"               "grey69"              
## [331] "grey70"               "grey71"               "grey72"              
## [334] "grey73"               "grey74"               "grey75"              
## [337] "grey76"               "grey77"               "grey78"              
## [340] "grey79"               "grey80"               "grey81"              
## [343] "grey82"               "grey83"               "grey84"              
## [346] "grey85"               "grey86"               "grey87"              
## [349] "grey88"               "grey89"               "grey90"              
## [352] "grey91"               "grey92"               "grey93"              
## [355] "grey94"               "grey95"               "grey96"              
## [358] "grey97"               "grey98"               "grey99"              
## [361] "grey100"              "honeydew"             "honeydew1"           
## [364] "honeydew2"            "honeydew3"            "honeydew4"           
## [367] "hotpink"              "hotpink1"             "hotpink2"            
## [370] "hotpink3"             "hotpink4"             "indianred"           
## [373] "indianred1"           "indianred2"           "indianred3"          
## [376] "indianred4"           "ivory"                "ivory1"              
## [379] "ivory2"               "ivory3"               "ivory4"              
## [382] "khaki"                "khaki1"               "khaki2"              
## [385] "khaki3"               "khaki4"               "lavender"            
## [388] "lavenderblush"        "lavenderblush1"       "lavenderblush2"      
## [391] "lavenderblush3"       "lavenderblush4"       "lawngreen"           
## [394] "lemonchiffon"         "lemonchiffon1"        "lemonchiffon2"       
## [397] "lemonchiffon3"        "lemonchiffon4"        "lightblue"           
## [400] "lightblue1"           "lightblue2"           "lightblue3"          
## [403] "lightblue4"           "lightcoral"           "lightcyan"           
## [406] "lightcyan1"           "lightcyan2"           "lightcyan3"          
## [409] "lightcyan4"           "lightgoldenrod"       "lightgoldenrod1"     
## [412] "lightgoldenrod2"      "lightgoldenrod3"      "lightgoldenrod4"     
## [415] "lightgoldenrodyellow" "lightgray"            "lightgreen"          
## [418] "lightgrey"            "lightpink"            "lightpink1"          
## [421] "lightpink2"           "lightpink3"           "lightpink4"          
## [424] "lightsalmon"          "lightsalmon1"         "lightsalmon2"        
## [427] "lightsalmon3"         "lightsalmon4"         "lightseagreen"       
## [430] "lightskyblue"         "lightskyblue1"        "lightskyblue2"       
## [433] "lightskyblue3"        "lightskyblue4"        "lightslateblue"      
## [436] "lightslategray"       "lightslategrey"       "lightsteelblue"      
## [439] "lightsteelblue1"      "lightsteelblue2"      "lightsteelblue3"     
## [442] "lightsteelblue4"      "lightyellow"          "lightyellow1"        
## [445] "lightyellow2"         "lightyellow3"         "lightyellow4"        
## [448] "limegreen"            "linen"                "magenta"             
## [451] "magenta1"             "magenta2"             "magenta3"            
## [454] "magenta4"             "maroon"               "maroon1"             
## [457] "maroon2"              "maroon3"              "maroon4"             
## [460] "mediumaquamarine"     "mediumblue"           "mediumorchid"        
## [463] "mediumorchid1"        "mediumorchid2"        "mediumorchid3"       
## [466] "mediumorchid4"        "mediumpurple"         "mediumpurple1"       
## [469] "mediumpurple2"        "mediumpurple3"        "mediumpurple4"       
## [472] "mediumseagreen"       "mediumslateblue"      "mediumspringgreen"   
## [475] "mediumturquoise"      "mediumvioletred"      "midnightblue"        
## [478] "mintcream"            "mistyrose"            "mistyrose1"          
## [481] "mistyrose2"           "mistyrose3"           "mistyrose4"          
## [484] "moccasin"             "navajowhite"          "navajowhite1"        
## [487] "navajowhite2"         "navajowhite3"         "navajowhite4"        
## [490] "navy"                 "navyblue"             "oldlace"             
## [493] "olivedrab"            "olivedrab1"           "olivedrab2"          
## [496] "olivedrab3"           "olivedrab4"           "orange"              
## [499] "orange1"              "orange2"              "orange3"             
## [502] "orange4"              "orangered"            "orangered1"          
## [505] "orangered2"           "orangered3"           "orangered4"          
## [508] "orchid"               "orchid1"              "orchid2"             
## [511] "orchid3"              "orchid4"              "palegoldenrod"       
## [514] "palegreen"            "palegreen1"           "palegreen2"          
## [517] "palegreen3"           "palegreen4"           "paleturquoise"       
## [520] "paleturquoise1"       "paleturquoise2"       "paleturquoise3"      
## [523] "paleturquoise4"       "palevioletred"        "palevioletred1"      
## [526] "palevioletred2"       "palevioletred3"       "palevioletred4"      
## [529] "papayawhip"           "peachpuff"            "peachpuff1"          
## [532] "peachpuff2"           "peachpuff3"           "peachpuff4"          
## [535] "peru"                 "pink"                 "pink1"               
## [538] "pink2"                "pink3"                "pink4"               
## [541] "plum"                 "plum1"                "plum2"               
## [544] "plum3"                "plum4"                "powderblue"          
## [547] "purple"               "purple1"              "purple2"             
## [550] "purple3"              "purple4"              "red"                 
## [553] "red1"                 "red2"                 "red3"                
## [556] "red4"                 "rosybrown"            "rosybrown1"          
## [559] "rosybrown2"           "rosybrown3"           "rosybrown4"          
## [562] "royalblue"            "royalblue1"           "royalblue2"          
## [565] "royalblue3"           "royalblue4"           "saddlebrown"         
## [568] "salmon"               "salmon1"              "salmon2"             
## [571] "salmon3"              "salmon4"              "sandybrown"          
## [574] "seagreen"             "seagreen1"            "seagreen2"           
## [577] "seagreen3"            "seagreen4"            "seashell"            
## [580] "seashell1"            "seashell2"            "seashell3"           
## [583] "seashell4"            "sienna"               "sienna1"             
## [586] "sienna2"              "sienna3"              "sienna4"             
## [589] "skyblue"              "skyblue1"             "skyblue2"            
## [592] "skyblue3"             "skyblue4"             "slateblue"           
## [595] "slateblue1"           "slateblue2"           "slateblue3"          
## [598] "slateblue4"           "slategray"            "slategray1"          
## [601] "slategray2"           "slategray3"           "slategray4"          
## [604] "slategrey"            "snow"                 "snow1"               
## [607] "snow2"                "snow3"                "snow4"               
## [610] "springgreen"          "springgreen1"         "springgreen2"        
## [613] "springgreen3"         "springgreen4"         "steelblue"           
## [616] "steelblue1"           "steelblue2"           "steelblue3"          
## [619] "steelblue4"           "tan"                  "tan1"                
## [622] "tan2"                 "tan3"                 "tan4"                
## [625] "thistle"              "thistle1"             "thistle2"            
## [628] "thistle3"             "thistle4"             "tomato"              
## [631] "tomato1"              "tomato2"              "tomato3"             
## [634] "tomato4"              "turquoise"            "turquoise1"          
## [637] "turquoise2"           "turquoise3"           "turquoise4"          
## [640] "violet"               "violetred"            "violetred1"          
## [643] "violetred2"           "violetred3"           "violetred4"          
## [646] "wheat"                "wheat1"               "wheat2"              
## [649] "wheat3"               "wheat4"               "whitesmoke"          
## [652] "yellow"               "yellow1"              "yellow2"             
## [655] "yellow3"              "yellow4"              "yellowgreen"
sample(colors(),10)
##  [1] "grey40"        "grey89"        "grey86"        "slategray"    
##  [5] "olivedrab3"    "sienna4"       "plum4"         "cornsilk1"    
##  [9] "lightcoral"    "mediumorchid2"
hist(trees$Height,col=sample(colors(),6))

hist(trees$Height,border="blue",col="pink")

hist(trees$Height,col=rainbow(3))

hist(trees$Height,col=c("blue","pink","yellow"))

hist(trees$Height,border="tomato1",breaks=c(60,70,80,90,100),col="pink")

hist(trees$Height,col=c("blue","pink","yellow","grey"))

Wykres pudełkowy

boxplot(trees$Height,col="red")

head(trees)
##   Girth Height Volume
## 1   8.3     70   10.3
## 2   8.6     65   10.3
## 3   8.8     63   10.2
## 4  10.5     72   16.4
## 5  10.7     81   18.8
## 6  10.8     83   19.7

Wykresy rozrzutu

plot(trees$Height,trees$Volume)

pairs(trees)

Wykres słupkowy

head(chickwts)
##   weight      feed
## 1    179 horsebean
## 2    160 horsebean
## 3    136 horsebean
## 4    227 horsebean
## 5    217 horsebean
## 6    168 horsebean
chickwts$feed
##  [1] horsebean horsebean horsebean horsebean horsebean horsebean horsebean
##  [8] horsebean horsebean horsebean linseed   linseed   linseed   linseed  
## [15] linseed   linseed   linseed   linseed   linseed   linseed   linseed  
## [22] linseed   soybean   soybean   soybean   soybean   soybean   soybean  
## [29] soybean   soybean   soybean   soybean   soybean   soybean   soybean  
## [36] soybean   sunflower sunflower sunflower sunflower sunflower sunflower
## [43] sunflower sunflower sunflower sunflower sunflower sunflower meatmeal 
## [50] meatmeal  meatmeal  meatmeal  meatmeal  meatmeal  meatmeal  meatmeal 
## [57] meatmeal  meatmeal  meatmeal  casein    casein    casein    casein   
## [64] casein    casein    casein    casein    casein    casein    casein   
## [71] casein   
## Levels: casein horsebean linseed meatmeal soybean sunflower
# barplot(chickwts$feed)
# Powyższy kod nie zadziała, ponieważ dane powinny być wektorem lub macierzą.

t=table(chickwts$feed)
t
## 
##    casein horsebean   linseed  meatmeal   soybean sunflower 
##        12        10        12        11        14        12
barplot(t)

Wykres kołowy

pie(t)

procenty=round(prop.table(t)*100,1)
procenty
## 
##    casein horsebean   linseed  meatmeal   soybean sunflower 
##      16.9      14.1      16.9      15.5      19.7      16.9
pie(t,labels=paste(names(procenty),procenty,"%"))

pie(t,labels=paste(names(t),procenty,"%"))

📘 Elementy rachunku prawdopodobieństwa

# Elementy rachunku prawdopodobieństwa: generowanie wartości z rozkładów, gęstość, dystrybuanta i kwantyle.

Generowanie wartości z rozkładów

?rnorm

rnorm(20)
##  [1]  0.663661029  0.737207632  1.185194239 -0.639784038  0.455718207
##  [6] -0.121125555 -0.060011578  1.724591739 -0.970003484 -0.705763815
## [11]  2.745267291  0.005734573 -0.190608796  0.228148020 -0.257944166
## [16]  0.071950285 -0.979958663  0.376706193  0.198633308  1.247157955
rnorm(100,20,13)
##   [1]  13.4617664  -1.9510993  26.5319143  30.8791513  22.9374222  17.8514230
##   [7]  22.8787268  26.0717342   7.8761488  -5.8396177  38.0229916  27.4031349
##  [13]  39.9167464  25.3795626  17.6772933  23.0212719  15.4932065  46.4466135
##  [19]   7.4811643  14.8948796  21.4285714  29.5184909  24.4559478  38.5472827
##  [25]  -8.8336812  13.7816066  -1.4420144  15.7512748  40.9109695  28.9171889
##  [31]  22.2844159  25.0161140  27.9129226  38.3494161  30.8453834   5.9732789
##  [37]  17.5889788  33.1381900  -9.9519619  24.6582627   5.3415509   7.5619627
##  [43]  10.0608623  26.5960617  18.2209738  16.2768193  21.8908736  12.5898190
##  [49]  11.6948632  20.2343297  12.2306720   7.9973232  -0.2708146  27.2000297
##  [55]  26.0410089  32.3468019  11.3126659  14.3306998  31.9872599  13.3895533
##  [61]  29.0829639  27.4347567  21.8366002  36.2473378  29.5325971  34.8553535
##  [67]   5.4564581  35.9389551  22.0067645 -12.1067415  18.2896181  34.3270786
##  [73]  21.5674601  -4.9820654  24.9542461  24.1915089  47.3294781  20.4205197
##  [79]  24.6066449  -4.2964577   8.7157172  19.5020421  11.8795873  22.7317571
##  [85]  16.3782836  19.2304844   8.7769552  46.1361395   2.7234274   3.2453214
##  [91]  35.0073992  23.2832662  27.5148795  25.2148862   3.4055892  16.4043321
##  [97]  27.6565503  13.7552069  27.5621758   9.5624054
rnorm(100,sd=13,mean=20)
##   [1]  19.0709368  22.1551001  -1.9042326  25.2036983  15.4195334  37.4743697
##   [7]   5.8848813   4.5022888  17.3415006  14.1504120  15.2936710  18.0563477
##  [13]  41.8203703  -7.2874232  -7.3305122   5.6203874  39.6977051  28.8288394
##  [19]  21.4801035  21.0363860  29.7761427  25.7832826   6.6472551  17.4877387
##  [25]   2.1020683  27.8558299  12.3875013  55.3969186  44.7104679  35.4896447
##  [31]  28.2841179   9.8627847   2.7452104   8.4798604  41.8549881   3.5973551
##  [37]  15.1024549  26.9189412  22.5748619  12.8901137  10.9409298  29.6137008
##  [43]   4.1869191  16.2150847  20.2110435  14.4790360   7.3539014  43.0299086
##  [49]  13.2010255  19.6800936 -23.9637874  14.1437513   4.5013403  23.4078499
##  [55]  30.8580895  17.3633965  -3.5326969   4.7270049  33.1679167  13.2984038
##  [61]  17.2999312  16.1988946  18.1953379  22.8100332  -1.1454724  -0.8600863
##  [67]  21.5998812   0.5295292  45.5139165  30.0861151  27.2862420   3.8583841
##  [73]   9.2265384   9.3680528  10.3017267  26.6542742  30.2601877  19.3719682
##  [79]  25.8890233  32.8758616   8.1797736   2.4280145  11.8628715   8.6210813
##  [85]  44.3837974  16.5778399  26.1506495  17.1918143  38.7223182  24.6236984
##  [91]   8.5893015  14.8112653  16.7532451  17.6692389  16.3004390  43.0004755
##  [97]  11.7240404  29.0553161  26.5349822  31.9097669
set.seed(123)
n=rnorm(100, 2,0.5)
n
##   [1] 1.7197622 1.8849113 2.7793542 2.0352542 2.0646439 2.8575325 2.2304581
##   [8] 1.3674694 1.6565736 1.7771690 2.6120409 2.1799069 2.2003857 2.0553414
##  [15] 1.7220794 2.8934566 2.2489252 1.0166914 2.3506780 1.7636043 1.4660881
##  [22] 1.8910125 1.4869978 1.6355544 1.6874804 1.1566533 2.4188935 2.0766866
##  [29] 1.4309315 2.6269075 2.2132321 1.8524643 2.4475628 2.4390667 2.4107905
##  [36] 2.3443201 2.2769588 1.9690441 1.8470187 1.8097645 1.6526465 1.8960414
##  [43] 1.3673018 3.0844780 2.6039810 1.4384457 1.7985576 1.7666723 2.3899826
##  [50] 1.9583155 2.1266593 1.9857266 1.9785648 2.6843011 1.8871145 2.7582353
##  [57] 1.2256236 2.2923069 2.0619271 2.1079708 2.1898197 1.7488383 1.8333963
##  [64] 1.4907123 1.4641044 2.1517643 2.2241049 2.0265021 2.4611337 3.0250423
##  [71] 1.7544844 0.8454156 2.5028693 1.6453996 1.6559957 2.5127857 1.8576135
##  [78] 1.3896411 2.0906517 1.9305543 2.0028821 2.1926402 1.8146700 2.3221883
##  [85] 1.8897567 2.1658910 2.5484195 2.2175907 1.8370342 2.5744038 2.4967519
##  [92] 2.2741985 2.1193659 1.6860470 2.6803262 1.6998702 3.0936665 2.7663053
##  [99] 1.8821498 1.4867895
j=runif(100)
j
##   [1] 0.238726027 0.962358936 0.601365726 0.515029727 0.402573342 0.880246541
##   [7] 0.364091865 0.288239281 0.170645235 0.172171746 0.482042606 0.252964929
##  [13] 0.216254790 0.674376388 0.047663627 0.700853087 0.351888638 0.408943998
##  [19] 0.820951324 0.918857348 0.282528330 0.961104794 0.728394428 0.686375082
##  [25] 0.052843943 0.395220135 0.477845380 0.560253264 0.698261595 0.915683538
##  [31] 0.618351227 0.428421509 0.542080367 0.058478489 0.260856857 0.397151953
##  [37] 0.197744737 0.831927563 0.152887223 0.803418542 0.546826157 0.662317642
##  [43] 0.171698494 0.633055360 0.311869747 0.724554346 0.398939825 0.969356411
##  [49] 0.967398371 0.726702539 0.257216746 0.221787935 0.593045652 0.267521432
##  [55] 0.531070399 0.785291671 0.168060811 0.404399181 0.471576278 0.868106807
##  [61] 0.925707956 0.881977559 0.674186843 0.950166979 0.516444894 0.576519021
##  [67] 0.336331206 0.347324631 0.020024301 0.502813046 0.871043414 0.006300784
##  [73] 0.072057124 0.164211225 0.770334074 0.735184306 0.971875636 0.466472377
##  [79] 0.074384513 0.648818124 0.758593170 0.137106081 0.396584595 0.224985329
##  [85] 0.057958561 0.395892688 0.064928300 0.225886433 0.054629109 0.670282040
##  [91] 0.297741783 0.100721582 0.071904097 0.880440569 0.754247402 0.816605888
##  [97] 0.982140374 0.103599645 0.099041829 0.798831611
s=rt(100,df=3)
s
##   [1]  0.69683236  0.82060066 -0.02475866 -0.30644712 -0.30056295 -3.47363186
##   [7] -1.31714799 -0.46532917 -0.01932117  5.99328177 -0.02021276 -2.00872529
##  [13] -0.57321111 -0.16850664 -1.41785209 -1.20204135 -0.52541258  1.23078376
##  [19]  0.63466850  2.34977229  1.88108475 -2.20687007  6.41947266 -0.25537575
##  [25] -0.66606686 -0.01114930 -0.06244739  0.18466027 -1.25469184  0.08335478
##  [31]  0.35642587 -0.53703200  1.00405279 -0.43246746 -1.34543674  1.92487880
##  [37] -0.09990203 -1.26894058  0.83961497 -0.15032212 -0.83106377  0.01523773
##  [43]  1.00633019 -0.12547272 -1.77088587 -3.22061623 -0.07322264 -0.50701695
##  [49] -0.50295934  0.07520585  0.18176418 -0.85664567  0.67830426  1.78504381
##  [55] -0.24748408  0.93705785  1.97205423  0.21457114  0.36545186  1.70894197
##  [61] -0.34914733  0.93052910  4.51244483 -1.46438907  1.56468817  0.49041375
##  [67]  1.11727468  0.43312514  0.29718297 -2.04567260 -1.36525725 -0.66750687
##  [73] -0.49447123 -0.93744377  0.51235989 -8.61233265  0.70151938  0.89078422
##  [79] -0.21608493 -0.81058160 -0.25568636 -2.63293864 -1.00076993 -3.08952378
##  [85] -0.01260136 -0.20001064  0.93324733 -0.06879633  2.80205973 -0.95441015
##  [91] -0.78877949  0.42289498  0.75830163  0.53165633  0.85237062  0.55751630
##  [97]  0.65719967  1.41166493  0.32995741  2.43177705
c=rchisq(100,3)
c
##   [1] 3.3850922 3.2984545 1.6909650 0.1576374 6.6977146 0.4255763 2.1398074
##   [8] 3.0006399 3.1373579 2.7302180 5.8218036 0.4584186 1.6201983 0.7657052
##  [15] 2.9739183 4.1567176 5.0755977 1.1720594 1.3637716 3.0758988 2.2844511
##  [22] 3.8891418 0.6878708 6.9350646 2.1324304 4.8828211 7.9019783 1.4766519
##  [29] 0.5130345 1.5948243 1.9896741 2.0411105 2.6774314 5.5385065 2.2499958
##  [36] 3.6797567 3.8610315 4.2479516 1.0161754 5.9954669 2.3432680 2.7945191
##  [43] 3.2567545 0.9773518 4.6550916 1.8906219 9.3182640 2.7903987 1.0009019
##  [50] 2.4556660 4.1543027 2.4323302 0.9571249 2.2230586 0.3827861 0.4611069
##  [57] 1.7458361 1.7893021 3.1419871 0.1678722 0.4058235 3.9225606 5.0094145
##  [64] 2.7821002 0.9680566 1.3273275 1.1724402 0.9700765 4.1080274 4.1016847
##  [71] 0.3211031 3.6369040 2.6853121 0.6705557 0.7415162 2.1502738 6.2488106
##  [78] 2.9134959 4.3211165 1.4858849 1.6249270 2.3603811 0.6411606 0.4162545
##  [85] 2.3591057 2.3395796 4.2461522 2.3558489 3.5971023 1.0379425 4.3845427
##  [92] 5.5239566 0.4184004 3.5731431 2.1469375 0.1211514 0.7123071 1.8005444
##  [99] 1.9840761 2.8355945
f=rf(100,df1=4,df2=5)
f
##   [1]  0.52610805  1.74521305  1.02604707  3.25032204  1.52430340  0.28097148
##   [7]  0.38109784  0.55076467  0.69027447  0.48196860  4.41313923  0.73094071
##  [13]  0.37996535  2.06320618  0.84712631  0.61603119  0.71032146  0.62933857
##  [19]  2.82465565  0.41869146  2.67819390  1.77195322  0.53829047  1.08785238
##  [25]  1.95254581  1.14638625  0.27337680  0.58384781  0.33364606  0.77739386
##  [31]  0.48367768  1.58395506  0.85833052  0.08154664  0.28514547  0.84480764
##  [37]  2.62597743  1.21076087  2.08760690  1.89972530  1.17307090  0.21546587
##  [43]  1.24627808  1.35692310 17.71356434  0.20513635  1.58029619  2.90312814
##  [49]  0.07009730  1.31514070  0.00564881  3.51205181  2.68952096  1.18568879
##  [55]  1.58932593  2.01802763  1.59471709  0.75218091  0.59938150  1.70882973
##  [61]  1.77348471  0.28766346  0.17464260  1.10959418  0.83223825  4.74271587
##  [67]  3.58385752  0.09552937  1.26651119  0.28091568  0.48236156 12.00955112
##  [73]  0.69463279  0.71923276  1.15728964  1.90554090  5.86638409  0.44790960
##  [79]  0.09469444  2.63703334  0.09821284  0.21453281  0.17428717  3.00294331
##  [85]  0.38385209  3.40359044  0.66721172  0.69316870  1.09003637  0.85520126
##  [91]  3.29537360  0.66066119  0.27395316  0.10956574  0.26362624  0.53813684
##  [97]  0.24114574  0.05285393  0.37716631  0.73218773

Gęstość

dnorm(0)
## [1] 0.3989423
dnorm(0,mean=3,sd=1)
## [1] 0.004431848
dnorm(seq(-3,3,0.05))
##   [1] 0.004431848 0.005142641 0.005952532 0.006872767 0.007915452 0.009093563
##   [7] 0.010420935 0.011912244 0.013582969 0.015449347 0.017528300 0.019837354
##  [13] 0.022394530 0.025218220 0.028327038 0.031739652 0.035474593 0.039550042
##  [19] 0.043983596 0.048792019 0.053990967 0.059594706 0.065615815 0.072064874
##  [25] 0.078950158 0.086277319 0.094049077 0.102264925 0.110920835 0.120009001
##  [31] 0.129517596 0.139430566 0.149727466 0.160383327 0.171368592 0.182649085
##  [37] 0.194186055 0.205936269 0.217852177 0.229882141 0.241970725 0.254059056
##  [43] 0.266085250 0.277984886 0.289691553 0.301137432 0.312253933 0.322972360
##  [49] 0.333224603 0.342943855 0.352065327 0.360526962 0.368270140 0.375240347
##  [55] 0.381387815 0.386668117 0.391042694 0.394479331 0.396952547 0.398443914
##  [61] 0.398942280 0.398443914 0.396952547 0.394479331 0.391042694 0.386668117
##  [67] 0.381387815 0.375240347 0.368270140 0.360526962 0.352065327 0.342943855
##  [73] 0.333224603 0.322972360 0.312253933 0.301137432 0.289691553 0.277984886
##  [79] 0.266085250 0.254059056 0.241970725 0.229882141 0.217852177 0.205936269
##  [85] 0.194186055 0.182649085 0.171368592 0.160383327 0.149727466 0.139430566
##  [91] 0.129517596 0.120009001 0.110920835 0.102264925 0.094049077 0.086277319
##  [97] 0.078950158 0.072064874 0.065615815 0.059594706 0.053990967 0.048792019
## [103] 0.043983596 0.039550042 0.035474593 0.031739652 0.028327038 0.025218220
## [109] 0.022394530 0.019837354 0.017528300 0.015449347 0.013582969 0.011912244
## [115] 0.010420935 0.009093563 0.007915452 0.006872767 0.005952532 0.005142641
## [121] 0.004431848
x<-rnorm(100,3,1)
hist(x,prob=T)

lx=seq(-3,9,length=120)
lx
##   [1] -3.00000000 -2.89915966 -2.79831933 -2.69747899 -2.59663866 -2.49579832
##   [7] -2.39495798 -2.29411765 -2.19327731 -2.09243697 -1.99159664 -1.89075630
##  [13] -1.78991597 -1.68907563 -1.58823529 -1.48739496 -1.38655462 -1.28571429
##  [19] -1.18487395 -1.08403361 -0.98319328 -0.88235294 -0.78151261 -0.68067227
##  [25] -0.57983193 -0.47899160 -0.37815126 -0.27731092 -0.17647059 -0.07563025
##  [31]  0.02521008  0.12605042  0.22689076  0.32773109  0.42857143  0.52941176
##  [37]  0.63025210  0.73109244  0.83193277  0.93277311  1.03361345  1.13445378
##  [43]  1.23529412  1.33613445  1.43697479  1.53781513  1.63865546  1.73949580
##  [49]  1.84033613  1.94117647  2.04201681  2.14285714  2.24369748  2.34453782
##  [55]  2.44537815  2.54621849  2.64705882  2.74789916  2.84873950  2.94957983
##  [61]  3.05042017  3.15126050  3.25210084  3.35294118  3.45378151  3.55462185
##  [67]  3.65546218  3.75630252  3.85714286  3.95798319  4.05882353  4.15966387
##  [73]  4.26050420  4.36134454  4.46218487  4.56302521  4.66386555  4.76470588
##  [79]  4.86554622  4.96638655  5.06722689  5.16806723  5.26890756  5.36974790
##  [85]  5.47058824  5.57142857  5.67226891  5.77310924  5.87394958  5.97478992
##  [91]  6.07563025  6.17647059  6.27731092  6.37815126  6.47899160  6.57983193
##  [97]  6.68067227  6.78151261  6.88235294  6.98319328  7.08403361  7.18487395
## [103]  7.28571429  7.38655462  7.48739496  7.58823529  7.68907563  7.78991597
## [109]  7.89075630  7.99159664  8.09243697  8.19327731  8.29411765  8.39495798
## [115]  8.49579832  8.59663866  8.69747899  8.79831933  8.89915966  9.00000000
lines(lx,dnorm(lx,3,1),col='red')

Kwantyle

qnorm(0.5)
## [1] 0
qnorm(0.75,2,0.23)
## [1] 2.155133
qnorm(0.95,1,2)
## [1] 4.289707

Dystrybuanta

pnorm(0.5)
## [1] 0.6914625

Przedstawienie kilku wykresów gęstości na 1 wykresie

z=seq(-5,5,length=120)
z
##   [1] -5.00000000 -4.91596639 -4.83193277 -4.74789916 -4.66386555 -4.57983193
##   [7] -4.49579832 -4.41176471 -4.32773109 -4.24369748 -4.15966387 -4.07563025
##  [13] -3.99159664 -3.90756303 -3.82352941 -3.73949580 -3.65546218 -3.57142857
##  [19] -3.48739496 -3.40336134 -3.31932773 -3.23529412 -3.15126050 -3.06722689
##  [25] -2.98319328 -2.89915966 -2.81512605 -2.73109244 -2.64705882 -2.56302521
##  [31] -2.47899160 -2.39495798 -2.31092437 -2.22689076 -2.14285714 -2.05882353
##  [37] -1.97478992 -1.89075630 -1.80672269 -1.72268908 -1.63865546 -1.55462185
##  [43] -1.47058824 -1.38655462 -1.30252101 -1.21848739 -1.13445378 -1.05042017
##  [49] -0.96638655 -0.88235294 -0.79831933 -0.71428571 -0.63025210 -0.54621849
##  [55] -0.46218487 -0.37815126 -0.29411765 -0.21008403 -0.12605042 -0.04201681
##  [61]  0.04201681  0.12605042  0.21008403  0.29411765  0.37815126  0.46218487
##  [67]  0.54621849  0.63025210  0.71428571  0.79831933  0.88235294  0.96638655
##  [73]  1.05042017  1.13445378  1.21848739  1.30252101  1.38655462  1.47058824
##  [79]  1.55462185  1.63865546  1.72268908  1.80672269  1.89075630  1.97478992
##  [85]  2.05882353  2.14285714  2.22689076  2.31092437  2.39495798  2.47899160
##  [91]  2.56302521  2.64705882  2.73109244  2.81512605  2.89915966  2.98319328
##  [97]  3.06722689  3.15126050  3.23529412  3.31932773  3.40336134  3.48739496
## [103]  3.57142857  3.65546218  3.73949580  3.82352941  3.90756303  3.99159664
## [109]  4.07563025  4.15966387  4.24369748  4.32773109  4.41176471  4.49579832
## [115]  4.57983193  4.66386555  4.74789916  4.83193277  4.91596639  5.00000000
plot(z,dnorm(z),type="l")
lines(z,dnorm(z,0,3),type="l",lty="dotted")
lines(z,dnorm(z,0,0.5),type="l",lty="dashed")   

# Ostatnia linia wychodzi poza obszar wykresu, ponieważ te linie dorysowywane są do wykresu, 
# czyli nie zmieniają jego rozmiaru. 
# Wykres uzyskany za pomocą funkcji plot() determinuje rozmiar wykresu na stałe. 

z=seq(-10,10,length=120)
plot(z,dnorm(z,0,1),type="l")
lines(z,dnorm(z,2,1),type="l",lty="dotted")
lines(z,dnorm(z,0,2),type="l",lty="dashed")

Dodawanie legendy do wykresu

plot(z,dnorm(z),type="l")
lines(z,dnorm(z,0,3),type="l",lty="dotted")
legend("topright",inset =0.01,lty=c("solid","dotted"),
       c(expression(N(mu==0,sigma==1)),expression(N(mu==0,sigma==3))) )

# Możliwe umiejscowienia legendy:  
# "bottomright"
# "bottom"
# "bottomleft"
# "left"
# "topleft"
# "top"
# "topright"
# "right" 
# "center"

Przykład

# Wygeneruj 100 wartości z rozkładu normalnego o średniej równej -3 i odchyleniu standardowym równym 1.
# Wygeneruj 100 wartości z rozkładu t-Studenta o 8 stopniach swobody.
# Wygeneruj 100 wartości z rozkładu Chikwadrat o 2 stopniach swobody.
# Przedstaw histogramy dla wygenerowanych wartości na 1 wykresie jeden obok drugiego.


x=rnorm(100,-3,1)
y=rt(100,8)
z=rchisq(100,2)
par(mfrow=c(1,3))
hist(x)
hist(y)
hist(z)