Importing Data in R Script - GeeksforGeeks

25 Nov, 2021
In this article, we are going to see how to Import data in R Programming Language.
First, let’s consider a data-set which 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
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
# import and store the dataset in data1data1 <- read.csv(file.choose(), header=T) # display the datadata1
Output: 
This function specifies how the dataset is separated, in this case we take sep=”, “ as an argument.
Example: 
R
# import and store the dataset in data2data2 <- read.table(file.choose(), header=T, sep=", ") # display datadata2
Output: 
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
# import and store the dataset in data3data3 <- read.delim(file.choose(), header=T) # display the datadata3
Output: 
This function specifies how the dataset is separated, in this case we take sep=”\t” as the argument.
Example: 
R
# import and store the dataset in data4data4 <- read.table(file.choose(), header=T, sep="\t") # display the datadata4
Output: 
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
Select the file extension from the option
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.
Example:
R
# display the datasetdataset
Output:
In order to load the data onto the console for use, we use the attach command.
Example:
R
# To load the data for useattach(dataset)
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
# Read a JSON file # Load the package required to read JSON files.library("rjson") # Give the input file name to the function.result <- fromJSON(file = "E:\\example.json") # Print the result.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"
nidhi_biet
kumar_satyam
Picked
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change column name of a given DataFrame in R
How to Replace specific values in column in R DataFrame ?
Adding elements in a vector in R programming - append() method
How to change Row Names of DataFrame in R ?
Filter data by multiple conditions in R using Dplyr
Change Color of Bars in Barchart using ggplot2 in R
Loops in R (for, while, repeat)
Convert Factor to Numeric and Numeric to Factor in R Programming
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr