Creating a help page for 13 functions:

These are all of my randomly selected functions (13):

print(x)
##     functionAssignments     RFunctions
## 3               Burnham      intersect
## 14              Burnham           log2
## 25              Burnham            log
## 36              Burnham      as.matrix
## 47              Burnham          combn
## 58              Burnham           diag
## 69              Burnham       colnames
## 80              Burnham             <-
## 91              Burnham    data.matrix
## 102             Burnham          range
## 113             Burnham capture.output
## 124             Burnham           tail
## 135             Burnham           diff

intersect()

Alex Burnham

Description: This function is one of four Set Operators. It takes two arguments (x and y) each of them is an atomic vector. It checks the two vectors for numbers that are the same value and returns those numbers.

Arguments:

  • x = your vector 1
  • y = your vector 2

Example:

# create two vectors x and y
x <- c(3:13)
y <- c(8:18)

# use the intersect function:
intersect(x=x, y=y)
## [1]  8  9 10 11 12 13

Explaination of Example: In this example the vector x has values 3 through 13 and y has 8 through 18. Values 8 through 13 are returned because they overlap or intersect betwee the vectors x and y.

log2()

Alex Burnham

Description: The log2() function takes a single argument (x) which is a single value or a vector and log transforms it using the base 2 logarithm.

Arguments:

  • x = a single value or vector

Example:

# create vector of values 1 through 10:
x <- c(1:10)

# take the log (base 2) of this vector x:
log2(x=x)
##  [1] 0.000000 1.000000 1.584963 2.000000 2.321928 2.584963 2.807355
##  [8] 3.000000 3.169925 3.321928

Explaination of Example: The output is the log (base 2) transformation of the vector x (1 through 10)

log()

Alex Burnham

Description: The log() function takes two arguemnts: a single value or a vector (x) and “base”, which allows you to choose the base of your log. By default, log() transforms the vector using the base e or natural logarithm which is defined in R as exp(1).

Arguments:

  • x = a single value or vector
  • base = the base of the log you want to use (default = exp(1) or natural log)

Example:

# create vector of values 1 through 10:
x <- c(1:10)

# take the log (base e) of this vector x:
log(x=x)
##  [1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379 1.7917595 1.9459101
##  [8] 2.0794415 2.1972246 2.3025851
# take the log (base 10) of this vector x:
log(x=x, base=10)
##  [1] 0.0000000 0.3010300 0.4771213 0.6020600 0.6989700 0.7781513 0.8450980
##  [8] 0.9030900 0.9542425 1.0000000

Explaination of Example: The first output is the log (base e) transformation of the vector x (1 through 10) The second output is the log (base 10) transformation of the vector x (1 through 10)

as.matrix()

Alex Burnham

Description: This function allows you to convert another data structure into matrix form. Arguments:

  • x = dataframe or another R object
  • dimnames = (character string) and gives names to the dimensions of the matrix

Example:

# make a dataframe:
dataframe <- data.frame(cbind(c("blue", "red", "green"), c(1:3)))
print(dataframe)
##      X1 X2
## 1  blue  1
## 2   red  2
## 3 green  3
# convert data frame to a matrix:
matrix <- as.matrix(x=dataframe)
str(matrix)
##  chr [1:3, 1:2] "blue" "red" "green" "1" "2" "3"
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:2] "X1" "X2"

Explaination of Example: The dataframe, which had two variable types (character and numeric), was converted into a dataframe of the same dimensions. Numeric variables were coerced into characters because a matrix must be of all the same variable type and numeric variables get coerced into characters as part of R’s hierarchical structure.

combn()

Alex Burnham

Description: This function determines the combinations that can be generated from an atomic vector taken m number of ways and returns a matrix, array or list of these combinations.

Arguments:

  • x = vector source for combinations
  • m = number of elements to choose
  • FUN = is a function that can be applied to matrices (by default its the identity matrix)
  • simplify = TRUE or FALSE (by default TRUE) asking if it should be simplified into a matrix (or another array) or a list if FALSE

Example:

# create vector of values 1 through 10:
x <- c(1:10)

# combine 1 through 10 two ways and return a matrix:
combn(x=x, m = 2, simplify = TRUE)
##      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,]    1    1    1    1    1    1    1    1    1     2     2     2     2
## [2,]    2    3    4    5    6    7    8    9   10     3     4     5     6
##      [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
## [1,]     2     2     2     2     3     3     3     3     3     3     3
## [2,]     7     8     9    10     4     5     6     7     8     9    10
##      [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35]
## [1,]     4     4     4     4     4     4     5     5     5     5     5
## [2,]     5     6     7     8     9    10     6     7     8     9    10
##      [,36] [,37] [,38] [,39] [,40] [,41] [,42] [,43] [,44] [,45]
## [1,]     6     6     6     6     7     7     7     8     8     9
## [2,]     7     8     9    10     8     9    10     9    10    10

Explaination of Example: This example shows how the values 1 through 10 can be taken 2 ways in combination. There are 45 ways to combine 2 different values from this vector together without repeating combinations.

diag()

Alex Burnham

Description: Extracts or replaces the diagnals of a matrix. Value will can assign differnt values to the matrix. On its own, it will extract the diagnals which can be useful in multivariate analysis when dealing with correlation or variance/covariance matrices.

Arguments:

  • x = a matrix
  • value = either a single value or a vector of length equal to that of the current diagonal.
  • nrow = optional number of rows when x isn’t a matrix
  • ncol = optional number of columns when x isn’t a matrix

Example:

# make a 3 by 3 matrix of interger values 1 through 9:
matrix <- matrix(data=c(1:9), nrow=3)

# Returns the diagnal of this matrix
diag(x=matrix)
## [1] 1 5 9
# Replaces the diagnal with all 3s and prints matrix:
diag(x=matrix) <- 3
print(matrix)
##      [,1] [,2] [,3]
## [1,]    3    4    7
## [2,]    2    3    8
## [3,]    3    6    3

Explaination of Example: Output one takes the diagnal starting in the top left going down to the bottom right and returns the values. The second output replaces that same diagnal with a string of 3s.

colnames()

Alex Burnham

Description: Retrieve or set the column names of a matrix-like object. The names are in a list and stored in the object using the assignment operator “<-”.

Arguments:

  • x = a matrix-like object (matrix or dataframe) with at least two dimensions
  • <- value is a list of names that is stored in the object “x”
  • do.null = TRUE or FLASE (default is TRUE), if FALSE and names are NULL, names are created.

Example:

# make a dataframe:
dataframe <- data.frame(cbind(c("blue", "red", "green"), c(1:3)))
print(dataframe)
##      X1 X2
## 1  blue  1
## 2   red  2
## 3 green  3
# assign names to columns and print:
colnames(dataframe) <- c("color", "number")
print(dataframe)
##   color number
## 1  blue      1
## 2   red      2
## 3 green      3

Explaination of Example: The names “color” and “number” were added to the two columns of the data frame previously known by generic varable names x1 and x2.

<-

Alex Burnham

Description: This operator, the assingment opperator is the most powerful and usefull character in the R language. It is used to store information into objects creating various data structures. It can be thought of as a unidirectional equals sign.

Arguments:

  • x = object
  • value = your data

Example:

# store 10 random uniform numbers in a vector using <-
x <- runif(10)
print(x)
##  [1] 0.90847850 0.12253259 0.72886279 0.95037718 0.04335591 0.01959930
##  [7] 0.19907246 0.50508626 0.92638876 0.13840169

Explaination of Example: In this example the vector x was created by using the assingment opperator to store 10 randum uniform numbers in x

data.matrix()

Alex Burnham

Description: Converts a data frame into a numeric matrix. This means that character and factor variables will be coerced into numeric values (ranked) rather thatn following R’s usual hierarchical structure.

Arguments:

  • frame = a data frame whose components are logical vectors, factors or numeric vectors.
  • rownames.force = TRUE or FALSE or NA. Should the matrix have character row names rather than no names.

Example:

# make a dataframe:
dataframe <- data.frame(cbind(c("blue", "red", "green"), c(1:3)))
print(dataframe)
##      X1 X2
## 1  blue  1
## 2   red  2
## 3 green  3
# convert to a matrix
data.matrix(frame=dataframe)
##      X1 X2
## [1,]  1  1
## [2,]  3  2
## [3,]  2  3

Explaination of Example: dataframe was converted into a matrix (numeric) with the factor levels for color being used as their numeric values.

range()

Alex Burnham

Description: range returns a vector containing the minimum and maximum of all the given arguments.

Arguments:

  • first value is any numeric or character object
  • na.rm = TRUE or FALSE (if TRUE NAs are omitted)

Example:

# create vector of values 1 through 10:
x <- c(1:10)

# find the range in vector x
range(x)
## [1]  1 10

Explaination of Example: The smallest value “1” and largest “10” are given.

capture.output()

Alex Burnham

Description: Evaluates its arguments with the output being returned as a character string or sent to a file. Related to sink in the same way that with is related to attach.

Arguments:

  • first argument is expression to be evaulated
  • file = filename to store output in (or NULL)
  • append = TRUE or FALSE (apend or overwrite)

Example:

# make a data frame:
dataframe <- data.frame(cbind(rep(c("blue", "red", "green"),3), c(1:9)))
dataframe$X1 <- as.factor(dataframe$X1)
dataframe$X2 <- as.numeric(dataframe$X2)
print(dataframe)
##      X1 X2
## 1  blue  1
## 2   red  2
## 3 green  3
## 4  blue  4
## 5   red  5
## 6 green  6
## 7  blue  7
## 8   red  8
## 9 green  9
# run an ANOVA model:
model <- aov(dataframe$X2~dataframe$X1)

# use capture to 
capture.output(model, file = NULL)
##  [1] "Call:"                                        
##  [2] "   aov(formula = dataframe$X2 ~ dataframe$X1)"
##  [3] ""                                             
##  [4] "Terms:"                                       
##  [5] "                dataframe$X1 Residuals"       
##  [6] "Sum of Squares             6        54"       
##  [7] "Deg. of Freedom            2         6"       
##  [8] ""                                             
##  [9] "Residual standard error: 3"                   
## [10] "Estimated effects may be unbalanced"

Explaination of Example: The output takes the summary output of the model and displays it and saves it

tail()

Alex Burnham

Description: Shows the last rows of a data set (by default 6 but can be changed to any value)

Arguments:

  • x = data set (data frame)
  • n = number of rows displayed

Example:

# make a data frame:
dataframe <- data.frame(cbind(rep(c("blue", "red", "green"),3), c(1:9)))

# use tail
tail(x=dataframe, n=3)
##      X1 X2
## 7  blue  7
## 8   red  8
## 9 green  9

Explaination of Example: This shows me the last 3 rows of this data frame

diff()

Alex Burnham

Description: Returns suitably lagged and iterated differences between the values in a vector or a matrix.

Arguments:

  • x = a numeric vector or matrix containing the values to be differenced
  • differences = an integer indicating the order of the difference.
  • lag = inger of which lag to use

Example:

# create vector of values 1 through 10:
x <- c(1:10)

# Differnces between values in that have a differnce of 2
diff(x=x, differences = 2)
## [1] 0 0 0 0 0 0 0 0
# Differnces between values in that have a differnce of 2
diff(x=x, differences = 1)
## [1] 1 1 1 1 1 1 1 1 1

Explaination of Example: The first output gives all 0 values because this sequece increases by 1 and no values next to eachother have differences of 2.The next output looks for differnces of 1. They are all differnt by 1 so it returns a string of 1s.

&

Alex Burnham

Description: This character is a logical operator and acts on logical and numeric (numeric-like) vectors. single “&” indicates each value within the vector is evaluated and in && only the first value is evaluated.

Arguments:

  • x = vector that is to be operated on
  • y = object with operations within

Example:

# create vector of values 1 through 10:
x <- c(1:10)
# create vector of values 11 through 20:
y <- x>5

# single and symbol
y & x
##  [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
# double and symbol
y && x
## [1] FALSE

Explaination of Example: In this case, the opparator puts out a string of logicals indcating where the values of x are greater than 5 and the second example does this only to the first value in the vector.

count.fields()

Alex Burnham

Description: This functon counts how many fields are in a data on each line of file being loaded in by counting what is between the seperator in the file.

Arguments:

  • file = file name
  • sep = the type of seperation in the file (comma “,” if .csv)
  • skip = how many lines to skip before reading in data

Example:

# create dataframe and write out as a .csv file:
dataframe <- data.frame(cbind(rep(c("blue", "red", "green"),3), c(1:9)))
dataframe <- write.csv(dataframe, "dataframe.csv")

# count fields in .csv file:
count.fields(file="dataframe.csv", sep=",", skip=1)
## [1] 3 3 3 3 3 3 3 3 3

Explaination of Example: This says that there are 3 values on each of the 9 line of this .csv file we read in.

ceiling()

Alex Burnham

Description: This function takes a vector (x) and returns a vector that rounds all values up to their nearest integer value. For instance 3.4 becomes 4 not 3.

Arguments:

  • x = a vector

Example:

# create vector of values:
x <- c(0.4, 2.3, 9.4, 3.4, 4.2)

ceiling(x)
## [1]  1  3 10  4  5

Explaination of Example: All values got rounded up to their nearest integer values.

any()

Alex Burnham

Description: Given a set of logical vectors, is at least one of the values true?

Arguments:

  • 1 or more logical vectors
  • na.rm = if TRUE, na values are removed

Example:

# two vectors of 10 values:
set.seed(100)
x <- rnorm(10)
y <- rnorm(10)
# logical statements (y greater than x - TRUE or FALSE) or the opposite
z <- y > x
t <- y < x

# one logical vector 
any(z)
## [1] TRUE
# evaluating 2 logical vectors 
any(z,t)
## [1] TRUE

Explaination of Example: Returns a TRUE because at least on of the values is in fact TRUE. Second example also returns TRUE because both vectors have at least one TRUE value contained within.

sweep()

Alex Burnham

Description:

Arguments:

  • x = an array (matrix or higher dimension)
  • FUN = finary opporator “-” subtraction symbol is default
  • MARGIN 1 = rows and 2 = columns
  • STATS = vecotor as long as either the row or column as sepcified by MARGIN
  • check.margin = logical, if TRUE checks if margin equals dimensions of matrix x

Example:

# make a dataframe:
dataframe <- data.frame(cbind(c(2,4,6), c(1:3)))
print(dataframe)
##   X1 X2
## 1  2  1
## 2  4  2
## 3  6  3
# convert to a matrix
matrix <- data.matrix(frame=dataframe)

sweep(x=matrix, MARGIN = 2, STATS = c(2,3), check.margin=TRUE, FUN="-") 
##      X1 X2
## [1,]  0 -2
## [2,]  2 -1
## [3,]  4  0

Explaination of Example: Returns a matrix where 2 and 3 are subtracted from each row