- Relational Operators in R
- Logical Operators in R
- Conditional Statements in R
- For Loop in R Programming
- While and Repeat Loop in R Programming
- Functions in R Programming
- Creating Functions in R
- Apply Functions in R
- Importing Data from External Data Sources in R
- Importing Data Using read.csv in R
- Import Data using read.table in R
- Importing Data Using data.table – fread in R
- Importing Data from Excel in R
- Using XLConnect in R Programming
- Importing Data from a Database in R
- SQL Queries from R
- Importing Data from Web in R
Importing Data from Web in R
While working as a data scientist, you will often deal with data stored on the web. Typically you will have a URl for the data and you will make an HTTP request to fetch the data. The data itself can come in many formats, such as:
- Files such as csv, delimited and excel files
- Data available through APIs. The most popular format of data available through APIs is JSON
- Data available in web tables inside web pages
- And many more
In this lesson, we will look at how to import data in R from files and from APIs. While there are many packages and techniques to get data from the web, we will introduce some of these to you.
Earlier we used
read.csv() from the
utils package to import CSV files stored locally. The
read.util() function is equally efficient to load data from csv files stored on the internet.
For this lesson, we will use a csv files which contains fundamentals of the top 100 stocks in the US. The file is available below:
If you paste this URL directly in a web browser, the browser will simply download the CSV file. To import the data from this file into R, you will need to pass the URL instead of the local file path as shown below:
#Import the csv file into variable 'top_stocks' top_stocks <- read.csv("http://financetrain.com/assets/course/data/top-100-stocks.csv") #print top-stocks top_stocks
This content is for paid members only.
Join our membership for lifelong unlimited access to all our data science learning content and resources.