Importing and Exporting in R

From AWF-Wiki
Revision as of 15:45, 25 January 2015 by Aknop (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Importing and exporting data in R

R allows data importing from many different source formats. The most common used functions to import data are the read functions. The following code can be used to import a .csv table named "forest-data.csv" from the folder "my-working-folder". The data is stored in an object named data.

data <- read.csv("C:/Program Files/... /my-working-folder/forest-data.csv")

Note that / or \\ instead of \ is used in R to define the the file path in your computer.

A more convenient way of working is defining first the working directory from where the data will be imported and where the result can be stored. The actual working directory can be consulted by getwd(), and the objects contained there by dir(). The working directory can be defined as described below.

setwd("D:/Data/... /R-course/")

Once defined the working directory, the file can be imported without specifying the file path as follows:

data <- read.csv("forest-data.csv")

All the objects created or imported to R are temporary stored in the R-workspace. Objects in the workspace can be removed by using the rm() function. All objects in the workspace are usually lost after finishing the R session. The easiest option to store permanently an object in the working directory is by using the write() functions. The code to save as a .csv file the object d is shown below.

write.csv(d, "Data-saved.csv")

Personal tools
Namespaces

Variants
Actions
Navigation
Development
Toolbox
Print/export