04 Lesson 1B



Original: Anne Laure Buisson
Revised substantially by A.Mani

You can use data from CSV, plain text, databases or R''s native format in R. Data from spreadsheets and other statistical s/w formats can also imported. For importing data you need to make use of the library ''foreign''.

You can use the functions ''read.table'' or ''read.csv'' for reading data in tabular or csv form. The form of the function is:
read.table(file-name, header = FALSE, sep = "", ...)
header=TRUE : If you want R to recognise the first row as header row.

Sep: can be any character used to separate fields: often a comma (sep=”,”), a tab (sep=”\\t”), a space (sep=” ”)

The read.csv function work quite the same, but with header=TRUE by default, and comma as separator.
read.csv(file, header = TRUE, sep = ","...)

Sometimes csv files have no fields delimiters but have fields in pre-specified columns, then you can use: read.delim, or read.fwf.

You can import data from SPSS, SAS… with the recommended package ''foreign''. Use ''Gnumeric'' for converting spreadsheets into csv files.

For discovering all the items linked to the ''read'' function, you can use the apropos function: apropos ("read").

getwd() : will indicate the default working directory.
setwd(dir="PATH") : can be used for changing the default directory

## Exercices ##

>abc <-read.table(...)

1 - Read file stress.csv, with header row and semicolon as separator, using read.table and read.csv

2 - Read files memoire.txt, with header row and tab as separator

3 - Read files pulbsplus.csv with the following options:

Missing values are indicated with ”Blank” (na.strings)
You need to skip empty rows (blank.lines.skip)
Don’t use the first row as header (skip it)
Try importing the 45th row alone

4. Read row names properly

5. Read the same from Rcmdr

6. Write all files with '','' as separator and with column names.