This is an R Markdown document explaining about different way to impoting data in to R. There are several ways by which we can import different data formats into R; some need support of packages some are directly importable by using inbuild commands of R. In all R support variety of formats.

All R inbuild commands for data importing are starts with read and explains its specifics further. The general command for imporitng all types of data format is read.table and takes three impotant arguments 'file'(path) (where the file is situated), 'header'(logiacal)(does the header exist or not), 'sep'(type of separation).

Example 1) importing text data (tab delimited)

read.table(file = "~/test002.txt", header = TRUE, sep = "\t")
##   x   y
## 1 1 100
## 2 2 200
## 3 3 300
## 4 4 400
## 5 5 500
## 6 6 600
## 7 7 700
## 8 8 800
## 9 9 900

Example 2) importing csv data (comma separated)

my_data_001 <- read.table(file = "~/test001.csv", header = TRUE, sep = ",")
head(my_data_001)
##          Store Total.Sale  Target
## 1  Lifestyle 1     748551 1035825
## 2  Lifestyle 2     630151 1542863
## 3  Lifestyle 3     137482 1364882
## 4  Lifestyle 4     131256 1245889
## 5 Pantaloons 1     354973 1396857
## 6 Pantaloons 2     872624 1245368

We can import the data from various other Analytical Software’s with the help of pacakges:

temp <- read.delim(file = "~/add-on.txt", header = FALSE)
library(knitr)
Q <- c("Analytics_soft", "pack1", "pack2")
kable(temp, caption = "**Table of Soft and their respective packages**",col.names = Q)
Table of Soft and their respective packages
Analytics_soft pack1 pack2
Excel XLConnect xlsx
Minitab foreign -
SPSS foreign Hmisc
SAS foreign Hmisc
Stata foreign -
syStata foreign -

An easy way to import a small data, is to directly read it from the clipboard, just by coping it.‘copy the data’ then call following command :

x <- read.table(file = "clipboard", header = TRUE, sep = "\t")
## Warning in read.table(file = "clipboard", header = TRUE, sep = "\t"):
## incomplete final line found by readTableHeader on 'clipboard'

if your data have just one column (very unsual case) then we can use :

readClipboard()

We already worked with read.table which is in general use for all types of importing formats but we can also use the specific functions like read.csv for comma separated values(,), read.csv2 for semi-colon separated values(;) ,read.delim for tab delimited values and read.fwf for fix width format.

Lets import the fix width file

read.fwf(file = "~/test002.txt", header = TRUE, widths = c(1,-1,3), sep = "\t")
##   x   y
## 1 1 100
## 2 2 200
## 3 3 300
## 4 4 400
## 5 5 500
## 6 6 600
## 7 7 700
## 8 8 800
## 9 9 900

Here, the first row of the data was 1 (space) 100 hence our we set our width as 1(which is width of 1st variable), -1(to ignore the space), 3(which is width of 2nd variable)

All above are the possible ways to import the data into R, but it is highly recommended and encourage that ‘To import the data in CSV format’ to avoid any trouble with the data.

Hence, the read.csv command so popular in R. So lets use,

temp001<- read.csv(file = "~/mtcars.csv", header = TRUE, sep = ",")
head(temp001)
##                   X  mpg cyl disp  hp drat    wt  qsec vs am gear carb
## 1         Mazda RX4 21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## 2     Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## 3        Datsun 710 22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## 4    Hornet 4 Drive 21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## 5 Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## 6           Valiant 18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

This is the end of our first lesson, we almost covered all the function available to import the data into R, but I will update this sheet whenever I found it essential.

** " So enjoy learning R " **