###-------------------------### ### BRM Session 1 - R Intro ### ###-------------------------### # Dennis Abel and Lukas Birkenmaier # Intro ------------------------------------------------------------------- # Based on: # 1. Wickham, H., Cetinkaya-Rundel, M., Grolemund, G. (2023): R for Data Science (https://r4ds.hadley.nz/); # 2. Field et al. (2012): Discovering Statistics Using R. (https://studysites.uk.sagepub.com/dsur/study/default.htm) # 3. https://cran.r-project.org/doc/contrib/Torfs+Brauer-Short-R-Intro.pdf # Describe the user interface # bottom left -> console/command window # top left -> editor/script window # top right -> workspace/history window # bottom right -> files/plots/packages/help window # Packages ---------------------------------------------------------------- # Normally, we load all relevant packages at the beginning of the script #install.packages("tidyverse") library(tidyverse) # We also set the working directory in the beginning setwd("[insert directory here]") # Understanding R --------------------------------------------------------- # Coding basics in R # Using R as a calculator 1 / 200 * 30 (59 + 73 + 2) / 3 sin(pi / 2) 3^3 #three to the power of three # Creating a variable (scalar) a <- 4 # same thing a=4 # a a*5 a <- a*10 # Remove a variable rm(a) # Clear all workspace rm(list=ls()) # Scalars (0d), vectors/arrays (1d) and matrices (2d) # Vectors b <- c(3,4,5) # Standard functions in R # Definition of 'arguments' between the brackets mean(x=b) ?mean #same thing mean(b) # Creation of new objects # R is an object-oriented programming language x <- 3 * 4 x # character objects my_name <- "Dennis" my_name # ls() to see what objects are currently stored in the workspace ls() # Shortcut for arrow: Alt + "-" # Calling a simple function y <- seq(1, 10) # Check out vignettes to understand functions ?seq # Repeats a certain number x times rep(8, 15) # repeats the number "8" 15 times. ############################################################################## # Exercise 1: Create an object "b" as a sequence of 100 numbers and remove # it afterwards from the workspace. # ############################################################################## # Data frames ------------------------------------------------------------- # Important statistical functions like regression usually use dataframes # Remember from above weight<-c(60,72,57,90,95,72) height<-c(1.75,1.8,1.65,1.9,1.74,1.91) # turn these vectors into a matrix / table by combining the two vectors column-wise a <- cbind(weight,height) a class(a) # convert this matrix into a data frame so we can work with it a1 <- as.data.frame(a) a1 class(a1) # We can subset and retrieve a column with '$'; this works only for dataframes a1$weight a1$height # Basic functions for summary statistics ---------------------------------- # Creating two vectors first weight<-c(60,72,57,90,95,72) height<-c(1.75,1.8,1.65,1.9,1.74,1.91) #Calculating the mean (average) of weight sum(weight) #sum of all elements length(weight) #counts the elements sum(weight)/length(weight) #gives the average # Easier way to do that mean(weight) #Finding minimum and maximum in an object max(weight) # maximum element in weight min(weight) # minimum element sort(weight) # sorts a from lowest to highest weight_sorted <- sort(weight) weight_count <- length(weight) weight_count ############################################################################# # Exercise 2: # Calculate the mean of height ############################################################################# # Missing data ------------------------------------------------------------ j <- c(1,2,NA) j # a lot of functions are not working with missing values. max(j) # removing missing values max(j, na.rm=TRUE) # Classes(numeric, charater, POSIX(date-time)) class(j)