Data import and export

You can check and set the path representing the current working directory, respectively by using:

getwd()
setwd("mypath/mycodename.r")

There are different options for importing data in R. If your data are in a txt file, you may use the function read.table. Pay attention to the header parameter, if header = TRUE, the first line of your dataset is the header. If you don’t have header in your txt file, set header = FALSE. If your data are stored in an excel file, open the file, save it as a csv file and use the functions read.csv or read.csv2. These two functions are identical to read.table except for the defaults. The function read.cvs() is intended for reading ‘comma separated value’ files (‘.csv’). Its twin function read.csv2() is used when, in the csv file, a comma separates the decimal point and a semicolon as field separator (the choice depends on the Language and Text preferences in the OS). The dataset is stored as data.frame object. The syntax is the following:

#first, check the current working directory, and check that the data file is in the current working directory:
getwd()
my_data = read.table("experiment1.txt", header = TRUE)
#if you are working in a different directory:
my_data = read.table("data_path/experiment1.txt", header = TRUE)

In order to run a script or upload your function saved in a script.r, use the source function

source("mypath/mycodename.r")

You can use the function write.table in order to export an object, such as data frame or a matrix. If you want to export as special object such as a table or a fitted model, see the function xtable form the CRAN-package xtable.

You can save the workspace by using the save.image function. To reload the saved workspace, use the function load. We suggest you to have a look in the R Data Import/Export tutorial, that you can find in the CRAN/Manuals.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s