You are on page 1of 3

Import file to R – read.

table()
• read.table() reads a file in a table format
and creates a dataframe from it.

• Usage
Data Management > read.table(file, header=FALSE,
sep=“”, …)
and Statistical analysis
# file – name of the file
# header – a logical value indicating
whether the file contains the names of
LOADING DATA the variables as its first line
# sep – field separator character

Changing the Default Directory Changing the Default Directory


1. Change the default working directory 2. Change the default working directory (More temporarily)
• Right-click on your R shortcut → • From the main window of R, select File → Change dir
Select Properties • Change the starting directory from where your files are saved
(eg. R-OBJECT/ My RData)
• Opposite the “Start in”, change the
starting directory from where your • Click Ok.
files are saved (eg. R-OBJECT/ My
RData)

• Click Apply
Loading a .xls File : odbcConnectExcel() Loading a .CSV Text File : read.table()
• Open connections to ODBC data in excel file • read.table() reads a data file in a table format and
• The following scripts reads data from excel file using creates a dataframe from it.
RODBC package • The following scripts reads data from a .csv text file using
> # Reads the data from an excel table or query the read.table utility
> library(RODBC);
> cname<-odbcConnectExcel(“filename.xls"); > # Reads the data from .csv text file
> qname <- "SELECT * FROM Sheet_name$;" > dframe_name <- read.table(“filename.CSV",
> dframe_name <- sqlQuery(cname, qname); > sep = ",",
> odbcClose(cname); > header = TRUE);

# odbcClose() close connection to ODBC database

Loading a .mdb database File :


Some useful commands
odbcConnect Access()
• View objects available in workspace
• Open connections to ODBC databases > ls()
• The following scripts reads data from Access database [1] "a" "a1" "connect" "ids" "Irrig" "newtra"
using RODBC package [7] "query" "RF_early" "RF_late" "srdta" "srdta1" "x"

> # Reads the data from an access table or query • Remove an object
> cname<-odbcConnectAccess(“filename.mdb"); > rm()
> qname <- "SELECT * FROM Sheet_name$;" > rm(newtra)
> dframe_name <- sqlQuery(cname, qname);
> odbcClose(cname); • Remove all objects
> rm(list=ls())
> # Checks if the data is available in R
> is.data.frame(dframe_name);
Some useful commands : str() Some useful commands
• Displays the structure of an R object • List contents of an object
> str(datasetname) > datasetname
> str(RF_late) > RF_late
'data.frame': 24 obs. of 8 variables: LINE REP RLD0 RLD15 RMD0 RMD15 TTLRW BMSS
$ LINE : int 1 1 1 2 2 2 3 3 3 4 ... 1 1 1 2.710 0.329 0.193 0.008 30.18 NA
$ REP : int 1 2 3 1 2 3 1 2 3 1 ... 2 1 2 6.086 0.434 0.298 0.019 47.49 NA
$ RLD0 : num 2.71 6.09 12.12 7.07 4.6 ... 3 1 3 12.116 1.084 1.391 0.040 216.71 NA
$ RLD15: num 0.329 0.434 1.084 0.462 0.496 ... . . .
$ RMD0 : num 0.193 0.298 1.391 0.313 0.153 ... 24 8 3 14.324 0.763 0.860 0.023 132.42 72.68
$ RMD15: num 0.008 0.019 0.04 0.014 0.02 0.008
0.008 0.007 0.012 0.014 ...
$ TTLRW: num 30.2 47.5 216.7 49 25.9 ...
$ BMSS : num NA NA NA 15.7 10.8 ...

Saving and restoring workspace


• To save all the results of calculations done, the
functions created, and the packages loaded in the
memory
Click File/Save Workspace THANK YOU!
Please do Exercise A ☺
• To load workspace
Click File/Load Workspace

You might also like