# Start RStudio # (Session -> New session, Environment -> Clear environment) # Objects y <- 5 # "assignment" (see Environment window) y y = 5 x <- 8 - y x y # Functions sqrt(16) abs(-3) # Data types today <- "Monday" today a <- FALSE a !a # Important data types: vectors and matrices numbers <- c(5, 6, 4, 7, 3) # c for concatenate numbers numbers[2] numbers[3:5] numbers[-4] numbers + 3 numbers # ranges numbers <- 3:8 numbers <- seq(4, 11, 0.3) # repeat numbers <- rep(4, 3) # concatenate numbers <- c(numbers, 6:8) # matrices A <- matrix(1:12, 3, 4) A A[2, ] A[, 3] A[, 2:4] A[3, 4] <- 100 A # Getting help help(matrix) help.search("column sums") colSums(A) help.start() # (also New window -> An Intro to R) # Graphics x <- 0:20 y <- x^2 plot(x, y) # Environment (workspaces) and History # show in RStudio ls() # press Up-key # Can save the workspace (or when quitting) # Scripting # Open new R script # in new file window: a <- 5 print(a) # save file: "example" # in console: source("example.R") # add comment in file: # this prints "5" # Data frames (important and very useful!) # in file: table <- data.frame(letter=c("a", "b", "c"), number=4:6, w=c(3, 5, 1)) # source file # in console: table # vector and matrix computation x <- c(1,2,3,4) #vector assignment (c=concatenate) y<-1:8 #(vector assignment) y*2 y <- 1:8 + 5 y*z #element-wise y*x #x is 'helpful' y>=6 # (y>6 & y<=4) (y>6 & z<=4) sum(y<6) y[y>=6] # sample from normal distribution sample<-rnorm(10) # apply test<-matrix(rnorm(30),ncol=3) #create a matrix from a vector apply(test,2,mean) #apply mean to each column # create functions myfun<-function(x){ y=2*x+3 return(y) } myfun(5) apply(test,1,myfun) #apply own function apply(test,1,function(x) mean(x)-5) #define function directly myfun <- function(x) 2*x+3 # matrix sub-setting again test[1,1] #sub-setting test[,2] #all elements in second column test[,c(2,3)] #all elements in second and third column # packages lillie.test() install.packages('nortest') library(nortest) lillie.test() # more advanced function result<-t.test(y,z) #two sample t.test names(result) result$p.value #extract p.value ?t.test #HELP # for-loop for (i in 1:10){ print(i) }