Chapter 6 Data Frames
Generally researchers are interested in working with data frames rather than individual vectors. Data frames can be read into R or created within R.
6.1 loading in data frames from a package
For this workshop we will be working with data from the Lahmen data base. This can be downloaded as a package using the instructions from above.
#install.packages("Lahmen")
library(Lahman)
data(Batting)
# Allows you to see the first 6 hitters
head(Batting)
## playerID yearID stint teamID lgID G AB R H X2B X3B HR RBI SB CS BB SO IBB HBP SH SF GIDP
## 1 abercda01 1871 1 TRO NA 1 4 0 0 0 0 0 0 0 0 0 0 NA NA NA NA 0
## 2 addybo01 1871 1 RC1 NA 25 118 30 32 6 0 0 13 8 1 4 0 NA NA NA NA 0
## 3 allisar01 1871 1 CL1 NA 29 137 28 40 4 5 0 19 3 1 2 5 NA NA NA NA 1
## 4 allisdo01 1871 1 WS3 NA 27 133 28 44 10 2 2 27 1 1 0 2 NA NA NA NA 0
## 5 ansonca01 1871 1 RC1 NA 25 120 29 39 11 3 0 16 6 2 2 1 NA NA NA NA 0
## 6 armstbo01 1871 1 FW1 NA 12 49 9 11 2 1 0 5 0 1 0 1 NA NA NA NA 0
6.2 Saving CSV files.
First you want to find your directory.
## [1] "C:/Users/Owner/Documents/Introduction2R"
When you save a file it will be saved to this directory. You can save a csv file with the following code.
You can save a csv file with the following code.
6.3 Creating data frames in R
You can also create data frames within R. Here we are creating a data frame with hits and at bats for different players.
# create player
player <- c("Player01", "Player02", "Player03", "Player04","Player05")
# Create Vector of Hits
hits <- c(3,1,0,2,4)
# Create Vector of At Bats
at_bats <- c(4,4,3,5,4)
# Create full data frame
hits_data_frame <- data.frame(PlayerID = player,
Hits=hits,
AtBats = at_bats)
hits_data_frame
## PlayerID Hits AtBats
## 1 Player01 3 4
## 2 Player02 1 4
## 3 Player03 0 3
## 4 Player04 2 5
## 5 Player05 4 4
Note: Every vector used in the data frame must be of equal length.
6.3.1 Challenge
Create a data frame within R which stores information about 5 teams. The first column has team name, second column has wins, and the third column has losses. Team name and win/losses can be made up. Store the data with the name step_1. Save this data fram as a .csv file and then upload the saved data stored with the name step_2.