0% found this document useful (0 votes)
8 views17 pages

6 Input, Output, Connections

The document provides an overview of input/output features in R, including how to set and identify the working directory, and how to connect to various file types such as rda, txt, csv, and Excel. It includes instructions for exporting and importing data, reading from files, and writing data to new files. Additionally, it covers how to read data from a URL and emphasizes the importance of using specific functions for these tasks.

Uploaded by

KwongYew Ngu
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)
8 views17 pages

6 Input, Output, Connections

The document provides an overview of input/output features in R, including how to set and identify the working directory, and how to connect to various file types such as rda, txt, csv, and Excel. It includes instructions for exporting and importing data, reading from files, and writing data to new files. Additionally, it covers how to read data from a URL and emphasizes the importance of using specific functions for these tasks.

Uploaded by

KwongYew Ngu
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
You are on page 1/ 17

Input/output and connection

Session outcomes
To understand and use:

● Input output features


● Working directory
● Connections (input and output to rda, txt, csv, Excel, url files)
Working directory
● R will default to a working directory, which you can identify using the get
working directory function:

> getwd( )
Working directory
● To set a new connection directory, use the set working directory function

> my_md <- “C://Users…/dir/” (copy the path from your directory browser)

> setwd( my_wd)


Connections

● You can export data objects from the R environment using the save function

> x <- c(1,2,3)

> save(x, “my_x.rda”)


Connections

● To import the saved data, use the load() function

> load(“my_x.rda”)

>x

> y <- get(load(“my_x.rda”))

>y
Connections
● R can also read common data files

Open notepad and write

134

234

567

Then save as “fileA.txt”


Connections
● To scan a working directory for existing files, use list.files()

> file_list <- list.files()

> file_list
Connections
● To read from the file

> filename <- “fileA.txt” or > filename <- list.files( )

> matr <- matrix(scan(filename))

> matr

Also try

> matr <- matrix(scan(filename),nrow=3,byrow=T)

> matr
Connections
● To read from file as a data frame

> df <- read.table(filename)

> df

> df <- read.table(filename, header=FALSE)

> df

Compare

> class(df)

> class(matr)
Connections
● To read from a spreadsheet

Open Excel and write

Then save as a comma separated value file: “fileB.csv”

(save also as an Excel workbook fileB.xlsx for later use)


Connections
● To read from a csv file

> df <- read.csv("fileB.csv")

> df
Connections
● Writing to file

> filename <- paste(getwd(),”new.txt”, sep=”/”)

> write.table(data, filename)


Connections
● Writing to file

> filename <- paste(getwd(),”new.csv”, sep=”/”)

> write.csv(data, filename)


Connections
● To read Excel requires installing a package

> library("readxl")

> my_data <- read_excel("fileB.xlsx")


Connections
● Reading from a url

Go to http://faculty.marshall.usc.edu/gareth-james/ISL/data.html

Right click on a data file and copy link location

> data <- read.csv("http://faculty.marshall.usc.edu/gareth-james/ISL/Auto.csv")

> head(data)
Session outcomes
To understand and use:

● Input output features


● Working directory
● Connections (input and output to rda, txt, csv, Excel, url files)

You might also like