Business Analytics
Homework 1 Solutions
Before you begin the homework, please read question 8.
1. For the following, use the data frame hills2000 that comes in the DAAG package.
(a) How many columns does hills2000 have?
library(DAAG)
ncol(hills2000)
# [1]

1. For the following, use the data frame hills2000 that comes in the DAAG package.
a. How many columns does hills2000 have?
b. What are the names of the columns?
c. How many rows does it have?
d. Compute a summary of the columns in the data frame.
e. Crea

1. Use R commands to accomplish the following
a. Load the data file sales.RData (note that this is already in R's format so you should use
the function load rather than read.csv).
b. Provide a summary of each column in the sales data frame.
c. What are th

# Factors and Dates
# Factors
# Used for categorical data
#
gender <- c("M", "F", "F", "M", "F", "M", "F")
gender
# The function factor creates a factor
gender <- factor(gender)
gender
levels(gender) # Shows the levels
table(gender) # A handy function
yea

# Vectors are the fundamental data type in R
# Numeric, character, boolean
# Each column of a data frame is a vector
# c stands for concatenate
c(2, 3, 5, 2, 7, 1)
my.first.vector <- c(2, 3, 5, 2, 7, 1)
my.first.vector
3:10
c(TRUE, FALSE, TRUE, TRUE, FALS

library(DAAG)
hills
# Hills is a data frame
# remember - a data frame is the main data type we work with when analyzing data
# How big is it?
ncol(hills)
nrow(hills)
# Hills has 3 columns and 35 rows
dim(hills) # nrow and ncol all at once
names(hills) # T

# Sorting
# R has three functions related to sorting:
# sort - sorts the elements in a vector
# order - returns the order of the elements
# rank - returns each element's rank
a <- c(5, 10, 2)
# Sort a
sort(a)
a
# Order
# If we were to put the vector in or

# Grades
grades <- read.csv("grades.csv")
grades
head(grades)
# Shows first few rows
names(grades)
# Let's see what type of data is in each column
sapply(grades, class)
# R automatically reads in character data and converts it to factors
# Three options f

# Subscripting
# Works like vectors
# Except we use two subscripts: row and column
# data.frame[row(s), column(s)]
library(DAAG)
austpop
# The population of Queensland in 1957
# This is the 5th row and 4th column
austpop[5, 4]
# Total population of Austra

San Jos State University
College of Business
Department of Marketing & Decision Sciences
BUS194B Business Analytics
Spring 2016
Instructor:
Dr. David Czerwinski
Office Location:
Business Tower 753
Email:
[email protected]
Office Hours:
Online or b