0% found this document useful (0 votes)
17 views20 pages

Loading Data into R: A Step-by-Step Guide

This document provides instructions for loading data into R and performing basic exploratory data analysis. It discusses loading CSV files into R using read.csv and checking the data structure with str and dim. Further sections cover extracting subsets of data using [ and $ operators, updating data frames in place, handling missing values, and analyzing loaded cancer data to answer questions.

Uploaded by

BuckyKatt9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views20 pages

Loading Data into R: A Step-by-Step Guide

This document provides instructions for loading data into R and performing basic exploratory data analysis. It discusses loading CSV files into R using read.csv and checking the data structure with str and dim. Further sections cover extracting subsets of data using [ and $ operators, updating data frames in place, handling missing values, and analyzing loaded cancer data to answer questions.

Uploaded by

BuckyKatt9
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Loading Data into R

Heike Hofmann, Hadley Wickham


Loading data

We will use .csv (comma separated values),


because most software can both write and
read this format

somedataset <- [Link]([Link]())

Always check with str() that the le has loaded


correctly
Your turn

Open the Shangri La data in excel, save it as


csv, and then load into R.

Open the baseball data in excel, save it as


csv, and then load into R.

Check that they look OK using str()

Advanced: Open the csv in Word. Try and


break the data import, by adding odd
characters (try #, , , ), read ?[Link] and
gure out whats going on.
Examining variables

head(a)

summary(a)

str(a)

dim(a)
Your turn

Download data from the Unites States


Cancer Statistics at
[Link]
download_data.htm

Unzip the archive (use Winzip, e.g.)

Load [Link] into Excel (2007)


in Excel

Replace all ~ by NA. Are there other


symbols that should be replaced by NAs?

Delete all records for 2002-2004, for male


and female, and all other elds that
represent sums of other elds

Split rate, upper & lower CI, and count into


two columns each according to event type

Save as comma delimited text le (.csv)


Switch to R

# Load data into R


cancer <- [Link]([Link]())

Use
head(cancer)
dim(cancer)
summary(cancer)
to check that it worked

Did you catch all the symbols for missing data in


Excel?
Data frames

A data frame is a list of vectors of the same


length

Create with [Link] using named


arguments

[Link](a=1:10, b=c(TRUE,FALSE))

Created by [Link] too

One of the keys to mastering the R is


learning to use the extraction (or subset)
operators effectively.
[ $
Extracting subsets
[

By positive integers, select specied

By negative integers, omit specied

By logical vector, select T, omit F

By character vector (by name)


Subsets can be bigger

a <- c(1, 5, 9)

a[c(1,2,3)]

a[c(1,1,1,2,2,3)]
[ + logical vectors

The most complicated to understand, but


the most powerful

Lets you extract a subset dened by some


characteristic of the data

cancer$Site[cancer$[Link] > 100]

cancer[cancer$[Link] > 100,]


Updating subsets

You can take a subset and update the


original data

a <- 1:4

a[2:3] <- 0

Very useful with logical subsetting


Practice

Select the Race variable in three different


ways

Drop variables [Link],


[Link], and
[Link] from the dataset

Replace all ~ by NA

Replace all other symbols for missing data by


NA
More about missings

NA + x = NA, NA * x = NA

x == NA

[Link] returns logical vector, for single vector

[Link] does the same for a


[Link]

Many functions have [Link]


Practice

Remove all missings from the cancer data.


Why might this be a problem?

Remove all records with missing mortality


rate.
Analysing the data

What questions do we have about the data?

Write down questions for 1 min, then get


together with your neighbor and discuss.

What data will you need to try to answer


your questions? What graphics would you
draw in support of your questions?
Discuss again. Be ready to report.
Questions about the
cancer data
Report

Write a short report, which should include:

your question

your expectation before looking at the data

a graphic which answers the question

a conclusion based on the graphic

Print/Email your report (dont forget to put


your names on it, too)
Homework

Pick one of the Major Findings from


[Link]
facts_major_ndings.htm
and nd a graphic which supports this
nding

Write up a paragraph about what else the


graphic also shows

You might also like