Zhiguang Huo (Caleb)
Mon August 21, 2017
typeof(1.1)## [1] "double"
typeof("a")## [1] "character"
typeof(TRUE)## [1] "logical"
typeof(4L)## [1] "integer"
typeof(4)## [1] "double"
if(T){
print(TRUE)
}## [1] TRUE
typeof(FALSE)## [1] "logical"
is.logical(T)## [1] TRUE
aseq <- 1:10 ## <- represent assign value
typeof(aseq)## [1] "integer"
aint = 6L
is.integer(aint)## [1] TRUE
is.integer(6)## [1] FALSE
a = 1.6; print(a) ## assign 1.6 to a ## [1] 1.6
b <- 1.6; print(b) ## assign 1.6 to b## [1] 1.6
1.6 -> d; print(d) ## assign 1.6 to d## [1] 1.6
Difference between = and <-
typeof(1.4142)## [1] "double"
is.double(pi)## [1] TRUE
is.double(0L)## [1] FALSE
is.double(0L + 1.5)## [1] TRUE
acharacter <- "I like Biostatistical computing"
typeof(acharacter)## [1] "character"
bcharacter <- 'You like Biostatistical computing'
is.character(bcharacter)## [1] TRUE
ccharacter <- "He doesn't like Biostatistical computing"
print(ccharacter)## [1] "He doesn't like Biostatistical computing"
| Functions | Meaning |
|---|---|
| length(x) | Number of elements in x |
| unique(x) | Unique elements of x |
| sort(x) | Sort the elements of x |
| rev(x) | Reverse the order of x |
| names(x) | Name the elements of x |
| which(x) | Indices of x that are TRUE |
| which.max(x) | Index of the maximum element of x |
| which.min(x) | Index of the minimum element of x |
| append(x) | Insert elements into a vector |
| match(x) | First index of an element in a vector |
| union(x, y) | Union of x and y |
| intersect(x, y) | Intersection of x and y |
| setdiff(x, y) | Elements of x that are not in y |
| setequal(x, y) | Do x and y contain the same elements |
avec <- c(5,2,9,3)
length(avec)## [1] 4
sort(avec)## [1] 2 3 5 9
rev(avec)## [1] 3 9 2 5
| Functions | Meaning |
|---|---|
| sum(x) | Sum of x |
| prod(x) | Product of x |
| cumsum(x) | Cumulative sum of x |
| cumprod(x) | Cumulative product of x |
| min(x) | Minimum element of x |
| max(x) | Maximum element of x |
| pmin(x, y) | Pairwise minimum of x and y |
| pmax(x, y) | Pairwise maximum of x and y |
| mean(x) | Mean of x |
| median(x) | Median of x |
| var(x) | Variance of x |
| sd(x) | Standard deviation of x |
| cov(x, y) | Covariance of x and y |
| cor(x, y) | Correlation of x and y |
| range(x) | Range of x |
| quantile(x) | Quantiles of x for given probabilities |
| summary(x) | Numerical summary of x |
avec <- c(5,2,9,3)
max(avec)## [1] 9
which.max(avec)## [1] 3
mean(avec)## [1] 4.75
range(avec)## [1] 2 9
typeof(c("a", 1))## [1] "character"
x <- c(FALSE, FALSE, TRUE)
as.numeric(x)## [1] 0 0 1
as.character(x)## [1] "FALSE" "FALSE" "TRUE"
typeof(c(1.2,1L))## [1] "double"
typeof(NA)## [1] "logical"
typeof(NA_integer_)## [1] "integer"
typeof(NA_real_)## [1] "double"
typeof(NA_character_)## [1] "character"
x <- list(1:3, "a", c(TRUE, FALSE, TRUE), list(2.3, 5.9))
str(x)## List of 4
## $ : int [1:3] 1 2 3
## $ : chr "a"
## $ : logi [1:3] TRUE FALSE TRUE
## $ :List of 2
## ..$ : num 2.3
## ..$ : num 5.9
| Homogeneous | Heterogeneous | |
|---|---|---|
| 1d | Atomic vector | List |
| 2d | Matrix | Data frame |
| nd | Array |
sentenses <- "R is a great statistical software.\n\nWe use R in Biostatistical computing class!"
sentenses## [1] "R is a great statistical software.\n\nWe use R in Biostatistical computing class!"
cat(sentenses)## R is a great statistical software.
##
## We use R in Biostatistical computing class!
achar <- "this is a dog."
print(achar)## [1] "this is a dog."
print(toupper(achar))## [1] "THIS IS A DOG."
print(tolower("WWW.UFL.EDU"))## [1] "www.ufl.edu"
achar <- "this is a dog."
nchar(achar)## [1] 14
length(achar)## [1] 1
chars <- c("a dog", "a cat", "a gator")
nchar(chars)## [1] 5 5 7
length(chars)## [1] 3
chars <- "this is a dog"
substring(chars,1,1)## [1] "t"
substring(chars,11,13)## [1] "dog"
substring(chars,11,13) <- "cat"
print(chars)## [1] "this is a cat"
strsplit("this is a dog", split=" ")## [[1]]
## [1] "this" "is" "a" "dog"
strsplit("this is a dog", split="")## [[1]]
## [1] "t" "h" "i" "s" " " "i" "s" " " "a" " " "d" "o" "g"
strsplit(c("this is a dog", "this is a cat", "this is a gator"), split=" ")## [[1]]
## [1] "this" "is" "a" "dog"
##
## [[2]]
## [1] "this" "is" "a" "cat"
##
## [[3]]
## [1] "this" "is" "a" "gator"
paste('this','is','a','dog', sep=" ")## [1] "this is a dog"
paste0('this','is','a','dog')## [1] "thisisadog"
avec <- c('this','is','a','dog')
nchar(avec)## [1] 4 2 1 3
paste(c('this','is','a','dog'), collapse = " ")## [1] "this is a dog"
achar <- "this is a dog"
gsub("dog","cat",achar) ## pattern, replacement, x## [1] "this is a cat"
gsub(pattern = "dog",replacement="cat",x=achar) ## pattern, replacement, x## [1] "this is a cat"
chars <- c("this is a dog", "this is a cat", "this is a gator")
gsub("this","that",chars) ## pattern, replacement, x## [1] "that is a dog" "that is a cat" "that is a gator"
chars <- c("this is a dog", "this is a cat", "this is a gator")
grep("gator", chars)## [1] 3
grep("this", chars)## [1] 1 2 3
chars <- c("this is a dog", "this is a cat", "this is a gator")
grep("dog|cat", chars)## [1] 1 2
chars <- c("this is a dog", "this is a cat", "this is a gator")
grep("[bced]", chars)## [1] 1 2
chars <- c("this is a dog", "this is a cat", "this is a gator")
grep("[b-d]", chars)## [1] 1 2
grep("[0-9]", chars)## integer(0)
for(i in 1:10){
cat(i," ")
}## 1 2 3 4 5 6 7 8 9 10
i <- 1
while(i <= 10){
cat(i," ")
i <- i + 1
}## 1 2 3 4 5 6 7 8 9 10
y <- 1:10
attr(y, "my_attribute") <- "This is a vector"
attr(y, "my_attribute")## [1] "This is a vector"
attributes(y)## $my_attribute
## [1] "This is a vector"
y <- c(a=1,2:10)
names(y)## [1] "a" "" "" "" "" "" "" "" "" ""
names(y)[2] <- 'b'
dim(y)## NULL
dim(y) <- c(2,5)
print(y)## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
class(y)## [1] "matrix"
x <- factor(c("a", "b", "b", 'a'))
x## [1] a b b a
## Levels: a b
class(x)## [1] "factor"
levels(x)## [1] "a" "b"
factors are very useful when there exist missing class
sex_char <- c("m", "m", "m")
sex_factor <- factor(sex_char, levels=c("m", "f"))
table(sex_char)## sex_char
## m
## 3
table(sex_factor)## sex_factor
## m f
## 3 0
a <- matrix(1:6, ncol=3, nrow=2, dimnames = list(c("row1", "row2"),
c("C.1", "C.2", "C.3")))
a## C.1 C.2 C.3
## row1 1 3 5
## row2 2 4 6
colnames(a)## [1] "C.1" "C.2" "C.3"
rownames(a)## [1] "row1" "row2"
ncol(a)## [1] 3
nrow(a)## [1] 2
c <- 1:6
dim(c) <- c(3,2)
c## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
dim(c) <- c(2,3)
c## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
df <- data.frame(x=1:3, y=c("a","b","c"),z=0)
str(df)## 'data.frame': 3 obs. of 3 variables:
## $ x: int 1 2 3
## $ y: Factor w/ 3 levels "a","b","c": 1 2 3
## $ z: num 0 0 0
cat(names(df), "same as", colnames(df))## x y z same as x y z
cat(length(df), "same as", ncol(df))## 3 same as 3
df1 <- data.frame(x=1:3, y=c("a","b","c"),z=0)
str(df1)## 'data.frame': 3 obs. of 3 variables:
## $ x: int 1 2 3
## $ y: Factor w/ 3 levels "a","b","c": 1 2 3
## $ z: num 0 0 0
df2 <- data.frame(x=1:3, y=c("a","b","c"),z=0, stringsAsFactors=FALSE)
str(df2)## 'data.frame': 3 obs. of 3 variables:
## $ x: int 1 2 3
## $ y: chr "a" "b" "c"
## $ z: num 0 0 0
### subseting
## atomic vectors
x <- c(2.1, 4.2, 3.3, 5.4)
# Positive integer
x[c(3,1)]## [1] 3.3 2.1
order(x)## [1] 1 3 2 4
x[order(x)]## [1] 2.1 3.3 4.2 5.4
x[c(1,1,1)]## [1] 2.1 2.1 2.1
x[c(2.1, 2.9)]## [1] 4.2 4.2
# negative integer
x[-c(1, 3)]## [1] 4.2 5.4
# logical vector
x[c(TRUE, TRUE, FALSE, FALSE)]## [1] 2.1 4.2
x > 3## [1] FALSE TRUE TRUE TRUE
x[x > 3]## [1] 4.2 3.3 5.4
x[c(TRUE, TRUE, NA, FALSE)]## [1] 2.1 4.2 NA
# nothing
x[]## [1] 2.1 4.2 3.3 5.4
# zero
x[0]## numeric(0)
a <- matrix(1:9, nrow=3)
colnames(a) <- c("A","B","C")
a## A B C
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
a[1:2,]## A B C
## [1,] 1 4 7
## [2,] 2 5 8
a[c(T,F,T), c("B","A")]## B A
## [1,] 4 1
## [2,] 6 3
a[,-2]## A C
## [1,] 1 7
## [2,] 2 8
## [3,] 3 9
options(stringsAsFactors = FALSE)
df <- data.frame(x=1:2, y=2:1, z=letters[1:2])
df[df$x==2,]## x y z
## 2 2 1 b
df[c("x","z")] # like a list## x z
## 1 1 a
## 2 2 b
df[,c("x","z")] # like a matrix## x z
## 1 1 a
## 2 2 b
| Functions | simplifying | preserving |
|---|---|---|
| List | x[[1]] | x[1] |
| Vector | x[[1]] | x[1] |
| Factor | x[1:2, drop=T] | x[1:2] |
| Data frame | x[,1] or x[[1]] | x[, 1, drop=F] or x[1] |
grades <- c(1,2,2,3,1)
info <- data.frame(grade=3:1, desc=c("Excellent", "Good", "Poor"), fail=c(F,F,T))
id <- match(grades, info$grade)
id## [1] 3 2 2 1 3
info[id,]## grade desc fail
## 3 1 Poor TRUE
## 2 2 Good FALSE
## 2.1 2 Good FALSE
## 1 3 Excellent FALSE
## 3.1 1 Poor TRUE
If you take a long time to obtain your result. How to save your result so in the future, you won’t bother re-run them again?
a <- 1:4
b <- 2:5
ans <- a * b
result <- list(a=a, b=b, ans=ans)
save(result,file="myResult.rdata")suppressWarnings(library(knitr))
purl("basic.rmd", output = "basic.R ", documentation = 2)