Sunday, August 2, 2015

R Programming - Reading from a csv file

Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html 
 



Basic requirement would be to read data from a CSV file into a data frame.

Command to read from a csv file?
csv_file <-read.csv("file_name.csv",TRUE,",")

What are the input parameters?
The input parameters to the read.csv functionis
  • The file name from the working directory (or a full URL)
  • If the first line of the csv contains the header, use TRUE else FALSE
  • The delimiter. In case of csv its a comma (,)
How to find the working directory?
Use the command getwd()

How to change the working directory?
Use the setwd(dir) for the same
e.g setwd("C:/TestFolder")

 

I have saved the following csv in the working directory





To read the same, I will use the following command
csv_file <-read.csv("test.csv",TRUE,",")



Note that I have used the head and tail command to retrieve a snapshot (6 rows)

One can also use a URL to read a file

csv_file <-read.csv("file_url\file_name.csv",TRUE,",")


Refer index page for R at the following URL:
http://mylearningcafe.blogspot.in/2015/07/r-programming-index-page.html 
 

No comments:

Post a Comment