Creating sequences

s4 <- seq(length=10, from=0, by=1)
s4
##  [1] 0 1 2 3 4 5 6 7 8 9
s4 <- seq(length=51, from=-5, by=.2)
s4
##  [1] -5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2
## [16] -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2  0.0  0.2  0.4  0.6  0.8
## [31]  1.0  1.2  1.4  1.6  1.8  2.0  2.2  2.4  2.6  2.8  3.0  3.2  3.4  3.6  3.8
## [46]  4.0  4.2  4.4  4.6  4.8  5.0

Repeating items

rep(3, time=2)
## [1] 3 3
x <- c('a', '10')
s5 <- rep(x, times=5)
s5
##  [1] "a"  "10" "a"  "10" "a"  "10" "a"  "10" "a"  "10"
s6 <- rep(x, each=5)
s6
##  [1] "a"  "a"  "a"  "a"  "a"  "10" "10" "10" "10" "10"

Handling missing data

1st kind of missing data: ‘Not Available’ or ‘missing value’

z <- c(1:3, NA)
z
## [1]  1  2  3 NA
ind <- is.na(z)
ind
## [1] FALSE FALSE FALSE  TRUE

2nd kind of missing data is produced by numerical computation: Not a Number, NaN

0/0
## [1] NaN
Inf - Inf
## [1] NaN

In summary, is.na(xx) is TRUE both for NA and NaN values. To differentiate these, is.nan(xx) is only TRUE for NaNs.

Subsetting data frames

Working with a built-in dataset, mtcars***

mtcars
##                      mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
## Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
## Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
## Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
## Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
## Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
## Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
## Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
## Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
## Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
## Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
## Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
## AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
## Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
## Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2
## Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
## Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
## Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
## Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
mtcars[10, 4]
## [1] 123
mtcars['Merc 280', 'hp']
## [1] 123
mtcars["Merc 280", ]
##           mpg cyl  disp  hp drat   wt qsec vs am gear carb
## Merc 280 19.2   6 167.6 123 3.92 3.44 18.3  1  0    4    4
mtcars[["hp"]]
##  [1] 110 110  93 110 175 105 245  62  95 123 123 180 180 180 205 215 230  66  52
## [20]  65  97 150 150 245 175  66  91 113 264 175 335 109
mtcars$hp
##  [1] 110 110  93 110 175 105 245  62  95 123 123 180 180 180 205 215 230  66  52
## [20]  65  97 150 150 245 175  66  91 113 264 175 335 109
nrow(mtcars)
## [1] 32
ncol(mtcars)
## [1] 11
mtcars[, c("mpg", "hp")]
##                      mpg  hp
## Mazda RX4           21.0 110
## Mazda RX4 Wag       21.0 110
## Datsun 710          22.8  93
## Hornet 4 Drive      21.4 110
## Hornet Sportabout   18.7 175
## Valiant             18.1 105
## Duster 360          14.3 245
## Merc 240D           24.4  62
## Merc 230            22.8  95
## Merc 280            19.2 123
## Merc 280C           17.8 123
## Merc 450SE          16.4 180
## Merc 450SL          17.3 180
## Merc 450SLC         15.2 180
## Cadillac Fleetwood  10.4 205
## Lincoln Continental 10.4 215
## Chrysler Imperial   14.7 230
## Fiat 128            32.4  66
## Honda Civic         30.4  52
## Toyota Corolla      33.9  65
## Toyota Corona       21.5  97
## Dodge Challenger    15.5 150
## AMC Javelin         15.2 150
## Camaro Z28          13.3 245
## Pontiac Firebird    19.2 175
## Fiat X1-9           27.3  66
## Porsche 914-2       26.0  91
## Lotus Europa        30.4 113
## Ford Pantera L      15.8 264
## Ferrari Dino        19.7 175
## Maserati Bora       15.0 335
## Volvo 142E          21.4 109

Using which() function to subset data frame. The which() function in R returns the position or the index of the value which satisfies the given condition.

#subset data frame based on a value threshold of certain column
#only print cars that are higher than 20 mpg
mtcars[which(mtcars$mpg > 20), ]
##                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4      21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710     22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
## Merc 240D      24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
## Merc 230       22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
## Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Toyota Corona  21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
## Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Volvo 142E     21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

Another dataset ToothGrowth

The tooth growth data set is the length of the odontoblasts (teeth) in each of 10 guinea pigs at three Vitamin C dosage levels (0.5, 1, and 2 mg) with two delivery methods (orange juice or ascorbic acid).

data(ToothGrowth)
#To view top 6 rows of the data
head(ToothGrowth)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5

The data contains 60 observations of 3 variables

#To simplify the variable name
tg <- ToothGrowth

More basic R functions

#dimensions of the data
dim(tg) # returns number of rows and colums
## [1] 60  3
nrow(tg)# returns number of rows
## [1] 60
ncol(tg)# returns number of colums
## [1] 3
#Summary statistics
summary(tg)
##       len        supp         dose      
##  Min.   : 4.20   OJ:30   Min.   :0.500  
##  1st Qu.:13.07   VC:30   1st Qu.:0.500  
##  Median :19.25           Median :1.000  
##  Mean   :18.81           Mean   :1.167  
##  3rd Qu.:25.27           3rd Qu.:2.000  
##  Max.   :33.90           Max.   :2.000

Basic statistical functions

#Average of all given values
mean(tg$len)
## [1] 18.81333
#Standard deviation of all given values
sd(tg$len)
## [1] 7.649315

Basic plotting to observe the trend

plot(tg[ ,"dose"], tg[, "len"])

#or plot(tg$dose, tg$len)

Welcome to tidyverse

For more practises: https://m-clark.github.io/data-processing-and-visualization/tidyverse.html

Load tidyverse library

library(tidyverse)
tg %>% head()
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5
tg %>% dim()
## [1] 60  3
tg %>% select(dose) %>% head()
##   dose
## 1  0.5
## 2  0.5
## 3  0.5
## 4  0.5
## 5  0.5
## 6  0.5
tg %>% select(len) %>% head()
##    len
## 1  4.2
## 2 11.5
## 3  7.3
## 4  5.8
## 5  6.4
## 6 10.0
tg %>% filter(supp == "VC")
##     len supp dose
## 1   4.2   VC  0.5
## 2  11.5   VC  0.5
## 3   7.3   VC  0.5
## 4   5.8   VC  0.5
## 5   6.4   VC  0.5
## 6  10.0   VC  0.5
## 7  11.2   VC  0.5
## 8  11.2   VC  0.5
## 9   5.2   VC  0.5
## 10  7.0   VC  0.5
## 11 16.5   VC  1.0
## 12 16.5   VC  1.0
## 13 15.2   VC  1.0
## 14 17.3   VC  1.0
## 15 22.5   VC  1.0
## 16 17.3   VC  1.0
## 17 13.6   VC  1.0
## 18 14.5   VC  1.0
## 19 18.8   VC  1.0
## 20 15.5   VC  1.0
## 21 23.6   VC  2.0
## 22 18.5   VC  2.0
## 23 33.9   VC  2.0
## 24 25.5   VC  2.0
## 25 26.4   VC  2.0
## 26 32.5   VC  2.0
## 27 26.7   VC  2.0
## 28 21.5   VC  2.0
## 29 23.3   VC  2.0
## 30 29.5   VC  2.0
tg %>% filter(supp == "VC") %>% filter(dose == 2.0)
##     len supp dose
## 1  23.6   VC    2
## 2  18.5   VC    2
## 3  33.9   VC    2
## 4  25.5   VC    2
## 5  26.4   VC    2
## 6  32.5   VC    2
## 7  26.7   VC    2
## 8  21.5   VC    2
## 9  23.3   VC    2
## 10 29.5   VC    2
# Summarize by dose and supp, the mean length of growth.
tg %>% 
    group_by(supp, dose) %>%
    summarize(lenmean=mean(len), lensd=sd(len), count = n())
## `summarise()` has grouped output by 'supp'. You can override using the
## `.groups` argument.
## # A tibble: 6 × 5
## # Groups:   supp [2]
##   supp   dose lenmean lensd count
##   <fct> <dbl>   <dbl> <dbl> <int>
## 1 OJ      0.5   13.2   4.46    10
## 2 OJ      1     22.7   3.91    10
## 3 OJ      2     26.1   2.66    10
## 4 VC      0.5    7.98  2.75    10
## 5 VC      1     16.8   2.52    10
## 6 VC      2     26.1   4.80    10
# Summarize by supp only.
tg %>% 
    group_by(supp) %>%
    summarize(lenmean=mean(len), lensd=sd(len), count = n())
## # A tibble: 2 × 4
##   supp  lenmean lensd count
##   <fct>   <dbl> <dbl> <int>
## 1 OJ       20.7  6.61    30
## 2 VC       17.0  8.27    30
# Summarize by dose only.
tg %>% 
    group_by(dose) %>%
    summarize(lenmean=mean(len), lensd=sd(len), count = n())
## # A tibble: 3 × 4
##    dose lenmean lensd count
##   <dbl>   <dbl> <dbl> <int>
## 1   0.5    10.6  4.50    20
## 2   1      19.7  4.42    20
## 3   2      26.1  3.77    20

Visualization with ggplot

Best practices with ggplot: http://www.sthda.com/english/wiki/be-awesome-in-ggplot2-a-practical-guide-to-be-highly-effective-r-software-and-data-visualization

# Plot the length (y) by the dosage (x)
g <- ggplot(tg, aes(x= dose, y= len)) +
    geom_point(aes(color=supp))
print(g)

More built-in datasets for future practises

/ ***

DNase Elisa assay of DNase

Orange Growth of Orange Trees

PlantGrowth Results from an Experiment on Plant Growth

Puromycin Reaction Velocity of an Enzymatic Reaction

Theoph Pharmacokinetics of Theophylline

ToothGrowth The Effect of Vitamin C on Tooth Growth in Guinea Pigs

esoph Smoking, Alcohol and (O)esophageal Cancer

fdeaths (UKLungDeaths) Monthly Deaths from Lung Diseases in the UK

iris Edgar Anderson’s Iris Data

sleep Student’s Sleep Data

trees Diameter, Height and Volume for Black Cherry Trees