--- title: "R - podstawy (2020-03-05)" output: html_notebook --- This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*. ```{r} plot(cars) print(cars) ``` Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*. When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file). The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed. ```{r} cat("Hello, World!\nWitaj Świecie!") #typy proste zmienna1 <- 1L #obiekt nazwany (zmienna) zmienna2 <- 3.0 typeof(zmienna1) typeof(zmienna2) mode(zmienna1) mode(zmienna2) zmienna3 <- "4.0" typeof(zmienna3); mode(zmienna3) (zmienna4 <- 1) #wymusza wydruk zmienna1 str(zmienna3) wektor <- c(0,1,2,3,4,5,6,7,8,9) print(wektor) typeof(wektor) mode(wektor) length(wektor) "print"(wektor) wektor wektor[1] #indeksujemy od 1, a nie od 0 "["(wektor,10) #ciekawostka wektor[c(1,4,10,11)] wektor[3] <- 5 wektor wektor[c(4,5,6)] <- 7 wektor wektor[4:6] <- 0 wektor wektor[4:6] <- c(-1,-2,-3) wektor str(wektor) min(wektor) max(wektor) mean(wektor) max(wektor+2) wektor;wektor+2 max(wektor^2) sum(wektor) wektor3d <- c(1,1,0) sqrt(sum(wektor3d^2)) wektor3dprim <- c(-1,1,0) wektor3d wektor3dprim sum(wektor3d*wektor3dprim) #iloczyn skalarny wektor3d <- c(1,2,3) prod(wektor3d[]) wektor1 <- 0:9 str(wektor1) wektor2 <- seq(0,9,2) str(wektor2) wektor2 <- seq(from = -1, to = 9, by = 2) str(wektor2) wektor2 <- seq(0,9,length.out = 11) str(wektor2) wektor3 <- rep(1,10) str(wektor3) wektor4 <- c(1L, 1.0, 1E1) wektor4 typeof(wektor4) mode(wektor4) is.numeric(wektor4) wektor4 <- c(1L, 1.0, 1E1, "1") wektor4 typeof(wektor4) mode(wektor4) is.numeric(wektor4) as.numeric(wektor4) is.numeric(wektor4) wektor4 <- as.numeric(wektor4) is.numeric(wektor4) funkcja.kwadrat <- function(argument) { result <- argument * argument #result - zmienna lokalna return(result) } funkcja.kwadrat(2) funkcja.kwadrat(c(2,5)) funkcja.kwadrat(0:9) print(funkcja.kwadrat) str(funkcja.kwadrat) funkcja.kwadrat <- function(argument) argument^2 funkcja.kwadrat(2) funkcja.kwadrat(c(2,5)) funkcja.kwadrat(0:9) print(funkcja.kwadrat) str(funkcja.kwadrat) funkcja.kwadrat ~ argument^2 #formuła funkcja.kwadrat(2) funkcja.kwadrat(c(2,5)) funkcja.kwadrat(0:9) print(funkcja.kwadrat) str(funkcja.kwadrat) wektor <- 1:10 wektor wynik <- funkcja.kwadrat(wektor) wynik plot(wektor, wynik, type="b") zmienna <- 1 #R-owcy to preferują zmienna = 2 #R-owcy tego nie lubią 3 -> zmienna paste("jeden", "dwa", "trzy") paste("jeden", "dwa", "trzy", sep = "") paste("jeden", "dwa", "trzy", sep = ", ") paste(c("jeden", "dwa", "trzy"), collapse = ",") paste(c("jeden", "dwa", "trzy"), collapse = ",") s <- paste(c("jeden", "dwa", "trzy"), collapse = ",") print(s) cat(s) cat("jeden", "dwa", "trzy") cat("jeden", "dwa", "trzy", sep=", ") strsplit(s,",") ``` ```{r} 2+3 cat(2+3) 2+3;1+2 2-3 2*3 2/3 2L/3L 2%/%3 #dzielenie całkowite 2%%3 2^3 2**3 #to samo co 2^3 2==3 typeof(2==3) mode(2==3) str(2==3) 2==3-1 2!=3 2!=3-1 2+2*2 (2+2)*2 {2+2}*2 pi pi-3.141593 2*pi sin(1) #funkcje trygonometryczne przyjmują argumenty w radianach sin(pi) sin(pi/2) sin(90) deg2rad <- function(deg) { result <- deg*2*pi/360 return(result) } deg2rad(180) sin(deg2rad(90)) ```