Importing Data into R | R Programming

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)
Data Types in R |R Programming
Basic Syntax of R Programming

Leave a Comment