Vous êtes sur la page 1sur 9

Importing & Exporting Data in R

Hukum Chandra
ICAR-National Fellow & Principal Scientist
Email: hchandra@iasri.res.in

ICAR-Indian Agricultural Statistics Research Institute


Library Avenue, PUSA, New Delhi, India
www.iasri.res.in
Reading Data
Importing TXT files
If you have a .txt or a tab-delimited text file, you can import it with the basic
R function read.table()
Most important and commonly used function to import simple data files into R

mydata <- read.table("<FileName>.txt", header = FALSE)


? read.table
read.table(file, header = FALSE, sep = "", quote = "\"'", dec = ".", numerals = c ("allow.loss",
"warn.loss", "no.loss"), row.names, col.names, na.strings = "NA", .)

Importing CSV Files


read.csv()
mydata <- read.csv("<FileName>.csv", header = FALSE)
mydata <- read.table("<FileName>.csv", header = FALSE, sep = ",")

?read.csv
read.csv(file, header = TRUE, sep = ",", quote="\"", dec=".", fill = TRUE,
comment.char="", ...)
2
Importing Excel Files into R

install.packages("<name of the package>") # Install package


library("<name of the package>") # Load package

Use readxl package


The readxl package comes with function read_excel() to read xls and xlsx files

Instaling and loading readxl package

install.packages("readxl")
library(readxl)

Read xlsx Dataset


mydata=read_excel("filename.xlsx",sheet=1)

Read xls Dataset


mydata=read_excel("filename.xls",sheet=1)

3
Specify sheet with a number or name

# Specify sheet by its name


mydata<-read_excel("myfile.xlsx", sheet="data")

# Specify sheet by its index


mydata<-read_excel("my_file.xlsx", sheet=2)

Case of missing values: NA (not available). If NAs are represented by


something (example: ) other than blank cells, set the na argument

mydata<-read_excel("myfile.xlsx", na="---")

4
Reading Data in Other Format
Read SPSS Dataset
library (foreign)
MySpssdata=read.spss(file="yielddata.sav", use.value.labels=True,
to.data.frame=True)

Read STAT Dataset


library (foreign)
MyStatdata=read.dta(file="yielddata.dta")

Check
?read.spss

Other functions include, scan, read.csv, read.csv2, read.delim, read.delim2


etc.

5
Writing Data to Files
setwd("<location of your dataset>")

To A Tab Delimited Text File


write.table(Result, "c:/mydata.txt", sep="\t")

Exporting to CSV File Format


write.csv(results, file = "filename.csv")

To an Excel Spreadsheet
library(xlsx)
write.xlsx(mydata, "c:/mydata.xlsx")

write(Results," H:/ R Course / MyResults2.txt")


write(Results," H:/R Course/ MyResults2.txt", ncolumns=2)

How to save R workshop


save.image("H:/R Course /myworshop.RData")

6
How to load R workshop
##know where your working directory is set at the moment:
getwd()
## Set or change your working directory
setwd("<location of your dataset>")
## set working directory
setwd("E:\\R Course")
## Specify the package
library (MASS)

## Reading ASCII Format


mydata=read.table("H:/R Course /yielddata.txt")
dim(mydata)
summary(mydata)

7
mydata=read.table("yielddata.txt",header=T)
dim(mydata)
summary(mydata)
names(mydata)

mydata=read.table(file="yielddata.txt",header=T)
dim(mydata)
summary(mydata)
names(mydata)
[1] "Dist" "Yield" "MARG_HH_F" "HH_SIZE"
[5] "NetArea" "Cropedarea" "Netirrig" "GrossIrrigated"
[9] "Rainfall" "Fert"

mydata$Yield
mydata$Fert

mydata1=data.frame(mydata)
mydata1$Yield
mydata1$Fert

8
## Extract district with yield less than median yield
mydata1$Yield[mydata1$Yield<median(mydata1$Yield)]

## Extract data with yield less than median yield


mydata2=mydata1[mydata1$Yield<median(mydata1$Yield)]

mydata2=mydata1[mydata1$Yield<median(mydata1$Yield),]
dim(mydata2)

Vous aimerez peut-être aussi