• Home
  • University of Lincoln
  • Data Skills for Life Sciences
  • Introduction to R Programming for Data Analysis

Introduction to R Programming for Data Analysis

Workbook 16 October 2020 16:54 Chapter 1 (Random) = object We assign values to objects using the backwards arrow <-. o Mean = mean(random) o Standard deviation = sd(random) o Median = median(random) Chapter 2 There are 6 key components to Rcode: - Objects - Assignment - Functions - Arguments - Outputs - Comments Objects - Objects are packets of data saved in the R that you refer back to. - Objects are created using functions. Simple functions include + - * / - The function seq() means "sequence". Object <- Function Example: numbers <- seq(1, 10, 1) = [1] 1 2 3 4 5 6 7 8 9 10 - Summary(numbers) - Length(numbers) - Mean(numbers) To create new objects from old, we rename the origin object and change the arguments of seq > Example: numbers2 <- seq(2, 20, 2) numbers2 rnorm() = creates random numbers Classes These objects have different classes, which define what the object is and how R works with it. There are 5 different class types that it's important to know at this point. o Numeric - the function class() o Character - the function c() - Character variables should have quote marks " " - They are unique information for each data row or ' around each data point, otherwise R will interpret them as factors. o Factor - to make a factor we must convert a character vector to a factor using as.factor() and overwrite the original factor object by reassigning it. - Shared information across rows o Logical operators - == exactly equal to - != not equal to - == tells you whether the two objects on the left and the right are exactly equal to each other: - %in% tells you which elements of one vector belong to another vector. - >= greater than or equal to o Data.frame - data.frames are extremely important objects. This is the way most data is stored for data analysis. - data frames are two-dimensional: rows and columns. The function dim() shows you how many dimensions: · First, rows · Second, columns " dataframe[rows, columns] read.csv() to read the data frame Chapter 3 - One variable per column. - One observation (also sometimes called replicate / data point) per row. - Each column is a variable. Each row represents a single observation. That is, one number of a numeric variable, or one category of a factor variable. Simple Plots - we use ggplot() o Histograms can be produced using the hist() function. o Box plots can be produced using the boxplot() function. - geom_boxplot() we load up these packages is using library() o The x and y variables are defined using aes() o The coordinates are altered in a very simple way by changing the v and v avie limite neina vlim(\ and slim() ? - geom_boxplot() o Bar graphs can be produced using the barplot() function. o Scatter plots can be produced using the plot() function - geom_point() = scatter plot o ine x