Reading and Writing Information to and from R


Reading files into R

Usually we will be using data already in a file that we need to read into R in order to piece of work on it. R can read data from a multifariousness of file formats—for example, files created as text, or in Excel, SPSS or Stata. We will mainly be reading files in text format .txt or .csv (comma-separated, usually created in Excel).

To read an entire data frame directly, the external file will usually have a special form

  • The start line of the file should have a proper noun for each variable in the data frame.
  • Each additional line of the file has equally its showtime detail a row label and the values for each variable.

Hither we utilise the case dataset called airquality.csv and airquality.txt

Input file class with names and row labels:

Ozone Solar.R * Current of air Temp Month Day

1 41 ***** 190 ** 7.iv ** 67 **** 5 ** 1

ii 36 ***** 118 ** 8.0 ** 72 **** 5 ** 2

three 12 ***** 149 * 12.6 ** 74 **** 5 ** 3

4 eighteen ***** 313 * xi.five ** 62 **** 5 ** 4

v NA ***** NA ** xiv.3 ** 56 **** 5 ** 5

   ...

By default numeric items (except row labels) are read as numeric variables. This can exist inverse if necessary.

The function read.tabular array() can so be used to read the information frame directly

     > airqual <- read.table("C:/Desktop/airquality.txt")

Similarly, to read .csv files the read.csv() function can be used to read in the information frame direct

[Note: I have noticed that occasionally yous'll need to practice a double slash in your path //. This seems to depend on the machine.]

> airqual <- read.csv("C:/Desktop/airquality.csv")

 In addition, yous can read in files using the file.cull() function in R. Afterwards typing in this control in R, you can manually select the directory and file where your dataset is located.

  1. Read the airquality.csv file into R using the read.csv command.
  2. Read the airquality.txt file into R using the file.choose() command

Occasionally, you volition need to read in information that does not already have column name information.  For case, the dataset BOD.txt looks like this:

i    8.3

2   x.3

3   19.0

4   16.0

5   xv.vi

7   19.8

Initially, there are no column names associated with the dataset.  We can use the colnames() command to assign cavalcade names to the dataset.  Suppose that nosotros want to assign columns, "Time" and "demand" to the BOD.txt dataset.  To do then we do the following

> bod <- read.table("BOD.txt", header=F)

> colnames(bod) <- c("Time","need")

> colnames(bod)

[i] "Time"   "demand"

The first command reads in the dataset, the control "header=F" specifies that there are no column names associated with the dataset.

Read in the cars.txt dataset and call it car1.  Brand sure you use the "header=F" selection to specify that there are no cavalcade names associated with the dataset.  Next, assign "speed" and "dist" to be the first and second cavalcade names to the car1 dataset.

The ii videos below provide a prissy explanations of different methods to read information from a spreadsheet into an R dataset.

Import Information, Copy Data from Excel to R, Both .csv and .txt Formats (R Tutorial 1.3) MarinStatsLectures [Contents]

alternative accessible content

Importing Data and Working With Data in R (R Tutorial 1.iv) MarinStatsLectures [Contents]

alternative accessible content

Writing Data to a File


After working with a dataset, nosotros might like to save information technology for future use. Earlier we practice this, permit's first set up a working directory then we know where we tin can discover all our data sets and files later.

Setting up a Directory

In the R window, click on "File" and then on "Change dir". You should then see a box popular upwards titled "Cull directory". For this form, choose the directory "Desktop" by clicking on "Browse", so select "Desktop" and click "OK". In the future, y'all may want to create a directory on your computer where you keep your data sets and codes for this grade.

Alternatively, y'all can utilize the setwd() function to assign as working directory.

> setwd("C:/Desktop")

To find out what your current working directory is, blazon

> getwd()

Setting Up Working Directories in R (R Tutorial 1.eight) MarinStatsLectures [Contents]

alternative accessible content

In R, nosotros can write data frames easily to a file, using the write.table() command.

> write.table(cars1, file=" cars1.txt ", quote=F)

The starting time argument refers to the data frame to exist written to the output file, the second is the proper noun of the output file. By default R volition surround each entry in the output file by quotes, so nosotros use quote=F.

Now, let'south bank check whether R created the file on the Desktop, past going to the Desktop and clicking to open the file. You should encounter a file with iii columns, the starting time giving the index (or row number) and the other two the speed and altitude. R by default creates a column of row indices. If we wanted to create a file without the row indices, we would use the control:

> write.table(cars1, file=" cars1.txt ", quote=F, row.names=F)

Datasets in R


Watch the video below for a curtailed intoduction to working with the variables in an R dataset

Working with Variables and Data in R (R Tutorial one.5) MarinStatsLecures [Contents]

alternative accessible content

Around 100 datasets are supplied with R (in the parcel datasets), and others are available.

To see the listing of datasets currently available use the control:

data()

We will showtime look at a information set on CO2 (carbon dioxide) uptake in grass plants bachelor in R.

> CO2

[ Notation: capitalization matters here; also: information technology'south the letter O, not cypher. Typing this command should display the entire dataset called CO2, which has 84 observations (in rows) and 5 variables (columns).]

To get more information on the variables in the dataset, blazon in

> help(CO2)

Evaluate and report the mean and standard departure of the variables "Concentration" and "Uptake".

Subsetting Data in R With Square Brackets and Logic Statements (R Tutorial 1.half dozen) MarinStatsLecures [Contents]

alternative accessible content