Reading and Writing Data to and from R


Reading files into R

Commonly nosotros will be using data already in a file that nosotros need to read into R in order to work on information technology. R can read data from a diverseness of file formats—for example, files created as text, or in Excel, SPSS or Stata. Nosotros will mainly be reading files in text format .txt or .csv (comma-separated, usually created in Excel).

To read an unabridged data frame directly, the external file volition normally have a special course

  • The outset line of the file should have a proper name for each variable in the data frame.
  • Each additional line of the file has every bit its first item a row label and the values for each variable.

Here we use the example dataset called airquality.csv and airquality.txt

Input file form with names and row labels:

Ozone Solar.R * Wind Temp Month Twenty-four hours

1 41 ***** 190 ** 7.4 ** 67 **** 5 ** 1

ii 36 ***** 118 ** viii.0 ** 72 **** v ** 2

iii 12 ***** 149 * 12.6 ** 74 **** 5 ** iii

4 18 ***** 313 * 11.5 ** 62 **** v ** 4

5 NA ***** NA ** xiv.3 ** 56 **** 5 ** five

   ...

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

The function read.table() can then be used to read the data frame directly

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

Similarly, to read .csv files the read.csv() role tin be used to read in the information frame straight

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

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

 In addition, y'all can read in files using the file.choose() part in R. Subsequently 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 will need to read in data that does not already take column name information.  For example, the dataset BOD.txt looks like this:

1    eight.iii

2   10.three

iii   xix.0

4   sixteen.0

5   15.half-dozen

seven   19.8

Initially, there are no column names associated with the dataset.  We tin utilize the colnames() control to assign column names to the dataset.  Suppose that nosotros want to assign columns, "Time" and "need" to the BOD.txt dataset.  To exercise so nosotros do the following

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

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

> colnames(bod)

[1] "Time"   "demand"

The outset command reads in the dataset, the command "header=F" specifies that in that location are no cavalcade names associated with the dataset.

Read in the cars.txt dataset and call information technology car1.  Make sure you use the "header=F" option to specify that there are no column names associated with the dataset.  Side by side, assign "speed" and "dist" to exist the first and second cavalcade names to the car1 dataset.

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

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

alternative accessible content

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

alternative accessible content

Writing Data to a File


Subsequently working with a dataset, we might like to relieve it for future use. Earlier we exercise this, allow's start ready a working directory then we know where we can find all our data sets and files after.

Setting up a Directory

In the R window, click on "File" and then on "Alter dir". You should then see a box pop up titled "Choose directory". For this class, choose the directory "Desktop" by clicking on "Browse", then select "Desktop" and click "OK". In the hereafter, yous may desire to create a directory on your computer where yous go on your data sets and codes for this class.

Alternatively, you can use the setwd() function to assign as working directory.

> setwd("C:/Desktop")

To observe out what your current working directory is, type

> getwd()

Setting Upwards Working Directories in R (R Tutorial one.viii) MarinStatsLectures [Contents]

alternative accessible content

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

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

The first argument refers to the data frame to be written to the output file, the second is the name of the output file. Past default R will environment each entry in the output file past quotes, and so we use quote=F.

Now, let's check whether R created the file on the Desktop, by going to the Desktop and clicking to open up the file. You should run across a file with three columns, the first giving the index (or row number) and the other ii the speed and altitude. R by default creates a cavalcade of row indices. If we wanted to create a file without the row indices, we would use the command:

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

Datasets in R


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

Working with Variables and Information in R (R Tutorial i.5) MarinStatsLecures [Contents]

alternative accessible content

Around 100 datasets are supplied with R (in the package datasets), and others are bachelor.

To encounter the list of datasets currently available employ the command:

data()

We will get-go look at a data set on CO2 (carbon dioxide) uptake in grass plants available in R.

> CO2

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

To get more than data on the variables in the dataset, type in

> assistance(CO2)

Evaluate and report the hateful and standard divergence of the variables "Concentration" and "Uptake".

Subsetting Data in R With Square Brackets and Logic Statements (R Tutorial one.6) MarinStatsLecures [Contents]

alternative accessible content