‘<’

Matthias Nevins

R funtion “<” is a RELATIONAL OPERATION. This binary operation can be used to compare atomic vectors.
# e.g. You could assign a variable "a" to a random set of numbers and then determing which of those numbers are less than a selected value
runif(10)
##  [1] 0.058000634 0.295759255 0.707494915 0.687932926 0.373144211
##  [6] 0.002972961 0.742724370 0.775231160 0.081741105 0.742932014
a <- runif(10)
a < .05
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
# Or you could create two variables and compare them
x<-5
y<-6
x<y
## [1] TRUE

‘writeLines’

Matthias Nevins

At the most basic level, writeLines is used to write text lines from r code. The more advanced use of this function can write text to other types of connected files (text files, .html, etc.). See help(writeLines) for more details on advanced usage.
# For example, writeLines can be used with "LETTERS"
# We can assign LETTERS to variable B
b<-LETTERS 
# Printing b gives us all the leters seperated by ""
head(b)  
## [1] "A" "B" "C" "D" "E" "F"
writeLines(LETTERS) # writeLines applied to letters removes the "" separator
## A
## B
## C
## D
## E
## F
## G
## H
## I
## J
## K
## L
## M
## N
## O
## P
## Q
## R
## S
## T
## U
## V
## W
## X
## Y
## Z
# Another Example 

a<-c("ant", "bee", "bug", "tree", "fern", "crow") 
writeLines(a)
## ant
## bee
## bug
## tree
## fern
## crow
# To write to an actual file you can create a new text file and write to that file. See the example below using the file.create function 
file.create("sample.txt")
## [1] TRUE
fileConn <- file("sample.txt")
writeLines(a, con = fileConn, sep = " ")
file.show("sample.txt")

‘seq_along’

Matthias Nevins

seq_along generates regular sequences. seq_along(x) takes a vector for x, and it creates a sequence upto the count of elements in the vector. seq() acts like seq_along() except when passed a vector of length 1, in which case it acts like seq_len().

# EXAMPLE
a <- c(8, 9, 10)
b <- c(9, 10)
c <- 10

seq_along(a)
## [1] 1 2 3
seq_along(b)
## [1] 1 2
seq_along(c)
## [1] 1
# Compared to the seq() function 

seq(a)
## [1] 1 2 3
seq(b)
## [1] 1 2
seq(c)
##  [1]  1  2  3  4  5  6  7  8  9 10

‘union’

Matthias Nevins

union() is used on a set of vectors. union will join two vectors and discard any duplicated values.

unionX <- c(1:5)
unionX 
## [1] 1 2 3 4 5
unionY <- c(3:8)
unionY
## [1] 3 4 5 6 7 8
union(unionX,unionY)
## [1] 1 2 3 4 5 6 7 8
# The duplicates are removed and the set is now joined with union()
## More perfect?? 

‘is.na’

Matthias Nevins

is.na is a generic function that indicates which elements are missing within a vector. “NA” is a logical constant of length 1 which contains a missing value indicator.

isX <- c(1,2,NA,4,5,NA,6)
is.na(isX) # is.na indicates which elemenst are missing 
## [1] FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE
is.na(c(1,4,8,2,4,NA,6,NA,9))
## [1] FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE
xx <- c(0:4)
is.na(xx) <- c(2, 4)
is.na(xx)
## [1] FALSE  TRUE FALSE  TRUE FALSE

‘data.frame’

Matthias Nevins

Description: The function data.frame() creates data frames, tightly coupled collections of variables which share many of the properties of matrices and of lists, used as the fundamental data structure by most of R’s modeling software.

Arguments:

# We explored a similar example in class. See below for an example of how to construct a data.frame

# Begin by creating variables 
number <- 1:20
species <- rep(c("Maple", "Beech", "Pine", "Oak", "Spruce"), each=4)
basalArea <- runif(20)

dFrame <- data.frame(number,species,basalArea, stringsAsFactors = FALSE)
print(dFrame)
##    number species basalArea
## 1       1   Maple 0.7919968
## 2       2   Maple 0.8239264
## 3       3   Maple 0.4882047
## 4       4   Maple 0.8566885
## 5       5   Beech 0.8737558
## 6       6   Beech 0.6188289
## 7       7   Beech 0.6958675
## 8       8   Beech 0.5078135
## 9       9    Pine 0.2459670
## 10     10    Pine 0.1224092
## 11     11    Pine 0.4783158
## 12     12    Pine 0.9533409
## 13     13     Oak 0.1067167
## 14     14     Oak 0.2545774
## 15     15     Oak 0.4665553
## 16     16     Oak 0.8648503
## 17     17  Spruce 0.2098662
## 18     18  Spruce 0.8477939
## 19     19  Spruce 0.4696177
## 20     20  Spruce 0.5959844
str(dFrame)
## 'data.frame':    20 obs. of  3 variables:
##  $ number   : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ species  : chr  "Maple" "Maple" "Maple" "Maple" ...
##  $ basalArea: num  0.792 0.824 0.488 0.857 0.874 ...

‘match’

Matthias Nevins

Description: The ‘match’ function returns or finds the first occurance of the first argument in the second argument. The %in% function can also be used in a similar way to return a logical vector (TRUE,FALSE)

  • Usage: match(x,table,nomatch=NA_integer_,incomparables = NULL) or x %in% table
  • Arguments:
    • x: a vector or NULL and represent the values to be matched
    • table: a vector or NULL and represent the values to be matched against the first argument
    • nomatch: the value to be returned if there is no match *incomparables: defines the a vector of the values that cannot be matched. FALSE=NULL
EXAMPLE: Using ‘match’ and ‘%in%’
  • Argument 1: x=3
  • Argument 2: table = 2:6
match(x=3, table=2:6)
## [1] 2
# The match function returns the first occurance of the first argument (x=3), in the second argument (table = 2:6). In this example the vector '3' is found in the second position of the table
# If you would like to return a logical vector you can use %in%
3%in%2:6 # This asks if 3 is in your table 2:6. It returns "TRUE" indictating that the first argument is indeed found in the second
## [1] TRUE

‘message’

Matthias Nevins

The message function generates a diagnostic message from the argument you define. These messages are not warnings or errors, but nevertheless represented as condition.

  • Usage: message(…, domain = NULL, appendLF = TRUE)
  • Arguments:
    • …: zero or more objects which displayed as the message. Multiple objects will be pasted together with no seperator. domain: If NA, messages will not be translated appendLF: Logical argument should messages
message("ABC","DEFGH") # Both arguments are not separated 
## ABCDEFGH
message("good morning everyone", appendLF = FALSE) # you can display a separated message by including the whole message within a single object
## good morning everyone