10
Programming in R Getting data into R

Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Embed Size (px)

Citation preview

Page 1: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Programming in R

Getting data into R

Page 2: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Importing data into R

In this session we will learn:• Some basic R commands• How to enter data directly into R• How to use commands to read data from CSV files, tab delimited files, and fixed field files.

Page 3: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

R Data type

• Vectors – a single column (or row )of data– Example – a numeric vector

containing test scores of students• Matrix – a collection of vectors but

must all be of the same data type• Data frame – a special matrix that

can contain both numeric and character columns.

Page 4: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

R commands

• c() – creates a column vector• print – prints the contents of an

object• read.csv – reads in a CSV file• read.table – more generic function

that can read in files with any delimiter, such as tab delimited.

• read.fwf – reads in fixed record formats.

Page 5: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Creating Data in R

• The easiest way to add data to R is to code it.

names <- c("Bob","Gene","Valerie")print(names)age <- c(30, 40, 19)hometown <- c("Dallas, TX", "Little Rock, AR", "Dayton, OH")print(hometown)

Page 6: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Importing Data Into R

• In order to import data into R, we need to know how the file was created.– Excel file– SAS– SPSS– Delimited– Fixed-field or fixed record.

Page 7: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Importing Data Into R

• Base R cannot import data directly from Excel.– There are packages available that

allow R to import directly from Excel.– We will learn about packages in the

next session.

Page 8: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Importing Excel

• Need to save Excel as a CSV file.– Click File/Save As – On the next GUI, select CSV from the

Save as type.– We will demonstrate it in a few

minutes.

Page 9: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

CSV File

Page 10: Programming in R Getting data into R. Importing data into R In this session we will learn: Some basic R commands How to enter data directly into R How

Importing CSV file#~~~~~~~~~~~~~~~~~~~~~~~~## code chunk 2 ## Import CSV File ##~~~~~~~~~~~~~~~~~~~~~~~~#widge <- read.csv("C:\\Path here\\WidgeOne.csv")head(widge)