forked from datadicts/Intro-to-R-2ed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Intro to R - Ch 05A.R
61 lines (43 loc) · 1.34 KB
/
Intro to R - Ch 05A.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
############################################################
# R script to supplementIntro to R for Business, Chapter 05#
# Written by Troy Adair #
############################################################
## if...else statements vs. ifelse() function
# Loading vector name back into memory
name <- c("Mike", "Lucy", "John")
# Using if...else statement with one element of name
if (name[2]=="Lucy") {
print("Name Matched")
} else {
print("Name NOT Matched")
}
# What happens if we try and use if...else with entire name vector?
if (name=="Lucy") {
print("Name Matched")
} else {
print("Name NOT Matched")
}
if (name=="Mike") {
print("Name Matched")
} else {
print("Name NOT Matched")
}
# To get vector of results, use ifelse() function:
ifelse(name=="Lucy","Name Matched","Name NOT Matched")
ifelse(name=="John",1,0)
# Programmatically remove all variables from Environment
rm(list=ls(all=TRUE))
# Programmatically clear the Console
cat("\014")
## R for loops
# Let's add another "Lucy" to our list of names and create a vector of ages
name <- c("Mike", "Lucy", "John", "Lucy")
age <- c(7,34,57,9)
# Set n equal to the number of elements in the name vector
n<-length(name)
# Loop to print out the ages of all values where name="Lucy"
for (i in 1:n) {
if(name[i]=="Lucy") {
print(age[i])
} else {}
}