Logical Operators:
- Use logical operations to get R to agree that “two plus two equals
5” is FALSE.
a = (5 == (2+2))
print(a)
## [1] FALSE
- Use logical operations to test whether 8 ^ 13 is less than 15 ^
9.
b = (8^13) < (15^9)
print(paste0("Is 8 ^ 13 is less than 15 ^ 9? : ", b))
## [1] "Is 8 ^ 13 is less than 15 ^ 9? : FALSE"
Variables:
- Create a variable called potato whose value corresponds to the
number of potatoes you’ve eaten in the last week. Or something equally
ridiculous. Print out the value of potato.
potato <-8
print(paste0("Number of potatoes consumed in the last week: ", potato ))
## [1] "Number of potatoes consumed in the last week: 8"
- Calculate the square root of potato using the sqrt() function. Print
out the value of potato again to verify that the value of potato hasn’t
changed.
sqrt_potato <- sqrt(potato)
print(paste0("square root of potato: ", sqrt_potato))
## [1] "square root of potato: 2.82842712474619"
print(paste0("value of potato after square root: ", potato))
## [1] "value of potato after square root: 8"
- Reassign the value of potato to potato * 2. Print out the new value
of potato to verify that it has changed.
potato <- potato * 2
print(paste0("value of potato *2: ", potato))
## [1] "value of potato *2: 16"
- Try making a character (string) variable and a logical variable .
Try creating a variable with a “missing” value NA. You can call these
variables whatever you would like. Use class(variablename) to make sure
they are the right type of variable.
st_variable <- "hola"
log_variable <- TRUE
mis_value <- NA
class(st_variable)
## [1] "character"
Vectors:
- Create a numeric vector with three elements using c().
n_vec <- c(0,1,2)
print(n_vec)
## [1] 0 1 2
- Create a character vector with three elements using c().
c_vec <- ("Hola, welcome, everyone")
print (c_vec)
## [1] "Hola, welcome, everyone"
- Create a numeric vector called age whose elements contain the ages
of three people you know, where the names of each element correspond to
the names of those people.
age=c(26,28,32)
names(age)=c("Singhania", "Dsouza", "Ahmed")
print (age)
## Singhania Dsouza Ahmed
## 26 28 32
- Use “indexing by number” to get R to print out the first element of
one of the vectors you created in the last questions.
print(age[1])
## Singhania
## 26
- Use logical indexing to return all the ages of all people in age
greater than 20.
age[age>20]
## Singhania Dsouza Ahmed
## 26 28 32
- Use indexing by name to return the age of one of the people whose
ages you’ve stored in age
age["Dsouza"]
## Dsouza
## 28
Matrices:
Dataframes:
- Load the airquality dataset.
- Use the $ method to print out the Wind variable in airquality.
- Print out the third element of the Wind variable.
airquality
airquality$Wind
## [1] 7.4 8.0 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 6.9 9.7 9.2 10.9 13.2
## [16] 11.5 12.0 18.4 11.5 9.7 9.7 16.6 9.7 12.0 16.6 14.9 8.0 12.0 14.9 5.7
## [31] 7.4 8.6 9.7 16.1 9.2 8.6 14.3 9.7 6.9 13.8 11.5 10.9 9.2 8.0 13.8
## [46] 11.5 14.9 20.7 9.2 11.5 10.3 6.3 1.7 4.6 6.3 8.0 8.0 10.3 11.5 14.9
## [61] 8.0 4.1 9.2 9.2 10.9 4.6 10.9 5.1 6.3 5.7 7.4 8.6 14.3 14.9 14.9
## [76] 14.3 6.9 10.3 6.3 5.1 11.5 6.9 9.7 11.5 8.6 8.0 8.6 12.0 7.4 7.4
## [91] 7.4 9.2 6.9 13.8 7.4 6.9 7.4 4.6 4.0 10.3 8.0 8.6 11.5 11.5 11.5
## [106] 9.7 11.5 10.3 6.3 7.4 10.9 10.3 15.5 14.3 12.6 9.7 3.4 8.0 5.7 9.7
## [121] 2.3 6.3 6.3 6.9 5.1 2.8 4.6 7.4 15.5 10.9 10.3 10.9 9.7 14.9 15.5
## [136] 6.3 10.9 11.5 6.9 13.8 10.3 10.3 8.0 12.6 9.2 10.3 10.3 16.6 6.9 13.2
## [151] 14.3 8.0 11.5
airquality$Wind[3]
## [1] 12.6
- Create a new data frame called aq that includes only the first 10
cases. Hint: typing c(1,2,3,4,5,6,7,8,9,10) is tedious. R allows you to
use 1:10 as a shorthand method!
- Use logical indexing to print out all days (ie. cases) in aq where
the Ozone level was higher than 20.
- What did the output do with NA values?
- Use subset() to do the same thing. Notice the difference in the
output.
aq=head(airquality,10)
aq_idx = aq[aq$Ozone>20, ]
print(aq_idx)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## NA NA NA NA NA NA NA
## 6 28 NA 14.9 66 5 6
## 7 23 299 8.6 65 5 7
## NA.1 NA NA NA NA NA NA
aq_sbset = subset(aq,Ozone>20)
print(aq_sbset)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 6 28 NA 14.9 66 5 6
## 7 23 299 8.6 65 5 7
- Create a TooWindy variable inside aq, which is a logical variable
that is TRUE if Wind is greater than 10, and FALSE otherwise.
aq$TooWindy<-aq$Wind>10
print(aq$Wind)
## [1] 7.4 8.0 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6
print(aq$TooWindy)
## [1] FALSE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE FALSE
- Use the length() function to determine the number of observations in
the airquality dataframe.
length(airquality)
## [1] 6
- Calculate the mean and standard deviation of one of the variables in
airquality.
mean(airquality$Temp)
## [1] 77.88235
- Make a table of the Temp values.
sd(airquality$Temp)
## [1] 9.46527
- Make a histogram of the Ozone column. Is it a normal distribution?
Why or why not?
hist(airquality$Ozone)

print("The distribution is not normal as it is not symmetric about the mean and is skewed")
## [1] "The distribution is not normal as it is not symmetric about the mean and is skewed"
Functions:
- Make a simple function to calculate x + 6.
add_six <- function(int_val){
return(int_val+6)
}
add_six(9)
## [1] 15
- Use that function add 6 to the Temp column in airquality.
airquality$Temp
## [1] 67 72 74 62 56 66 65 59 61 69 74 69 66 68 58 64 66 57 68 62 59 73 61 61 57
## [26] 58 57 67 81 79 76 78 74 67 84 85 79 82 87 90 87 93 92 82 80 79 77 72 65 73
## [51] 76 77 76 76 76 75 78 73 80 77 83 84 85 81 84 83 83 88 92 92 89 82 73 81 91
## [76] 80 81 82 84 87 85 74 81 82 86 85 82 86 88 86 83 81 81 81 82 86 85 87 89 90
## [101] 90 92 86 86 82 80 79 77 79 76 78 78 77 72 75 79 81 86 88 97 94 96 94 91 92
## [126] 93 93 87 84 80 78 75 73 81 76 77 71 71 78 67 76 68 82 64 71 81 69 63 70 77
## [151] 75 76 68
add_six(airquality$Temp)
## [1] 73 78 80 68 62 72 71 65 67 75 80 75 72 74 64 70 72 63
## [19] 74 68 65 79 67 67 63 64 63 73 87 85 82 84 80 73 90 91
## [37] 85 88 93 96 93 99 98 88 86 85 83 78 71 79 82 83 82 82
## [55] 82 81 84 79 86 83 89 90 91 87 90 89 89 94 98 98 95 88
## [73] 79 87 97 86 87 88 90 93 91 80 87 88 92 91 88 92 94 92
## [91] 89 87 87 87 88 92 91 93 95 96 96 98 92 92 88 86 85 83
## [109] 85 82 84 84 83 78 81 85 87 92 94 103 100 102 100 97 98 99
## [127] 99 93 90 86 84 81 79 87 82 83 77 77 84 73 82 74 88 70
## [145] 77 87 75 69 76 83 81 82 74
Packages:
- Install the ggplot2 package.
- Install the car package.
- Install the ez package. (no output necessary for these three)
- Load the car library.
#install.packages("ggplot2")
#install.packages("car")
#install.packages("ez")
library("car")
## Loading required package: carData
Files
- Import the csv file provided on Canvas.
library(readr)
lab_R_data <- read_csv("/Users/darklord/Downloads/example_introR.csv")
## Rows: 33949 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): expno, orginalcode, whichhand
## dbl (11): rating, id, speed, error, LR_switch, finger_switch, rha, word_leng...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.