Exercises

These exercises accompany the Data Types tutorial.

Tip: you can use a ? in front of the function name to access the help documentation for any function (e.g. ?round)

  1. Make sure you use the RIntroDay1.R script that you used from Exercise 1 and save these exercises to it as well. Calculate the log to the base 10 of ‘obj1’ (from Exercise 1) using a built-in function. In case you didn’t save it, obj1 <- 15
  2. Calculate the square root of ‘obj2’ (from Exercise 1) by raising it to the power of 0.5, then calculate the square root of ‘obj2’ using a a built-in R function. In case you didn’t save it, obj2 <- 125
  3. Create an object called ‘obj4’ and set it to the value of 55.4367. Use the round() function to round this number to 2 decimal places and to 0 decimal places. Use floor() and and ceiling() to trim obj4 to an integer and compare the output.
  4. Create a vector called ‘my.seq’ that consists of 9 numbers, starting at 1 and going to 27,skipping by 3.
  5. Create a vector called ‘my.rep.seq’ that will contain 30 values that consists of the sequence 1,2,3 repeated 10 times. 6. Install the ‘bReeze’ package and load it. Load the example data using data(winddata). View the first five lines of data. What is the name of the first column of data?
  6. Go to the “datasets” folder on your thumb drive and open the spreadsheet called “so2_hourly.xlsx”. Convert Sheet 1 in the .xls file to a .csv file and save it as “so2_data.csv”. Then use the read.csv() function to import the data into R. Save it as a data frame named ‘df1.’
  7. What are the column names of the ‘df1’ dataset? What is the last value in the third column of ‘df1’?


Solutions


Solution 1

obj1 <- 15
log10(obj1)
## [1] 1.176091

Solution 2

obj2 <- 125
obj2 ^ 0.5
## [1] 11.18034
sqrt(obj2)
## [1] 11.18034

Solution 3

obj4 <- 55.4367
round(obj4, digits= 2)
## [1] 55.44
round(obj4, digits= 0)
## [1] 55
floor(obj4)
## [1] 55
ceiling(obj4)
## [1] 56

Solution 4

my.seq <- seq(1, 27, by= 3)
my.seq
## [1]  1  4  7 10 13 16 19 22 25

Solution 5

my.rep.seq <- rep(seq(1, 3, by= 1), 10)
my.rep.seq
##  [1] 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3

Solution 6

install.packages("bReeze")
# or use the Install button in RStudio and search for bReeze
library(bReeze)  # load the bReeze package
data(winddata)   # load the example dataset called 'winddata'
head(winddata)  # Use head to view the first five lines of data, can view colnames as well 
colnames(winddata) #First column name is date_time

Solution 7

df1 <- read.csv("E:/RIntro/datasets/so2_data.csv", header = TRUE, stringsAsFactors = FALSE)

Solution 8

colnames(df1)  # Use colnames to view all the column names in this dataset
#  "Date" "Hour" "SO2"  "WNSP" "WNDR"

tail(df1)   # Use the tail function to view the last 5 rows of data. 
# Value is 0.006