Data

telo.control <- c(0.95, 0.96, 1.07, 1.08, 1.09, 
             1.1,  1.12, 1.14, 1.18, 1.19, 
             1.19, 1.2, 1.34, 1.45,  1.49, 
             1.55)

telo.disturbed <- c(0.83, 0.84, 0.87, 0.93, 0.94, 
               0.96, 0.99, 1.00, 1.01, 1.01, 
               1.02, 1.05, 1.07, 1.08, 1.09,
               1.09, 1.12, 1.13, 1.16, 1.24, 
               1.41)
               
cort.control <-c(6.32, 1.56,  8.61, 4.86, 3.32, 
                4.31,  1.84, 10.45, 1.47, 4.57, 
                1.95,  5.93,  8.89, 1.16,   NA,
                  NA)

cort.disturbed <- c(7.38,  5.39, 5.39, 5.73, 18.35, 
                    0.95,  1.72, 7.26, 1.38,  4.22, 
                    7.52, 12.52, 3.86, 3.12,  1.36, 
                    2.26,  0.85, 4.76, 0.95,  6.92, 
                    2.16)
sex.cntrl <- c("F","M","F","F","F","F","F","M","F","F","M","F","M","M",
               "F","F")       
               
sex.dist <-  c("M", "M", "F", "F", "F", "M", "F", "M", "F", "M", "F",
                "F", "M", "F", "M", "M", "F", "M", "F", "M", "M")

Getting to Know my Data (## refers to a section title)

# what type of data, what type of data structure
is(telo.control)
## [1] "numeric" "vector"
is.vector(telo.control)
## [1] TRUE

RMarkdown (acts like wordprocessor)

hist(telo.disturbed)

## R as a calculator

mean(telo.disturbed)
## [1] 1.04
median(telo.control)
## [1] 1.16
max(telo.control)
## [1] 1.55
min(telo.disturbed)
## [1] 0.83
summary(telo.disturbed)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.83    0.96    1.02    1.04    1.09    1.41

R for plots

hist(telo.control)

boxplot(telo.control)

Making dataframes

The c() operator concatonates vectors (adds two vectors)

telomeres <- c(telo.control, telo.disturbed)
length(telo.control)
## [1] 16
length(telo.disturbed)
## [1] 21
length(telo.control)+length(telo.disturbed)
## [1] 37
length(telomeres)
## [1] 37
is(telomeres)
## [1] "numeric" "vector"
is.vector(telomeres)
## [1] TRUE
is.data.frame(telomeres)
## [1] FALSE

Check for items in memory using ls command

ls()
## [1] "cort.control"   "cort.disturbed" "sex.cntrl"      "sex.dist"      
## [5] "telo.control"   "telo.disturbed" "telomeres"
sex.cntrl
##  [1] "F" "M" "F" "F" "F" "F" "F" "M" "F" "F" "M" "F" "M" "M" "F" "F"
sex.dist
##  [1] "M" "M" "F" "F" "F" "M" "F" "M" "F" "M" "F" "F" "M" "F" "M" "M" "F" "M" "F"
## [20] "M" "M"
is(sex.cntrl)
## [1] "character"           "vector"              "data.frameRowLabels"
## [4] "SuperClassMethod"
sex <- c(sex.cntrl, sex.dist)
sex
##  [1] "F" "M" "F" "F" "F" "F" "F" "M" "F" "F" "M" "F" "M" "M" "F" "F" "M" "M" "F"
## [20] "F" "F" "M" "F" "M" "F" "M" "F" "F" "M" "F" "M" "M" "F" "M" "F" "M" "M"
length(telomeres)
## [1] 37
length(sex)
## [1] 37
length(telomeres)==length(sex)
## [1] TRUE

Data frames (gathers data and displays in a data tables)

telomeres
##  [1] 0.95 0.96 1.07 1.08 1.09 1.10 1.12 1.14 1.18 1.19 1.19 1.20 1.34 1.45 1.49
## [16] 1.55 0.83 0.84 0.87 0.93 0.94 0.96 0.99 1.00 1.01 1.01 1.02 1.05 1.07 1.08
## [31] 1.09 1.09 1.12 1.13 1.16 1.24 1.41
sex
##  [1] "F" "M" "F" "F" "F" "F" "F" "M" "F" "F" "M" "F" "M" "M" "F" "F" "M" "M" "F"
## [20] "F" "F" "M" "F" "M" "F" "M" "F" "F" "M" "F" "M" "M" "F" "M" "F" "M" "M"
df <- data.frame(telomeres, #telomere data vector
           sex)       #sex (male/female) vector

is(df)
## [1] "data.frame" "list"       "oldClass"   "vector"
is.data.frame(df)
## [1] TRUE
dim(df) #dimensions of dataframe and matrices (can't call it on vectors)
## [1] 37  2
#dim(telomeres) this is not possible

head(df) #shows top 6 rows of data table
##   telomeres sex
## 1      0.95   F
## 2      0.96   M
## 3      1.07   F
## 4      1.08   F
## 5      1.09   F
## 6      1.10   F
tail(df) ##shows bottom 6 rows of data table
##    telomeres sex
## 32      1.09   M
## 33      1.12   F
## 34      1.13   M
## 35      1.16   F
## 36      1.24   M
## 37      1.41   M

Boxplots

#       y variable~x variable, data=data table
boxplot(telomeres~sex,data = df)

rep command (replicate/repeat)

trt.c <- rep("C", 16)
trt.d <- rep("D", 21)
 
trt.c
##  [1] "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C" "C"
trt.d
##  [1] "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D" "D"
## [20] "D" "D"