Importing Data in R Script
Last Updated :
05 Jul, 2023
We can read external datasets and operate with them in our R environment by importing data into an R script. R offers a number of functions for importing data from various file formats.
In this article, we are going to see how to Import data in R Programming Language.
Importing Data in R
First, let’s consider a data set that we can use for the demonstration. For this demonstration, we will use two examples of a single dataset, one in .csv form and another .txt
Importing Data in R Script
Reading a Comma-Separated Value(CSV) File
Method 1: Using read.csv() Function Read CSV Files into R
The function has two parameters:
- file.choose(): It opens a menu to choose a CSV file from the desktop.
- header: It is to indicate whether the first row of the dataset is a variable name or not. Apply T/True if the variable name is present else put F/False.
Example:
R
data1 <- read.csv ( file.choose (), header=T)
data1
|
Output:
Importing Data in R Script
Method 2: Using read.table() Function
This function specifies how the dataset is separated, in this case we take sep=”, “ as an argument.
Example:
R
data2 <- read.table ( file.choose (), header=T, sep= ", " )
data2
|
Output:
Importing Data in R Script
Reading a Tab-Delimited(txt) File in R Programming Language
Method 1: Using read.delim() Function
The function has two parameters:
- file.choose(): It opens a menu to choose a csv file from the desktop.
- header: It is to indicate whether the first row of the dataset is a variable name or not. Apply T/True if the variable name is present else put F/False.
Example:
R
data3 <- read.delim ( file.choose (), header=T)
data3
|
Output:
Importing Data in R Script
Method 2: Using read.table() Function
This function specifies how the dataset is separated, in this case we take sep=”\t” as the argument.
Example:
R
data4 <- read.table ( file.choose (), header=T, sep= "\t" )
data4
|
Output:
Importing Data in R Script
Using R-Studio
Here we are going to import data through R studio with the following steps.
Steps:
- From the Environment tab click on the Import Dataset Menu.
Importing Data in R Script
- Select the file extension from the option.
Importing Data in R Script
- In the third step, a pop-up box will appear, either enter the file name or browse the desktop.
- The selected file will be displayed on a new window with its dimensions.
- In order to see the output on the console, type the filename.
Read JSON Files Into R
In order to work with JSON files in R, one needs to install the “rjson” package. The most common tasks done using JSON files under rjson packages are as follows:
- Install and load the rjson package in R console
- Create a JSON file
- Reading data from JSON file
- Write into JSON file
- Converting the JSON data into Dataframes
- Working with URLs
JSON file for demonstration:
{
"ID":["1","2","3","4","5"],
"Name":["Mithuna","Tanushree","Parnasha","Arjun","Pankaj"],
"Salary":["722.5","815.2","1611","2829","843.25"],
"StartDate":["6/17/2014","1/1/2012","11/15/2014","9/23/2013","5/21/2013"],
"Dept":["IT","IT","HR","Operations","Finance"]
}
Code:
R
library ( "rjson" )
result <- fromJSON (file = "E:\\example.json" )
print (result)
|
Output:
$ID
[1] "1" "2" "3" "4" "5"
$Name
[1] "Mithuna" "Tanushree" "Parnasha" "Arjun" "Pankaj"
$Salary
[1] "722.5" "815.2" "1611" "2829" "843.25"
$StartDate
[1] "6/17/2014" "1/1/2012" "11/15/2014" "9/23/2013" "5/21/2013"
$Dept
[1] "IT" "IT" "HR" "Operations" "Finance"
Share your thoughts in the comments
Please Login to comment...