Importing and Exporting in R

From AWF-Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "==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'' ...")
 
(Blanked the page)
 
Line 1: Line 1:
==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")
 

Latest revision as of 17:12, 31 January 2015

Personal tools
Namespaces

Variants
Actions
Navigation
Development
Toolbox
Print/export