3
LOADING DATA LOADING DATA LOADING DATA LOADING DATA Data Management and Statistical analysis Import f ile to R – read.table() • read.table() reads a file in a table format and creates a dataframe from it. Usage > read.table(file, header=FALSE, sep=“”, …) # file – name of the file # header a logical value indicating whether the file contains the names of the variables as its first line # sep field separator character 1. Change the def ault working directory Right -cli ck on your R shortcut Select Properties Opposi te the “Start in”, change the starting directory from where your files are saved (eg. R-OBJECT/ My RData) Cl ick Apply Changing the Default Directory 2. Change the default working directory (More temporarily) From the main window of R, select Fil e Change dir Change the starting directory from where your files are saved (eg. R-OBJECT/ My RData) Cl ick Ok. Changing the Default Directory

Data Management and Statistical Analysis - Loading data

Embed Size (px)

Citation preview

Page 1: Data Management and Statistical Analysis - Loading data

8/8/2019 Data Management and Statistical Analysis - Loading data

http://slidepdf.com/reader/full/data-management-and-statistical-analysis-loading-data 1/3

LOADING DATALOADING DATALOADING DATALOADING DATA

Data Managementand Statistical analysis

Import file to R – read.table()

• read.table() reads a file in a table format

and creates a dataframe from it.

• Usage

> read.table(file, header=FALSE,

sep=“”, …)

# file – name of the file

# header – a logical value indicating

whether the file contains the names of

the variables as its first line

# sep – field separator character

1. Change the default working directory

• Right-click on your R shortcut→Select Properties

• Opposite the “Start in”, change the

starting directory from where yourfiles are saved (eg. R-OBJECT/ My

RData)

• Click Apply

Changing the Default Directory

2. Change the default working directory (More temporarily)

• From the main window of R, select File→ Change dir

• Change the starting directory from where your files are saved

(eg. R-OBJECT/ My RData)

• Click Ok.

Changing the Default Directory

Page 2: Data Management and Statistical Analysis - Loading data

8/8/2019 Data Management and Statistical Analysis - Loading data

http://slidepdf.com/reader/full/data-management-and-statistical-analysis-loading-data 2/3

Loading a .xls File : odbcConnectExcel()

• Open connections to ODBC data in excel file

• The following scripts reads data from excel file using

RODBC package

> # Reads the data from an excel table or query

> library(RODBC);

> cname<-odbcConnectExcel(“filename.xls");

> qname <- "SELECT * FROM Sheet_name$;"

> dframe_name <- sqlQuery(cname, qname);

> odbcClose(cname);

# odbcClose() close connection to ODBC database

Loading a .CSV Text File : read.table()

> # Reads the data from .csv text file

> dframe_name <- read.table(“filename.CSV",

> sep = ",",

> header = TRUE);

• read.table() reads a data file in a table format and

creates a dataframe from it.

• The following scripts reads data from a .csv text file usingthe read.table utility

Loading a .mdb database File :odbcConnect Access()

• Open connections to ODBC databases

• The following scripts reads data from Access database

using RODBC package

> # Reads the data from an access table or query

> cname<-odbcConnectAccess(“filename.mdb");

> qname <- "SELECT * FROM Sheet_name$;"

> dframe_name <- sqlQuery(cname, qname);

> odbcClose(cname);

> # Checks if the data is available in R

> is.data.frame(dframe_name);

• View objects available in workspace

> ls()

Some useful commands

[1] "a" "a1" "connect" "ids" "Irrig" "newtra"

[7] "query" "RF_early" "RF_late" "srdta" "srdta1" "x"

• Remove an object

> rm()

> rm(newtra)

• Remove all objects

> rm(list=ls())

Page 3: Data Management and Statistical Analysis - Loading data

8/8/2019 Data Management and Statistical Analysis - Loading data

http://slidepdf.com/reader/full/data-management-and-statistical-analysis-loading-data 3/3

Some useful commands : str()

• Displays the structure of an R object

> str(datasetname)

> str(RF_late)

'data.frame': 24 obs. of 8 variables:

$ LINE : int 1 1 1 2 2 2 3 3 3 4 ...

$ REP : int 1 2 3 1 2 3 1 2 3 1 ...

$ RLD0 : num 2.71 6.09 12.12 7.07 4.6 ...

$ RLD15: num 0.329 0.434 1.084 0.462 0.496 ...

$ RMD0 : num 0.193 0.298 1.391 0.313 0.153 ...

$ 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 ...

Some useful commands

• List contents of an object

> datasetname

> RF_late

LINE REP RLD0 RLD15 RMD0 RMD15 TTLRW BMSS

1 1 1 2.710 0.329 0.193 0.008 30.18 NA

2 1 2 6.086 0.434 0.298 0.019 47.49 NA

3 1 3 12.116 1.084 1.391 0.040 216.71 NA

. . .

24 8 3 14.324 0.763 0.860 0.023 132.42 72.68

• To save all the results of calculations done, the

functions created, and the packages loaded in the

memory

Click File/Save Workspace

• To load workspace

Click File/Load Workspace

Saving and restoring workspace

THANK YOU!Please do Exercise A ☺☺☺☺