Read or Import CSV file in to R

Import CSV file in R

The files are mostly provided in the spreadsheet or excel sheet formats for analysis. The data are prepared by converting files to Comma separated file (CSV).

This article explains the necessary steps to import the CSV file into R and Plotting Graph and Histogram.

 

Step 1: # Import the order details data into sales.

read.csv("/home/vivek/Downloads/sample_orders.csv")
In the above exampple we imported the file Sample_orders.csv into sales 

Analyzing the imported dataset

Step 2: Let see some sample records of imported CSV file.

r>head(sales)
  Row.ID Order.ID Order.Date Order.Priority Order.Quantity
1      1        3 10/13/2010            Low              6
2     49      293  10/1/2012           High             49
3     50      293  10/1/2012           High             27
4     80      483  7/10/2011           High             30
5     85      515  8/28/2010  Not Specified             19
6     86      515  8/28/2010  Not Specified             21

Step 3: To see the summary of imported records.


r> summary(sales) 
     Row.ID        Order.ID          Order.Date         Order.Priority Order.Quantity 
 Min.   :   1   Min.   :    3   3/28/2012 :  20   Critical     :1608   Min.   : 1.00  
 1st Qu.:2100   1st Qu.:15012   9/15/2011 :  20   High         :1768   1st Qu.:13.00  
 Median :4200   Median :29857   12/12/2010:  18   Low          :1720   Median :26.00  
 Mean   :4200   Mean   :29965   11/19/2011:  17   Medium       :1631   Mean   :25.57  
 3rd Qu.:6300   3rd Qu.:44596   2/27/2010 :  17   Not Specified:1672   3rd Qu.:38.00  
 Max.   :8399   Max.   :59973   4/20/2010 :  17                        Max.   :50.00  
Plot Graph Example in R
Plot Graph Example in R

Plot Example in R

Step 4: R provide Plot command to Draw the chart.


r>plot(sales$Sales,sales$Order.Quantity,
+      main="Sales vs Quantity")

Plot Graph Example in R
Plot Graph Example in R

Statistical analysis:

Step 5: To Perform Statistical analysis, R Provides the lm function.

r> results <- lm(sales$Sales ~ sales$Order.Quantity)
> summary(results)

Call:
lm(formula = sales$Sales ~ sales$Order.Quantity)

Residuals:
   Min     1Q Median     3Q    Max 
 -3054  -1618   -795     12  87972 

Coefficients:
                     Estimate Std. Error t value Pr(>|t|)    
(Intercept)           379.431     77.438    4.90 9.77e-07 ***
sales$Order.Quantity   54.609      2.635   20.72  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3497 on 8397 degrees of freedom
Multiple R-squared:  0.04866,   Adjusted R-squared:  0.04854 
F-statistic: 429.5 on 1 and 8397 DF,  p-value: < 2.2e-16



Histogram Example in R

Step 6: Use hist function to draw the Histogram

r> hist(results$residuals, breaks = 800)

Example Histogram In R
Example Histogram In R
Filed in: R

Share this post

Leave a Reply