1. Importing Files in R
Codes are in Blue.
For Feedback Mail me: sharmakarishma91@gmail.com
2. Importing files
• To import different types of files in R, we must install and load
a foreign package. So, first you install and load foreign
package into R console window
• library(foreign)
For Feedback Mail me: sharmakarishma91@gmail.com
3. Excel File (XLS file)
• library(gdata) # load the gdata package
• help(read.xls) # documentation
• mydata = read.xls("mydata.xls") # read from first sheet
For Feedback Mail me: sharmakarishma91@gmail.com
4. Excel File (XLSX file)
• We have to first install the packages XLSX and XLSXJARS to
import XLSX file to R
Example Code:
• library(xlsx)
• library(xlsxjars)
• data1<-read.xlsx(“D:data1.xlsx”,1)
For Feedback Mail me: sharmakarishma91@gmail.com
5. CSV File
• One good trick to import CSV file
• file=read.csv(file.choose())
• You can directly import file using browse.
• Syntax:
read.csv(file, header = TRUE, sep = ",", quote=""", dec=".", fill
= TRUE, comment.char="", ...)
• mydata = read.csv("mydata.csv")
# read csv file
• mydata # print data frame
For Feedback Mail me: sharmakarishma91@gmail.com
6. SPSS FILE
• SPSS can be opened with the read.spss function from the
foreign package
• library(foreign) # load the foreign package
• help(read.spss) # documentation
• mydata=read.spss("myfile", to.data.frame=TRUE)
For Feedback Mail me: sharmakarishma91@gmail.com
7. Table File or TXT File
• mydata = read.table("mydata.txt") # read text file
• mydata # print data frame
For Feedback Mail me: sharmakarishma91@gmail.com
8. Package Selection
• It is always good to check the PDF file of the package and in
the packages PDF file check the dependencies of the Package
with other packages so that we can easily know which
packages will be required to call.
• Some commonly used packages
• Foreign- to import file
• Gdata- to import XLS file
• Xlsx, xlsxjars- to import XLSX file
• Attach()- to attach file before doing Regression.
For Feedback Mail me: sharmakarishma91@gmail.com