Importing Data into R | R Programming

sanket-parate Trending Technology

This is the third part of the series. This article is about how you can import data into R. Importing data into R is simple. Example of Importing data are provided below:

1. Importing CSV in R:
R can read .CSV (Comma separated files). The best way to read .csv file is as follows:

# Read CSV into R
Data <- read.csv(file=”D:/m/1.csv”, header=TRUE, sep=”,”)

Here header=TRUE specifies that this data includes a header row and sep=”,” specifies that the data is separated by commas.

2. Importing Excel File
One of the best ways to read an Excel file is to export it to comma delimited file. You can use the xlsx package to access Excel files. The first row should contain column names.

# If First row contains variable names then
Data <- read.xlsx(“D:/m/1.xlsx”,1)

# Read in the worksheet named mydata
Data <- read.xlsx(“D:/m/1.xlsx”, SheetName=”mydata”)

3. Importing txt File into R

# Reading .txt File as dataframe
Data <- read.delim(“D:/m/1.txt”, sept=”\t”, header=TRUE)

Sanket Parate

Tech enthusiast and an optimistic guy with the mindset to achieve the set target. I am an avid learner and passionate learner. After finding my interest in data science, I felt that 'Yes this is the field which I am looking for. I am more passionate to learn data science and R and looking forward to exploring more in these fields along with the python. I love to share information with people.

Leave a Reply

Your email address will not be published. Required fields are marked *