Skip to content Skip to sidebar Skip to footer

How to Read an Ods File Into R

Reading and Writing Data to and from R


Reading files into R

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

To read an entire data frame directly, the external file volition normally have a special form

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

Hither 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 Day

one 41 ***** 190 ** seven.4 ** 67 **** 5 ** i

ii 36 ***** 118 ** 8.0 ** 72 **** 5 ** two

3 12 ***** 149 * 12.vi ** 74 **** 5 ** three

4 18 ***** 313 * eleven.5 ** 62 **** five ** 4

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

   ...

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

The function read.table() can then exist used to read the data 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 data frame directly

[Notation: 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, you can read in files using the file.choose() role in R. After typing in this command in R, y'all 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() control

Occasionally, yous will demand to read in information that does not already accept column name information.  For example, the dataset BOD.txt looks like this:

one    eight.three

2   x.3

three   19.0

four   16.0

five   15.half-dozen

7   xix.viii

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 we want to assign columns, "Fourth dimension" and "demand" to the BOD.txt dataset.  To practice so we do the following

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

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

> colnames(bod)

[one] "Time"   "need"

The outset command reads in the dataset, the command "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 yous apply the "header=F" pick to specify that there are no column names associated with the dataset.  Next, assign "speed" and "dist" to exist the commencement and second column names to the car1 dataset.

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

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

alternative accessible content

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

alternative accessible content

Writing Data to a File


After working with a dataset, we might like to relieve it for futurity apply. Before we do this, permit'south first set up a working directory so we know where we tin discover all our data sets and files afterwards.

Setting up a Directory

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

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

> setwd("C:/Desktop")

To detect out what your current working directory is, blazon

> getwd()

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

alternative accessible content

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

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

The first statement refers to the data frame to be written to the output file, the second is the name of the output file. By default R will surround each entry in the output file by quotes, so we use quote=F.

Now, let'southward check whether R created the file on the Desktop, by going to the Desktop and clicking to open the file. You lot should come across a file with three columns, the first giving the alphabetize (or row number) and the other two the speed and distance. R by default creates a column of row indices. If we wanted to create a file without the row indices, we would use the command:

> write.tabular array(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 Data in R (R Tutorial i.5) MarinStatsLecures [Contents]

alternative accessible content

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

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

data()

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

> CO2

[ Notation: capitalization matters hither; too: information technology's the letter O, not zero. Typing this command should display the entire dataset chosen CO2, which has 84 observations (in rows) and 5 variables (columns).]

To go more than information on the variables in the dataset, type in

> help(CO2)

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

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

alternative accessible content

phillipswasseen.blogspot.com

Source: https://sphweb.bumc.bu.edu/otlt/MPH-Modules/BS/R/R1_GettingStarted/R1_GettingStarted8.html

Post a Comment for "How to Read an Ods File Into R"