Import files in r


Published on

Published in: Technology
  • Be the first to comment

Import files in r

  1. 1. Importing Files in R Codes are in Blue. For Feedback Mail me:
  2. 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:
  3. 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:
  4. 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:
  5. 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:
  6. 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", For Feedback Mail me:
  7. 7. Table File or TXT File • mydata = read.table("mydata.txt") # read text file • mydata # print data frame For Feedback Mail me:
  8. 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:
  9. 9. THANK YOU. Reference: For Feedback Mail me: