As part of the assignment template, the following two lines were run before the code run in the sections below.
> library(NCStats)
> library(ggplot2)
Note:
-
The object name to the left of each assignment operator (i.e.,
<-
) can largely be whatever you want. In the examples below, I tried to use names that made sense for the data being imported.
Loading Data from Webpage
- There are four variables in BrainHead.csv.
- The variables in Brainhead.csv are: head.size, brain.weight, gender, age.group.
- There are 237 individuals in BrainHead.csv.
R Code and Results
> bhobj <- read.csv("BrainHead.csv")
> str(bhobj)
'data.frame': 237 obs. of 4 variables:
$ head.size : int 4512 3738 4261 3777 4177 3585 3785 3559 3613 3982 ...
$ brain.weight: int 1530 1297 1335 1282 1590 1300 1400 1255 1355 1375 ...
$ gender : chr "male" "male" "male" "male" ...
$ age.group : chr "20-46" "20-46" "20-46" "20-46" ...
Loading Your Own Data
Note:
-
When entering your own data into R, make sure that variables are organized by columns (so that individuals are organized by rows) and that each column has a useful name in the first/top row. Also make sure that your variable names and your data do not have any spaces in them as this can cause some issues with how the data are loaded into R. So, if your variable is the professor’s name don’t label that column as
Professors Name
; rather call itProf
,Professor
,ProfName
,Prof.Name
, or something similar. Similarly, for the data don’t writeDerek Ogle
; rather useDerekOgle
,Derek.Ogle
,Derek_Ogle
,Ogle
,ProfessorDoctorOgleSir
, or something similar. -
Don’t use
str()
,peek()
, orheadtail()
for data.frames that are very small. Just type the name of the data.frame to show all of the individuals in that data.frame.
Answers will vary by student, but one example is shown below.
R Code and Results
> famex <- read.csv("Family_Example.csv")
> famex
name age relation
1 Will 87 Pops
2 Carolyn 82 Ma
3 Lisa 63 Sister
4 Pam 60 Sister
5 Brooke 50 Sister
Stacked Data
Note:
- In stacked data one variable denotes to which group an individual belongs and the other variable is the variable recorded on each individual.
The data.frame is shown below.
R Code and Results
> test <- read.csv("Test_Example.csv")
> test
section score
1 Early 68
2 Early 78
3 Early 64
4 Early 55
5 Early 82
6 Early 63
7 Early 59
8 Late 87
9 Late 92
10 Late 73
11 Late 83
12 Late 91