# FC721 Introduction to R Programming # Part1: Setting up R and RStudio #----------------------------------- # Check current directory getwd() # Set working directory setwd("/Users/ktrn/BOSTON UNIVERSITY Dropbox/Katia Bulekova/Classes/FC721") # Install packages install.packages("tidyverse") # Alternatively you can install R packages using RStudio GUI # Tools -> Install Packages # Type in the package name and click Install # Load packages library(tidyverse) # Note 1: you have to use quotes with install.packages() but not with library() # Note 2: you need to install a package only once, but you need to load it every time you start a new R session # Note 3: load the packages you need at the beginning of your script #----------------------------------- # Basic R data types #----------------------------------- # Numeric x <- 5 class(x) # Character y <- "Hello, World!" class(y) # Values of the same data types can be combined in a vector: z <- c(1, 21, 35, 74, 5) class(z) # systolic blood pressure values SBP <- c(96, 110, 100, 125, 90 ) # diastolic blood pressure DBP <- c(55, 70, 68, 100, 50) # calculate MAP (mean arterial pressure) MAP <- SBP/3 + 2*DBP/3 MAP # vector functions: # max(x), min(x), sum(x), prod(), # mean(x), sd(), median(x), range(x) # sort(x), rank(x), order(x) # cumsum(), cumprod(x), cummin(x), cummax(x) # var(x) - simple variance # cor(x,y) - correlation between x and y # duplicated(x), unique(x) # summary() # Vector slicing: MAP[2] # returns second element MAP[2:4] # returns second through 4th elements inclusive MAP[c(1,3,5)] # returns 1st, 3rd and 5th elements #----------------------------------- # Getting R help #----------------------------------- # If you need help with a specific function, you can use the help() function: help(mean) # You can also use the question mark symbol ? to get help: ?mean # If you are looking for a topic but do not know the function name, you can use double question marks ?? ??"standard deviation"