SWD kod lab1


# SWD Wykład nr 1, 27 marca 2013
# Wprowadzenie do R-a

> # wektory
> wektory
Error: object 'wektory' not found

# tworzenie wektorow

> # 1. funkcja c()
> waga <- c(56, 76, 85, 48, 90)
> waga
[1] 56 76 85 48 90
> c(56, 76, 85, 48, 90)
[1] 56 76 85 48 90
> waga * 2.204 # waga w funtach
[1] 123.424 167.504 187.340 105.792 198.360
> wzrost <- c(176, 150, 190, 167, )
Error in c(176, 150, 190, 167, ) : argument 5 is empty
> wzrost <- c(176, 150, 190, 167 )
> wzrost[waga > 60] # NA (ang. NotAvaible, brak danych)
[1] 150 190 NA
> waga[1]
[1] 56
> waga[-1]
[1] 76 85 48 90
> waga[2:3]
[1] 76 85
> waga[5] # wartosc wagi dla ostatniej osoby
[1] 90
> length(waga)
[1] 5
> waga[length(waga)] # wartosc wagi dla ostatniej osoby
[1] 90
> waga.funty <- waga * 2.204 # waga w funtach
> (waga.funty <- waga * 2.204) # waga w funtach
[1] 123.424 167.504 187.340 105.792 198.360
> waga.funty <- round(waga.funty, 2)
> waga.funty
[1] 123.42 167.50 187.34 105.79 198.36
> ?round
starting httpd help server ... done
> sum(waga)
[1] 355
> ?sum
> sum(150 190 NA
Error: unexpected numeric constant in "sum(150 190"
> sum(150, 190, NA)
[1] NA
> sum(150, 190, NA, na.rm=TRUE)
[1] 340
> waga
[1] 56 76 85 48 90
> wzrost
[1] 176 150 190 167
> wzrost <- c(wzrost, 150)
> wzrost
[1] 176 150 190 167 150
> length(waga)
[1] 5
> length(wzrost)
[1] 5
> wzrost <- c(150, wzrost)
> wzrost
[1] 150 176 150 190 167 150
> waga
[1] 56 76 85 48 90
> wzrost
[1] 150 176 150 190 167 150
> wzrost <- wzrost[-1]
> wzrost
[1] 176 150 190 167 150
> srednia <- sum(wzrost)/length(wzrost) # sredni wzrost
> srednia
[1] 166.6
> mean(wzrost)
[1] 166.6
> sd(wzrost) # odch.std.
[1] 17.22788
> median(wzrost) # mediana
[1] 167
> plot(waga, wzrost)
> plot(waga, wzrost, pch=25, col=2)
> boxplot(wzrost)
> boxplot(wzrost, col='green3')
> boxplot(wzrost, col='green3', main="To lubie")
> boxplot(wzrost, col='green3', main="To lubie", ylab="wzrost")
> imie <- c("Ania", "Basia", "Olek", "Jas", "Zdzisiek")
> imie
[1] "Ania" "Basia" "Olek" "Jas" "Zdzisiek"
> length(imie)
[1] 5
> imie[3
+ ]
[1] "Olek"
> plec <- c("K", "K", "M", "M", "M")
> imie[plec=="M"]
[1] "Olek" "Jas" "Zdzisiek"
> waga[imie="Basia']

+ > waga[imie="Basia"]
[1] NA
> waga[imie=="Basia"]
[1] 76
> imie=="Basia"
[1] FALSE TRUE FALSE FALSE FALSE
> imie="Basia'
+ "
> imie="Basia"
> waga[10]
[1] NA

> # 2. funkcja seq()
> seq(4,9)
[1] 4 5 6 7 8 9
> seq(7, 3)
[1] 7 6 5 4 3
> ?seq
> seq(4,9, 2)
[1] 4 6 8
> 1:10
[1] 1 2 3 4 5 6 7 8 9 10
> -4 :9
[1] -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9

> # 3. funkcja rep()
> rep(5, 10)
[1] 5 5 5 5 5 5 5 5 5 5
> x <- 1:2
> x
[1] 1 2
> rep(x, 4)
[1] 1 2 1 2 1 2 1 2
> y <- ":-)"
> y
[1] ":-)"
> rep(y, 5)
[1] ":-)" ":-)" ":-)" ":-)" ":-)"
> rep(x, 4)
[1] 1 2 1 2 1 2 1 2
> rep(x, each=4)
[1] 1 1 1 1 2 2 2 2
> rep(x, each=100)
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[29] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[57] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[85] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2
[113] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[141] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[169] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[197] 2 2 2 2
> waga
[1] 56 76 85 48 90
> print(waga)
[1] 56 76 85 48 90
> str(waga)
num [1:5] 56 76 85 48 90
> str(imie)
chr "Basia"
> typeof(waga)
[1] "double"
> typeof(1:7)
[1] "integer"
> typeof(imie)
[1] "character"
>
> imie <- c("Ania", "Basia", "Olek", "Jas", "Zdzisiek")
>
> waga
[1] 56 76 85 48 90
> wzrost
[1] 176 150 190 167 150
> imie
[1] "Ania" "Basia" "Olek" "Jas" "Zdzisiek"
> plec
[1] "K" "K" "M" "M" "M"
> klasa <- data.frame(x1=imie, x2=plec, x3=waga, x4=wzrost)
> klasa
x1 x2 x3 x4
1 Ania K 56 176
2 Basia K 76 150
3 Olek M 85 190
4 Jas M 48 167
5 Zdzisiek M 90 150
> x1
Error: object 'x1' not found
> imie
[1] "Ania" "Basia" "Olek" "Jas" "Zdzisiek"
> klasa$x1
[1] Ania Basia Olek Jas Zdzisiek
Levels: Ania Basia Jas Olek Zdzisiek
> klasa[klasa$x2=="M"]
Error in `[.data.frame`(klasa, klasa$x2 == "M") :
undefined columns selected
> klasa[klasa$x2=="M",]
x1 x2 x3 x4
3 Olek M 85 190
4 Jas M 48 167
5 Zdzisiek M 90 150
> klasa[klasa$x2=="M",klasa$x1]
Error in `[.data.frame`(klasa, klasa$x2 == "M", klasa$x1) :
undefined columns selected
> klasa[klasa$x2=="M",1]
[1] Olek Jas Zdzisiek
Levels: Ania Basia Jas Olek Zdzisiek
> klasa[klasa$x2=="M",1:2]
x1 x2
3 Olek M
4 Jas M
5 Zdzisiek M
> klasa[klasa$x2=="M",c(2,4)]
x2 x4
3 M 190
4 M 167
5 M 150
> buty <- 10:15
> buty
[1] 10 11 12 13 14 15
> klasa <- cbind(klasa, buty)
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 5, 6
> buty <- 10:14
> klasa <- cbind(klasa, buty)
> klasa
x1 x2 x3 x4 buty
1 Ania K 56 176 10
2 Basia K 76 150 11
3 Olek M 85 190 12
4 Jas M 48 167 13
5 Zdzisiek M 90 150 14
> klasa[klasa$buty==max(klasa$buty)]
buty
1 10
2 11
3 12
4 13
5 14
> klasa[klasa$buty==max(klasa$buty),]
x1 x2 x3 x4 buty
5 Zdzisiek M 90 150 14
> klasa$buty==max(klasa$buty)
[1] FALSE FALSE FALSE FALSE TRUE
> klasa[1]
x1
1 Ania
2 Basia
3 Olek
4 Jas
5 Zdzisiek



> # ZADANIA
>
>
>
> # zadanie 1
> x <- seq(-5,5,0.1)
> matplot(x, cbind(dnorm(x,0,1), dnorm(x,1,1), dnorm(x,2,1)))
> matplot(x, cbind(dnorm(x,0,1), dnorm(x,1,1), dnorm(x,2,1)),)
> matplot(x, cbind(dnorm(x,0,1), dnorm(x,1,1), dnorm(x,2,1)),
+ type="l", ylab="gestosc f(x)")
> legend("topleft", legend=c("N(0,1)", "N(1,1)", "N(2,1)"), col=1:3,
+ lty=1:3)
> matplot(x, cbind(pnorm(x,0,1), pnorm(x,1,1), pnorm(x,2,1)),
+ )
> matplot(x, cbind(pnorm(x,0,1, lower.tail=F), pnorm(x,1,1), pnorm(x,2,1))

+ > matplot(x, cbind(pnorm(x,0,1, lower.tail=F),pnorm(x,2,1, lower.tail=F))
+ )
> matplot(x, cbind(dnorm(x,0,1), dnorm(x,0,0.5), dnorm(x,0.2)))
> matplot(x, cbind(dnorm(x,0,1), dnorm(x,0,0.5), dnorm(x,0.2)),type="l")
> matplot(x, cbind(dnorm(x,0,1), dnorm(x,0,0.5), dnorm(x,0,2)),type="l")
>
>
> # zadanie 2
> # a
> qnorm(0.95)
[1] 1.644854
> qnorm(0.95, 0, 1)
[1] 1.644854
> # a & b
> qnorm(c(0.95, 0.975), 0, 1)
[1] 1.644854 1.959964
> qnorm(c(0.95, 0.975))
[1] 1.644854 1.959964
> qt(0.95, 10)
[1] 1.812461
> # kwantyl rozkladu t-Studenta o 10 st. swobody
> qchisq(0.9, 4)
[1] 7.77944
> # kwantyl rozkladu chi-kwadrat o 4st. swobody
> plot(x, dchisq(x,4))
> qf(0.99, 3, 18)
[1] 5.09189
> # kwantyl rozkladu F o 3 i 18 st. swobody
>
>
> # zadanie 3
> # a)
> pnorm(179, 173, 6)
[1] 0.8413447
> # b)
> pnorm(180, 173, 6) - pnorm(167, 173, 6)
[1] 0.7196722
> # c)
> 1-pnorm(181, 173, 6)
[1] 0.09121122
> # d)
> qnorm(0.6, 173, 6)
[1] 174.5201
>
>
>
> # zadanie 4
> x
[1] -5.0 -4.9 -4.8 -4.7 -4.6 -4.5 -4.4 -4.3 -4.2 -4.1 -4.0 -3.9 -3.8
[14] -3.7 -3.6 -3.5 -3.4 -3.3 -3.2 -3.1 -3.0 -2.9 -2.8 -2.7 -2.6 -2.5
[27] -2.4 -2.3 -2.2 -2.1 -2.0 -1.9 -1.8 -1.7 -1.6 -1.5 -1.4 -1.3 -1.2
[40] -1.1 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1
[53] 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4
[66] 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7
[79] 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0
[92] 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0
> matplot(x, cbind(dt(x,1), dt(x,5), dt(x,30), dnorm(x)), type="l")
>
>
> # zadanie 5
> # a)
> dbinom(5,5,0.95)
[1] 0.7737809
> # b)
> dbinom(3,5,0.95)+dbinom(4,5,0.95)+dbinom(5,5,0.95)
[1] 0.9988419
> dbinom(3:5,5,0.95)
[1] 0.02143438 0.20362656 0.77378094
> sum(dbinom(3:5,5,0.95))
[1] 0.9988419
> dbinom(0,5,0.95)
[1] 3.125e-07
>
>
> # zadania 6-8 ew. na przyszlosc
>
> # zadanie 9
> licznosc <- c(17364, 56128, 11239, 8170)
> stan <- c("panny", "mezatki", "wdowy", "rozwodki")
> pie(licznosc, stan)
> procenty <- round((licznosc/sum(licznosc))*100)
> stan <- paste(stan, procenty, "%")
> pie(licznosc, stan)
>
> barplot(licznosc, stan)
Error in width/2 : non-numeric argument to binary operator
In addition: Warning message:
In mean.default(width) : argument is not numeric or logical: returning NA
> barplot(licznosc)
> ?barplot
> barplot(licznosc, names.arg=stan)
> help(barplot)
> require(grDevices)
> require(grDevices) # for colours
> tN <- table(Ni <- stats::rpois(100, lambda=5))
> r <- barplot(tN, col=rainbow(20))
> #- type = "h" plotting *is* 'bar'plot
> lines(r, tN, type='h', col='red', lwd=2)
>
> barplot(tN, space = 1.5, axisnames=FALSE,
+ sub = "barplot(..., space= 1.5, axisnames = FALSE)")
>
>
> # zadanie 10
> dane <- read.csv2("P:/smwd/stacje.csv")
> head(dane)
Answers
1 S
2 S
3 N
4 W
5 W
6 W
> View(dane)
>
> # zliczamy....
> nazwy <- c("N", "E", "W", "S")
> licznoci <- c(sum(dane[,1]=="N"), sum(dane[,1]=="E"),
+ sum(dane[,1]=="W"), sum(dane[,1]=="S"))
> licznosci
Error: object 'licznosci' not found
> licznoci
[1] 160 154 338 348
> pie(licznoci, nazwy)
> save.image("C:\\Documents and Settings\\aolwert\\Desktop\\swd1213lab1")
>


Wyszukiwarka

Podobne podstrony:
kod z WOŚP polecane chomiki by closer9
Lab1 RoboWorks
APD lab1
KOD RAMKI 16
lab1 wprowadzenie
lab1 sprawko
KOD RAMKI 69
lab1(3)
Lab1 PA podstawy PSCAD v2
KOD RAMKI 72
Lab1 1 R3 lab11
kod atrakcyjnosci
kod do czcionek i obrazka
Lab1 1 R1 lab11

więcej podobnych podstron