# 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
# 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")
# 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
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??
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
# 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(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("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