Common packages R Click here for CRAN package overview by topic
Basic commands and uses R
setwd("C:/Projects/") # Set working directory
getwd() # Print the current working directory
install.packages('package') # Install a package
library(package) # Load a package
?help # Get documentation about requested item
var <- 'text' # store 'text' to a variable named 'var'
number <- 24.6 # double
typeof() # Get type of an object
vector <- vector(34,345,54,46) # data of one type
comb <- c(232,'Hi', 2.23, 'Data') # Combine; creates a vector. In this case the numbers become characters due to character values
list <- list(35,'A', 34) # lists can contain data of different types (recursive)
list[[1]]*2 # list position 1 (which is 35) times 2 --> 70
nlist <- list(a=1, b="two", c=FALSE) # list with names
nlist$b # Retrieves valye of 'b' --> two
nlist[['a']] # Retrieves value of 'a' --> 1 (note: double [[]] to get the inside value e.g. to perform calculations)
install.packages('tidyverse')
library(tidyverse)
Create dataset
a <- c(1,2,3,4)
b <- c('A', 'C', 'Placebo', 'A')
c <- c(TRUE,TRUE,FALSE,TRUE)
d <- c(12, 23, 2, NA)
df <- data.frame(a,b,c,d) # Combine data into a dataframe (length should be the same)
names(df) <- c('ID', 'Drug', 'Active', 'Score') # Set column names
df
View(df) # Show the data in a table view
Piping
Piping is used to improve readability and improve the workflow.
The symbol '%>%' can be read as 'and then'.
mtcars %>% filter(gear > 3) # Select 'mtcars' then filter from column 'gear' all values > 3.
result <- mtcars %>% filter(mpg < 20) # Store the end result into a variable
# Using wrapr package to parse output:
library(wrapr)
mtcars %>% filter(gear > 3) %.>% View(.) # If functions require input, this can be done using wrapr.
mtcars %>% filter(gear > 3) %.>% plot(.$qsec~.$wt) # The previous item is stored as '.' which can be called
Navigate, subset and edit database
There are several ways to navigate through the database and edit items.
PK analysis relies heavily on database quality. A lot of time entails exploring the data, checking for errors, etc.
Filter to (conditionally) select rows.
filter(iris, Petal.Width < 2) # Filter from 'iris' where 'Petal.Width' is < 2)
filter(iris, Species %in% c('setosa','virginica')) # select rows with species setosa and virginica
filter(iris, Petal.Width < 2 & Sepal.Width > 3) # select rows with petal width is <2 and sepal width is >3
Using '%>%': iris %>% filter(Species != 'setosa') # Select iris then filter where species is not setosa
Select or remove columns
db <- select(mtcars, wt) # Select from mtcars the column wt and store in 'db'
db <- mtcars %>% select(-c(am, drat)) # remove from mtcars columns 'am' and 'drat' and store in 'db'
Select unique values (rows)
mtcars %>% distinct(cyl) # Select all unique values from column cyl
mtcars %>% distinct(cyl, .keep_all = T) # With keep all the complete row data are kept
Create a new column from other columns (conditional)
mtcars <- mutate(mtcars, ratio = wt*hp) # in mtcars create new column 'ratio' from 'wt'and 'hp'
mtcars <- mtcars %>% mutate(ratio = wt*hp) # same but with piping
mtcars <- mutate(mtcars, ratio = wt*hp, kwad = ratio^2) # Create multiple colums sequentially. Note order!.
Perform groupwise operations
mtcars %>% group_by(cyl) %>% mutate(Mean = mean(wt)) # Get the mean wt per cyl in new column
Loops and if else statements
If a certain condition equals 'TRUE' do x otherwise do y.
mtcars$new <- if_else(mtcars$wt > 3, 'Y', 'N') # if wt in mtcars > then column new is 'Y' otherwise 'N'
The if else statements can be nested
mtcars$new <- if_else(mtcars$vs =='1','Y', if_else(mtcars$gear == '3','Y','N'))
For loop with if else statements
# In this case for every row (i), in range row 1 to total number of rows, check if cylinders are 4, 6 or more.
for(i in 1:nrow(mtcars)){
if(mtcars$cyl[i] == 4){
print('Four cylinders')
}else if(mtcars$cyl[i] == 6){
print('Six cylinders')
}else{
print('More than 6 cylinders')
}
}
While loop in R
a = 1
while(a < 10){
print(paste(a, 'is smaller than 10'))
a = a + 1
}