floor

Samantha Alger

Floor(x) rounds down a given number (x) to an integer.

floor(2.76) # the function rounds down 2.76 to the value '2'
## [1] 2
floor(-3.456) # the function rounds down -3.456 to the value '-4'
## [1] -4

unlist

Samantha Alger

unlist transforms a list structure into a vector.

# setting up a list
myList <- list(1:30,matrix(1:12,nrow=4,byrow=TRUE),
               LETTERS[1:5])
# view the list
print(myList)
## [[1]]
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30
## 
## [[2]]
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## [4,]   10   11   12
## 
## [[3]]
## [1] "A" "B" "C" "D" "E"
# using the unlist function
# output produces a vector containing all atomic components of the list
unlist(myList)
##  [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12" "13" "14"
## [15] "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28"
## [29] "29" "30" "1"  "4"  "7"  "10" "2"  "5"  "8"  "11" "3"  "6"  "9"  "12"
## [43] "A"  "B"  "C"  "D"  "E"

cat

Samantha Alger

Print output to the screen or to a file. Use ‘cat’ instead of ‘print’ to print information from a function. cat(…, file “”, sep=“”,append=FALSE)

  • … -The information to be printed to the screen or saved file

  • file- Specifies a file to be created (this is optional)

  • sep -Specifies what separates the objects in…that are to be printed.

  • append -If a file is specified, then indicate whether to append to the content in the existing file (the default is not to append, which means to overwrite the existing content).

x <- c(1:10)
print(x)
##  [1]  1  2  3  4  5  6  7  8  9 10
# Space separator
cat(x)
## 1 2 3 4 5 6 7 8 9 10
# Multi-space separator, and adds a period between each number
cat(x, sep=" . ")
## 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10
# Separates each number by a tab
cat(x, sep="\t")
## 1    2   3   4   5   6   7   8   9   10
# Separates each number by a line break
cat(x, sep="\n")
## 1
## 2
## 3
## 4
## 5
## 6
## 7
## 8
## 9
## 10
# Use cat to index particular values
# and specify how those values will be printed using sep

# creating a data set (y) of 10 random atomic components of either red or blue
y <- sample(c("red", "blue"), 10, TRUE)
y
##  [1] "red"  "blue" "red"  "blue" "red"  "red"  "red"  "blue" "blue" "blue"
# use cat to concatenate and print the two atomic components x and y
cat(x, y)
## 1 2 3 4 5 6 7 8 9 10 red blue red blue red red red blue blue blue
#use cat to specify which values to print (first value of each vector) and separate those two values by a tab.
cat(x[1], y[1], sep="\t")
## 1    red

rev

Samantha Alger

rev() reverses an R object, including vector, array, etc.

# First create a vector
x <- c("red","orange","blue")

#check it out
print(x)
## [1] "red"    "orange" "blue"
#now reverse the order of the vector and print
rev(x)
## [1] "blue"   "orange" "red"

cumprod

Samantha Alger

cumprod() returns a vector with elements that are the cumulative products

# create a vector consisting of 3 elements
x <-c(3,5,10)

# cumprod(x) will find the cumulative products
cumprod(x)
## [1]   3  15 150
# the cumprod() function is doing the following calculations:
3 * 1
## [1] 3
3 * 5
## [1] 15
3*5*10
## [1] 150

write.csv

Samantha Alger

write.csv() is used to write data to a csv file

# a sample data frame

A <- 1:16
B <- rep(c("red","blue","green","NA"), each =4)
C <-runif(16)
data <- data.frame(A,B,C,stringsAsFactors = FALSE)

# write to a file, without row names
write.csv(data, "data.csv")

# same, except without row names
write.csv(data, "data.csv", row.names = FALSE)

# same, except of "NA", return blank cell
write.csv(data, "data.csv", row.names = FALSE, na = "")

#check out the csv file created.
read.csv("data.csv")
##     A     B          C
## 1   1   red 0.91270162
## 2   2   red 0.66345343
## 3   3   red 0.34697132
## 4   4   red 0.50606257
## 5   5  blue 0.65251726
## 6   6  blue 0.86063331
## 7   7  blue 0.14748505
## 8   8  blue 0.34046882
## 9   9 green 0.39860152
## 10 10 green 0.32067043
## 11 11 green 0.78812826
## 12 12 green 0.41192988
## 13 13  <NA> 0.66861432
## 14 14  <NA> 0.28412022
## 15 15  <NA> 0.97299015
## 16 16  <NA> 0.05625328

rep_len

Samantha Alger

rep_len() replicates values for a desired length (“len”)

# the following code will repeat the three bird types 20 times
rep_len(c("finch","thrush","warbler"),20)
##  [1] "finch"   "thrush"  "warbler" "finch"   "thrush"  "warbler" "finch"  
##  [8] "thrush"  "warbler" "finch"   "thrush"  "warbler" "finch"   "thrush" 
## [15] "warbler" "finch"   "thrush"  "warbler" "finch"   "thrush"

read.delim

Samantha Alger

read.delim() is used to read in delimited text files, where data is organized in a data matrix with rows representing cases and columns representing variables. read.delim(file,header=TRUE,sep=“”)

  • file -A file location

  • header - Whether the first line describes the column names

  • sep - The table delimiter, often a tab () or comma

#The following is an example for how you would read in a .txt file.
#It is commented out since the .txt file does not exist

#d <-read.delim("annual.txt", header= TRUE, sep="\t")

all.equal

Samantha Alger

all.equal(target,current) will check if two objects are equal, or nearly equal.

  • target-An R object
  • current- A second R object to compare to target (the first argument)

Output will give the difference betwen the objects or will return TRUE if the values are sufficiently close enough

all.equal(pi, 3.14)
## [1] "Mean relative difference: 0.0005069574"
all.equal(pi, 3.1415)
## [1] "Mean relative difference: 2.949255e-05"
all.equal(pi, 3.141592)
## [1] "Mean relative difference: 2.080441e-07"
all.equal(pi, 3.14159265)
## [1] TRUE

sample

Samantha Alger

sample() will take a random sample of a specified size. You can specify whether to sample with or without replacement.

sample(x, size, replace =FALSE)

  • x - object to be sampled
  • size - size of the sample
  • replace - should sampling be with replacement?
#create an object to sample
x <- runif(1:10)

# take a sample of 100 from x
# without replacement (we will never find duplicates)
sample(x, 4, replace = FALSE)
## [1] 0.81932511 0.08372558 0.88793844 0.63174079
# if you sample with replacement, you can sample at a size larger than the original object
#in this case, there will be numbers repeated
sample(x, 11, replace = TRUE)
##  [1] 0.8193251 0.8879384 0.8440786 0.8879384 0.8879384 0.3359162 0.3124831
##  [8] 0.8440786 0.6317408 0.6301383 0.6301383

setequal

Samantha Alger

setequal() tests if two vectors are equal. The function will return a logical response: (TRUE or FALSE).

#create two different vectors using the sample function that will randomly sample 10 numbers from 1 to 100.
x <- sample(1:100, size=10)
y <- sample(1:100, size=10)

#use setequal to check if the two vectors are the same. since we are using the sample function, we are randomly sampling and so the two vectors will be different. setequal returns a FALSE value. 
setequal(x,y)
## [1] FALSE
#another example:
birds <- rep_len(c("finch","thrush","warbler"),20)
birds2 <- rep_len(c("NA","thrush","warbler"),20) 

#using setequal returns a FALSE value because there is one value different
setequal(birds,birds2)
## [1] FALSE

[

Samantha Alger

square brackets (‘[’) are used to reference or index a particular object within a vector, dataframe, or matrix.

# for vectors:
# create a vector
data = c(1,3,5,7,3,2)

#Using brackets will return the third value of the vector
data[3] 
## [1] 5
#for a given dataframes or matrices:
# make a dataframe:
A <- 1:16
B <- rep(c("red","blue","green","NA"), each =4)
C <-runif(16)
data <- data.frame(A,B,C,stringsAsFactors = FALSE)

#check out the dataframe
print(data)
##     A     B           C
## 1   1   red 0.002108186
## 2   2   red 0.826682980
## 3   3   red 0.367689416
## 4   4   red 0.452006319
## 5   5  blue 0.468060224
## 6   6  blue 0.966229594
## 7   7  blue 0.709837525
## 8   8  blue 0.146217275
## 9   9 green 0.828484280
## 10 10 green 0.963524191
## 11 11 green 0.725213890
## 12 12 green 0.735710083
## 13 13    NA 0.749526377
## 14 14    NA 0.500685158
## 15 15    NA 0.305466812
## 16 16    NA 0.064039144
# Using brackets, the first value specifies the row, the second value specifies the column
#The following will return the value in the first row, second column of the dataframe:
data[1,2]
## [1] "red"

which

Samantha Alger

Use which to find a subset of data that meet a particular criteria.

#Example 1:

#create a dataset
bird<- rep_len(c("finch","thrush","warbler"),20)

#use which to figure out the positions of a particular component
#of the dataset, which ones are "finch", which() returns the position.
which(bird =="finch")
## [1]  1  4  7 10 13 16 19
# Another example:

#For a given vector, which is the index of the 3rd non-NA value?
#create dataset
x <- c(1,NA,2,NA,3)

#which position is the third non-NA value? 
which(!is.na(x))[3]
## [1] 5

atan2

Samantha Alger

atan2(x,y) is a trigonometric function that computes the arc-tangent of two arguments. It will return the angle between the x-axis and the vector (x,y)

atan2(3,4)
## [1] 0.6435011

assign

Samantha Alger

assign() assigns a value to a name assign(x, value, pos=, envir=…)

assign("z", 5)
z
## [1] 5

signif

Samantha Alger

signif(x, digits=y) rounds a number (x) to a specified number of significant digits (y)

signif(7.462527,digits=4)
## [1] 7.463

[[

Samantha Alger

double brackets will return only a single element from a list. A single bracket will return you a list with as many elements.

lst <- list('one','two','three')

a <- lst[1]
class(a)
## [1] "list"
## returns "list"


a <- lst[[1]]
class(a)
## [1] "character"
# returns "character"


#Example showing different uses with double and single brackets:
#Create a matrix
A = matrix(c(2,4,3,1,5,7),nrow=2,ncol=3,byrow=TRUE)
print(A)
##      [,1] [,2] [,3]
## [1,]    2    4    3
## [2,]    1    5    7
#Use double brackets to see the first element in the matrix
A[[1]]
## [1] 2
#Use single brackets to see the entire first column
A[,1]
## [1] 2 1