Import flat files from the web

read.csv() and read.delim() are capable of automatically importing from URLs that point to flat files on the web. You must be wondering whether Hadley Wickham’s alternative package, readr, is equally potent.

# Load the readr package
library(readr)
## Warning: package 'readr' was built under R version 3.2.5
# Import the csv file: pools
url_csv <- "http://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/swimming_pools.csv"
pools <- read_csv(url_csv)

# Import the txt file: potatoes
url_delim <- "http://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/potatoes.txt"
potatoes <- read_tsv(url_delim)

# Print pools and potatoes
pools
##                                         Name
## 1                Acacia Ridge Leisure Centre
## 2                            Bellbowrie Pool
## 3                                Carole Park
## 4                Centenary Pool (inner City)
## 5                             Chermside Pool
## 6                Colmslie Pool (Morningside)
## 7             Spring Hill Baths (inner City)
## 8                 Dunlop Park Pool (Corinda)
## 9                      Fortitude Valley Pool
## 10 Hibiscus Sports Complex (upper MtGravatt)
## 11                 Ithaca Pool ( Paddington)
## 12                             Jindalee Pool
## 13                                Manly Pool
## 14            Mt Gravatt East Aquatic Centre
## 15       Musgrave Park Pool (South Brisbane)
## 16                            Newmarket Pool
## 17                              Runcorn Pool
## 18                             Sandgate Pool
## 19      Langlands Parks Pool (Stones Corner)
## 20                         Yeronga Park Pool
##                                        Address  Latitude Longitude
## 1           1391 Beaudesert Road, Acacia Ridge -27.58616  153.0264
## 2                 Sugarwood Street, Bellbowrie -27.56547  152.8911
## 3   Cnr Boundary Road and Waterford Road Wacol -27.60744  152.9315
## 4             400 Gregory Terrace, Spring Hill -27.45537  153.0251
## 5                 375 Hamilton Road, Chermside -27.38583  153.0351
## 6                 400 Lytton Road, Morningside -27.45516  153.0789
## 7             14 Torrington Street, Springhill -27.45960  153.0215
## 8                      794 Oxley Road, Corinda -27.54652  152.9806
## 9         432 Wickham Street, Fortitude Valley -27.45390  153.0368
## 10         90 Klumpp Road, Upper Mount Gravatt -27.55183  153.0735
## 11               131 Caxton Street, Paddington -27.46226  153.0103
## 12                 11 Yallambee Road, Jindalee -27.53236  152.9427
## 13                  1 Fairlead Crescent, Manly -27.45228  153.1874
## 14 Cnr wecker Road and Newnham Road, Mansfield -27.53214  153.0943
## 15       100 Edmonstone Street, South Brisbane -27.47978  153.0168
## 16                71 Alderson Stret, Newmarket -27.42968  153.0062
## 17                   37 Bonemill Road, Runcorn -27.59156  153.0764
## 18               231 Flinders Parade, Sandgate -27.31196  153.0691
## 19             5 Panitya Street, Stones Corner -27.49769  153.0487
## 20                     81 School Road, Yeronga -27.52053  153.0185
potatoes
##     area temp size storage method texture flavor moistness
## 1      1    1    1       1      1     2.9    3.2       3.0
## 2      1    1    1       1      2     2.3    2.5       2.6
## 3      1    1    1       1      3     2.5    2.8       2.8
## 4      1    1    1       1      4     2.1    2.9       2.4
## 5      1    1    1       1      5     1.9    2.8       2.2
## 6      1    1    1       2      1     1.8    3.0       1.7
## 7      1    1    1       2      2     2.6    3.1       2.4
## 8      1    1    1       2      3     3.0    3.0       2.9
## 9      1    1    1       2      4     2.2    3.2       2.5
## 10     1    1    1       2      5     2.0    2.8       1.9
## 11     1    1    1       3      1     1.8    2.6       1.5
## 12     1    1    1       3      2     2.0    2.8       1.9
## 13     1    1    1       3      3     2.6    2.6       2.6
## 14     1    1    1       3      4     2.1    3.2       2.1
## 15     1    1    1       3      5     2.5    3.0       2.1
## 16     1    1    1       4      1     2.6    3.1       2.4
## 17     1    1    1       4      2     2.7    2.9       2.4
## 18     1    1    1       4      3     2.2    3.1       2.3
## 19     1    1    1       4      4     3.1    3.4       2.7
## 20     1    1    1       4      5     3.0    2.6       2.7
## 21     1    1    2       1      1     3.1    3.0       2.8
## 22     1    1    2       1      2     2.7    2.8       2.7
## 23     1    1    2       1      3     2.4    3.0       2.9
## 24     1    1    2       1      4     2.2    2.9       2.3
## 25     1    1    2       1      5     1.9    2.9       2.0
## 26     1    1    2       2      1     1.8    2.6       1.8
## 27     1    1    2       2      2     2.2    2.9       2.1
## 28     1    1    2       2      3     2.8    3.2       2.8
## 29     1    1    2       2      4     2.3    3.2       2.4
## 30     1    1    2       2      5     2.0    3.0       2.0
## 31     1    1    2       3      1     1.9    3.0       1.8
## 32     1    1    2       3      2     1.8    2.7       1.8
## 33     1    1    2       3      3     3.3    3.2       3.2
## 34     1    1    2       3      4     2.5    3.1       2.2
## 35     1    1    2       3      5     2.5    3.4       2.3
## 36     1    1    2       4      1     1.5    2.6       1.3
## 37     1    1    2       4      2     1.4    2.6       1.3
## 38     1    1    2       4      3     2.1    2.5       2.0
## 39     1    1    2       4      4     1.8    3.1       1.7
## 40     1    1    2       4      5     1.7    2.7       1.7
## 41     1    2    1       1      1     2.8    2.6       3.0
## 42     1    2    1       1      2     2.5    2.4       2.8
## 43     1    2    1       1      3     3.2    2.7       3.2
## 44     1    2    1       1      4     2.4    2.4       2.6
## 45     1    2    1       1      5     2.0    2.5       2.2
## 46     1    2    1       2      1     2.3    2.9       1.9
## 47     1    2    1       2      2     2.8    2.7       2.5
## 48     1    2    1       2      3     3.7    3.3       3.1
## 49     1    2    1       2      4     2.8    2.7       2.5
## 50     1    2    1       2      5     2.6    2.6       2.3
## 51     1    2    1       3      1     2.4    2.7       2.0
## 52     1    2    1       3      2     2.7    2.5       2.1
## 53     1    2    1       3      3     2.7    2.9       2.7
## 54     1    2    1       3      4     2.6    2.8       2.2
## 55     1    2    1       3      5     2.6    2.8       1.8
## 56     1    2    1       4      1     3.0    3.2       2.4
## 57     1    2    1       4      2     3.1    3.1       2.8
## 58     1    2    1       4      3     3.6    3.0       3.3
## 59     1    2    1       4      4     3.4    3.4       2.9
## 60     1    2    1       4      5     2.7    3.0       2.6
## 61     1    2    2       1      1     2.2    2.6       2.6
## 62     1    2    2       1      2     2.3    2.3       2.4
## 63     1    2    2       1      3     2.7    2.6       2.7
## 64     1    2    2       1      4     2.0    2.5       2.0
## 65     1    2    2       1      5     1.4    2.4       1.7
## 66     1    2    2       2      1     2.5    2.6       1.9
## 67     1    2    2       2      2     3.2    2.9       2.9
## 68     1    2    2       2      3     3.0    3.1       1.9
## 69     1    2    2       2      4     2.6    2.8       2.7
## 70     1    2    2       2      5     2.6    3.1       2.2
## 71     1    2    2       3      1     2.4    3.0       2.2
## 72     1    2    2       3      2     2.8    2.8       2.5
## 73     1    2    2       3      3     3.3    3.1       3.1
## 74     1    2    2       3      4     2.8    3.1       2.6
## 75     1    2    2       3      5     2.9    2.9       2.4
## 76     1    2    2       4      1     1.4    2.9       1.4
## 77     1    2    2       4      2     2.1    2.5       1.6
## 78     1    2    2       4      3     2.3    2.6       1.8
## 79     1    2    2       4      4     1.8    2.9       1.6
## 80     1    2    2       4      5     1.5    2.4       1.5
## 81     2    1    1       1      1     2.5    2.7       2.6
## 82     2    1    1       1      2     2.8    2.9       2.7
## 83     2    1    1       1      3     2.2    3.0       3.0
## 84     2    1    1       1      4     2.5    3.1       2.4
## 85     2    1    1       1      5     2.7    3.0       2.3
## 86     2    1    1       2      1     2.7    2.8       2.4
## 87     2    1    1       2      2     2.5    2.9       2.6
## 88     2    1    1       2      3     1.6    3.1       1.8
## 89     2    1    1       2      4     2.5    3.2       2.3
## 90     2    1    1       2      5     2.5    2.9       2.5
## 91     2    1    1       3      1     2.2    2.8       2.3
## 92     2    1    1       3      2     2.4    2.9       2.1
## 93     2    1    1       3      3     2.2    3.1       2.3
## 94     2    1    1       3      4     3.1    3.1       2.6
## 95     2    1    1       3      5     2.9    3.3       2.8
## 96     2    1    1       4      1     2.4    3.4       2.4
## 97     2    1    1       4      2     3.1    3.1       2.7
## 98     2    1    1       4      3     2.3    3.2       2.5
## 99     2    1    1       4      4     3.2    3.5       3.1
## 100    2    1    1       4      5     2.9    2.7       2.7
## 101    2    1    2       1      1     2.6    3.3       2.6
## 102    2    1    2       1      2     2.7    3.0       2.7
## 103    2    1    2       1      3     2.5    2.9       2.7
## 104    2    1    2       1      4     2.4    3.0       2.5
## 105    2    1    2       1      5     2.0    2.9       2.1
## 106    2    1    2       2      1     2.0    3.0       1.9
## 107    2    1    2       2      2     2.3    3.1       2.3
## 108    2    1    2       2      3     1.7    3.1       2.4
## 109    2    1    2       2      4     2.6    3.1       2.5
## 110    2    1    2       2      5     2.2    2.9       2.1
## 111    2    1    2       3      1     1.7    3.2       1.5
## 112    2    1    2       3      2     2.2    3.2       2.0
## 113    2    1    2       3      3     1.7    3.1       2.0
## 114    2    1    2       3      4     2.8    3.2       2.7
## 115    2    1    2       3      5     2.6    3.3       2.6
## 116    2    1    2       4      1     2.0    3.5       2.2
## 117    2    1    2       4      2     1.8    3.0       2.0
## 118    2    1    2       4      3     1.6    3.4       2.1
## 119    2    1    2       4      4     2.8    3.3       2.6
## 120    2    1    2       4      5     2.7    2.3       2.6
## 121    2    2    1       1      1     2.8    2.6       2.5
## 122    2    2    1       1      2     2.9    2.0       2.7
## 123    2    2    1       1      3     3.0    2.7       2.9
## 124    2    2    1       1      4     2.6    3.0       2.5
## 125    2    2    1       1      5     2.8    2.2       2.6
## 126    2    2    1       2      1     3.4    3.2       2.8
## 127    2    2    1       2      2     3.5    2.9       3.0
## 128    2    2    1       2      3     2.6    2.8       2.5
## 129    2    2    1       2      4     3.3    3.0       3.1
## 130    2    2    1       2      5     2.0    2.8       2.5
## 131    2    2    1       3      1     2.8    2.8       2.6
## 132    2    2    1       3      2     3.5    2.8       3.0
## 133    2    2    1       3      3     2.5    3.2       2.3
## 134    2    2    1       3      4     3.3    3.0       2.7
## 135    2    2    1       3      5     3.5    2.9       2.9
## 136    2    2    1       4      1     3.2    3.4       2.5
## 137    2    2    1       4      2     3.3    2.8       2.8
## 138    2    2    1       4      3     3.0    3.0       2.8
## 139    2    2    1       4      4     3.5    3.2       3.1
## 140    2    2    1       4      5     3.4    3.0       2.8
## 141    2    2    2       1      1     2.7    2.5       2.5
## 142    2    2    2       1      2     2.5    2.7       2.3
## 143    2    2    2       1      3     3.2    2.7       3.0
## 144    2    2    2       1      4     2.4    2.7       2.5
## 145    2    2    2       1      5     2.7    2.1       2.3
## 146    2    2    2       2      1     2.2    2.7       2.3
## 147    2    2    2       2      2     3.1    2.9       2.6
## 148    2    2    2       2      3     2.2    2.8       3.1
## 149    2    2    2       2      4     2.9    3.0       2.7
## 150    2    2    2       2      5     2.8    2.7       2.6
## 151    2    2    2       3      1     2.5    3.2       2.3
## 152    2    2    2       3      2     2.9    3.3       2.7
## 153    2    2    2       3      3     2.5    3.1       2.5
## 154    2    2    2       3      4     3.0    2.9       2.5
## 155    2    2    2       3      5     2.9    3.1       3.1
## 156    2    2    2       4      1     2.7    3.3       2.6
## 157    2    2    2       4      2     2.6    2.8       2.3
## 158    2    2    2       4      3     2.5    3.1       2.6
## 159    2    2    2       4      4     3.4    3.3       3.0
## 160    2    2    2       4      5     2.5    2.8       2.3

Secure importing

In the previous exercises, you have been working with URLs that all start with http://. There is, however, a safer alternative to HTTP, namely HTTPS, which stands for HypterText Transfer Protocol Secure. Just remember this: HTTPS is relatively safe, HTTP is not.

  • Luckily for us, you can use the standard importing functions with https:// connections since R version 3.2.2.
# https URL to the swimming_pools csv file.
url_csv <- "https://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/swimming_pools.csv"

# Import the file using read.csv(): pools1
pools1 <- read.csv(url_csv)

# Import the file using read_csv(): pools2
pools2 <- read_csv(url_csv)

# Print the structure of pools1 and pools2
str(pools1)
## 'data.frame':    20 obs. of  4 variables:
##  $ Name     : Factor w/ 20 levels "Acacia Ridge Leisure Centre",..: 1 2 3 4 5 6 19 7 8 9 ...
##  $ Address  : Factor w/ 20 levels "1 Fairlead Crescent, Manly",..: 5 20 18 10 9 11 6 15 12 17 ...
##  $ Latitude : num  -27.6 -27.6 -27.6 -27.5 -27.4 ...
##  $ Longitude: num  153 153 153 153 153 ...
str(pools2)
## Classes 'tbl_df', 'tbl' and 'data.frame':    20 obs. of  4 variables:
##  $ Name     : chr  "Acacia Ridge Leisure Centre" "Bellbowrie Pool" "Carole Park" "Centenary Pool (inner City)" ...
##  $ Address  : chr  "1391 Beaudesert Road, Acacia Ridge" "Sugarwood Street, Bellbowrie" "Cnr Boundary Road and Waterford Road Wacol" "400 Gregory Terrace, Spring Hill" ...
##  $ Latitude : num  -27.6 -27.6 -27.6 -27.5 -27.4 ...
##  $ Longitude: num  153 153 153 153 153 ...

Import Excel files from the web

When you learned about gdata, it was already mentioned that gdata can handle .xls files that are located on a remote web server. readxl can’t, at least not yet. The URL with which you’ll be working is already available in the sample code. You will import it once using gdata and once with the readxl package via a workaround.

# Load the readxl and gdata package
#library(readxl)
#library(gdata)
##Unable to locate valid perl interpreter
##gdata: 
##gdata: read.xls() will be unable to read Excel XLS and XLSX files unless the 'perl=' argument is
##gdata: used to specify the location of a valid perl intrpreter.

# Specification of url: url_xls
##url_xls <- "http://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/latitude.xls"

# Import the .xls file with gdata: excel_gdata
##excel_gdata <- read.xls(url_xls)

# Download file behind URL, name it local_latitude.xls
##dest_path <- file.path("~", "local_latitude.xls")
##download.file(url_xls, dest_path)

# Import the local .xls file with readxl: excel_readxl
##excel_readxl <- read_excel("local_latitude.xls")

Downloading any file, secure or not

In the previous exercise you’ve seen how you can read excel files on the web using the read_excel package by first downloading the file with the download.file() function.

There’s more: with download.file() you can download any kind of file from the web, using HTTP and HTTPS: images, executable files, but also RData files. An RData file is very efficient format to store R data.

You can load data from an RData file using the load() function, but this function does not accept a URL string as an argument. In this exercise, you’ll first download the RData file securely, and then import the local data file.

# https URL to the wine RData file.
##url_rdata <- "https://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/wine.RData"

# Download the wine file to your working directory
##dest_path <- file.path("~", "wine_local.RData")
##download.file(url_rdata, dest_path)

# Load the wine data into your workspace using load()
##load("wine_local.RData")

# Print out the summary of the wine data
##summary(wine)

HTTP? httr! (1)

Downloading a file from the Internet means sending a GET request and receiving the file you asked for. Internally, all the previously discussed functions use a GET request to download files.

  • httr provides a convenient function, GET() to execute this GET request. The result is a response object, that provides easy access to the status code, content-type and, of course, the actual content.

You can extract the content from the request using the content() function. At the time of writing, there are three ways to retrieve this content: as a raw object, as a character vector, or an R object, such as a list. If you don’t tell content() how to retrieve the content through the as argument, it’ll try its best to figure out which type is most appropriate based on the content-type.

# Load the httr package
library(httr)
## Warning: package 'httr' was built under R version 3.2.5
# Get the url, save response to resp
url <- "http://www.example.com/"
resp <- GET(url)

# Print resp
resp
## Response [http://www.example.com/]
##   Date: 2016-06-01 17:47
##   Status: 200
##   Content-Type: text/html
##   Size: 1.27 kB
## No encoding supplied: defaulting to UTF-8.
## <!doctype html>
## <html>
## <head>
##     <title>Example Domain</title>
## 
##     <meta charset="utf-8" />
##     <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
##     <meta name="viewport" content="width=device-width, initial-scale=1" />
##     <style type="text/css">
##     body {
## ...
# Get the raw content of resp
raw_content <- content(resp, as = "raw")

# Print the head of content
head(raw_content)
## [1] 3c 21 64 6f 63 74

HTTP? httr! (2)

Web content does not limit itself to HTML pages and files stored on remote servers such as DataCamp’s Amazon S3 instances. There are many other data formats out there. A very common one is JSON. This format is very often used by so-called Web APIs, interfaces to web servers with which you as a client can communicate to get or store information in more complicated ways.

# Get the url
url <- "https://www.omdbapi.com/?t=Annie+Hall&y=&plot=short&r=json"
resp <- GET(url)

# Print resp
resp
## Response [https://www.omdbapi.com/?t=Annie+Hall&y=&plot=short&r=json]
##   Date: 2016-06-01 17:47
##   Status: 200
##   Content-Type: application/json; charset=utf-8
##   Size: 662 B
# Print content of resp as text
content(resp, as = "text")
## [1] "{\"Title\":\"Annie Hall\",\"Year\":\"1977\",\"Rated\":\"PG\",\"Released\":\"20 Apr 1977\",\"Runtime\":\"93 min\",\"Genre\":\"Comedy, Romance\",\"Director\":\"Woody Allen\",\"Writer\":\"Woody Allen, Marshall Brickman\",\"Actors\":\"Woody Allen, Diane Keaton, Tony Roberts, Carol Kane\",\"Plot\":\"Neurotic New York comedian Alvy Singer falls in love with the ditzy Annie Hall.\",\"Language\":\"English, German\",\"Country\":\"USA\",\"Awards\":\"Won 4 Oscars. Another 26 wins & 8 nominations.\",\"Poster\":\"http://ia.media-imdb.com/images/M/MV5BMTU1NDM2MjkwM15BMl5BanBnXkFtZTcwODU3OTYwNA@@._V1_SX300.jpg\",\"Metascore\":\"N/A\",\"imdbRating\":\"8.1\",\"imdbVotes\":\"188,690\",\"imdbID\":\"tt0075686\",\"Type\":\"movie\",\"Response\":\"True\"}"
# Print content of resp. Use content() to get the content of resp, but this time do not specify a second argument. R figures out automatically that you're dealing with a JSON, and converts the JSON to a named R list.
content(resp)
## $Title
## [1] "Annie Hall"
## 
## $Year
## [1] "1977"
## 
## $Rated
## [1] "PG"
## 
## $Released
## [1] "20 Apr 1977"
## 
## $Runtime
## [1] "93 min"
## 
## $Genre
## [1] "Comedy, Romance"
## 
## $Director
## [1] "Woody Allen"
## 
## $Writer
## [1] "Woody Allen, Marshall Brickman"
## 
## $Actors
## [1] "Woody Allen, Diane Keaton, Tony Roberts, Carol Kane"
## 
## $Plot
## [1] "Neurotic New York comedian Alvy Singer falls in love with the ditzy Annie Hall."
## 
## $Language
## [1] "English, German"
## 
## $Country
## [1] "USA"
## 
## $Awards
## [1] "Won 4 Oscars. Another 26 wins & 8 nominations."
## 
## $Poster
## [1] "http://ia.media-imdb.com/images/M/MV5BMTU1NDM2MjkwM15BMl5BanBnXkFtZTcwODU3OTYwNA@@._V1_SX300.jpg"
## 
## $Metascore
## [1] "N/A"
## 
## $imdbRating
## [1] "8.1"
## 
## $imdbVotes
## [1] "188,690"
## 
## $imdbID
## [1] "tt0075686"
## 
## $Type
## [1] "movie"
## 
## $Response
## [1] "True"

From JSON to R

The two most important function in the jsonlite package are fromJSON() and toJSON(). Let’s start with fromJSON() first. As you’ve seen in the video, this function can take character strings that represent JSON data. It also works if you pass a URL as a character string or the path to a local file that contains JSON data.

# Load the jsonlite package
library(jsonlite)
## Warning: package 'jsonlite' was built under R version 3.2.2
## 
## Attaching package: 'jsonlite'
## 
## The following object is masked from 'package:utils':
## 
##     View
# Convert wine_json to a list: wine
wine_json <- '{"name":"Chateau Migraine", "year":1997, "alcohol_pct":12.4, "color":"red", "awarded":false}'
#Use fromJSON() to convert it to a list, named wine.
wine <- fromJSON(wine_json)

# Import Quandl data: quandl_data
quandl_url <- "http://www.quandl.com/api/v1/datasets/IWS/INTERNET_INDIA.json?auth_token=i83asDsiWUUyfoypkgMz"
quandl_data <- fromJSON(quandl_url)

# Print structure of wine and quandl_data
str(wine)
## List of 5
##  $ name       : chr "Chateau Migraine"
##  $ year       : int 1997
##  $ alcohol_pct: num 12.4
##  $ color      : chr "red"
##  $ awarded    : logi FALSE
str(quandl_data)
## List of 18
##  $ errors      : Named list()
##  $ id          : int 2351831
##  $ source_name : chr "Internet World Stats"
##  $ source_code : chr "IWS"
##  $ code        : chr "INTERNET_INDIA"
##  $ name        : chr "India Internet Usage"
##  $ urlize_name : chr "India-Internet-Usage"
##  $ display_url : chr "http://www.internetworldstats.com/asia/in.htm"
##  $ description : chr "Internet Usage and Population Statistics"
##  $ updated_at  : chr "2016-01-01T04:23:55.235Z"
##  $ frequency   : chr "annual"
##  $ from_date   : chr "1998-12-31"
##  $ to_date     : chr "2012-12-31"
##  $ column_names: chr [1:4] "YEAR" "Users" "Population" "% Pen."
##  $ private     : logi FALSE
##  $ type        : NULL
##  $ premium     : logi FALSE
##  $ data        : chr [1:13, 1:4] "2012-12-31" "2010-12-31" "2009-12-31" "2007-12-31" ...

From JSON to R (2)

JSON is built on two structures: objects and arrays. To help you experiment with these, four JSON strings are included in the sample code. It’s up to you to change them appropriately and then call jsonlite’s fromJSON() function on them each time.

  • Change the assignment of json1 such that the R vector after conversion contains the numbers 1 up to 6, in ascending order. Next, call fromJSON() on json1.
  • Adapt the code for json2 such that it’s converted to a named list with two elements: a, containing the numbers 1, 2 and 3 and b, containing the numbers 4, 5 and 6. Next, call fromJSON() on json2.
  • Remove characters from json3 to build a 2 by 2 matrix containing only 1, 2, 3 and 4. Call fromJSON() on json3 again.
  • Add characters to json4 such that the data frame in which the json is converted contains an additional observation in the last row. For this observations, a equals 5 and b equals 6. Call fromJSON() one last time, on json4.
# Experiment 1
json1 <- '[1, 2, 3,4, 5,6]'
fromJSON(json1)
## [1] 1 2 3 4 5 6
# Experiment 2
json2 <- '{"a": [1, 2, 3], "b":[4,5,6]}'
fromJSON(json2)
## $a
## [1] 1 2 3
## 
## $b
## [1] 4 5 6
# Experiment 3
json3 <- '[[1, 2], [3, 4]]'
fromJSON(json3)
##      [,1] [,2]
## [1,]    1    2
## [2,]    3    4
# Experiment 4
json4 <- '[{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]'
fromJSON(json4)
##   a b
## 1 1 2
## 2 3 4
## 3 5 6

Ask OMDb

In the video, you saw how easy it is to interact with an API once you know how to formulate requests. Filip showed how you could ask for all information on Rain Man from OMDb. Simply perform a GET() call, and next ask for the contents with the content() function. This content() function, which is part of the httr package, uses jsonlite behind the scenes to import the JSON data into R.

However, by now you also know that jsonlite can handle URLs itself. Simply passing the request URL to fromJSON() will get your data into R. In this exercise, you will be using this technique to compare the release year of two movies in the Open Movie Database.

# Definition of the URLs
url_sw4 <- "http://www.omdbapi.com/?i=tt0076759&r=json"
url_sw3 <- "http://www.omdbapi.com/?i=tt0121766&r=json"

# Import two URLs with fromJSON(): sw4 and sw3
sw4 <- fromJSON(url_sw4)
sw3 <- fromJSON(url_sw3)

# Print out the Title element of both lists
sw4$Title
## [1] "Star Wars: Episode IV - A New Hope"
sw3$Title
## [1] "Star Wars: Episode III - Revenge of the Sith"
# Is the release year of sw4 later than sw3?
sw4$Year > sw3$Year
## [1] FALSE

From R to JSON

Apart from converting JSON to R with fromJSON(), you can also use toJSON() to convert R data to a JSON format. In its most basic use, you simply pass this function an R object to convert to a JSON. The result is an R object of the class json, which is basically a character string representing that JSON.

For this exercise, you will be working with a .csv file containing information on the amount of desalinated water that is produced around the world. As you’ll see, it contains a lot of missing values. This data can be found on the URL that is specified in the sample code.

# URL pointing to the .csv file
url_csv <- "http://s3.amazonaws.com/assets.datacamp.com/course/importing_data_into_r/water.csv"

# Import the .csv file located at url_csv
water <- read.csv(url_csv, stringsAsFactors = FALSE)

# Convert the data file according to the requirements
water_json <- toJSON(water)
   
# Print out water_json
water_json
## [{"water":"Algeria","X1992":0.064,"X2002":0.017},{"water":"American Samoa"},{"water":"Angola","X1992":0.0001,"X2002":0.0001},{"water":"Antigua and Barbuda","X1992":0.0033},{"water":"Argentina","X1992":0.0007,"X1997":0.0007,"X2002":0.0007},{"water":"Australia","X1992":0.0298,"X2002":0.0298},{"water":"Austria","X1992":0.0022,"X2002":0.0022},{"water":"Bahamas","X1992":0.0013,"X2002":0.0074},{"water":"Bahrain","X1992":0.0441,"X2002":0.0441,"X2007":0.1024},{"water":"Barbados","X2007":0.0146},{"water":"British Virgin Islands","X2007":0.0042},{"water":"Canada","X1992":0.0027,"X2002":0.0027},{"water":"Cape Verde","X1992":0.002,"X1997":0.0017},{"water":"Cayman Islands","X1992":0.0033},{"water":"Central African Rep."},{"water":"Chile","X1992":0.0048,"X2002":0.0048},{"water":"Colombia","X1992":0.0027,"X2002":0.0027},{"water":"Cuba","X1992":0.0069,"X1997":0.0069,"X2002":0.0069},{"water":"Cyprus","X1992":0.003,"X1997":0.003,"X2002":0.0335},{"water":"Czech Rep.","X1992":0.0002,"X2002":0.0002},{"water":"Denmark","X1992":0.015,"X2002":0.015},{"water":"Djibouti","X1992":0.0001,"X2002":0.0001},{"water":"Ecuador","X1992":0.0022,"X1997":0.0022,"X2002":0.0022},{"water":"Egypt","X1992":0.025,"X1997":0.025,"X2002":0.1},{"water":"El Salvador","X1992":0.0001,"X2002":0.0001},{"water":"Finland","X1992":0.0001,"X2002":0.0001},{"water":"France","X1992":0.0117,"X2002":0.0117},{"water":"Gibraltar","X1992":0.0077},{"water":"Greece","X1992":0.01,"X2002":0.01},{"water":"Honduras","X1992":0.0002,"X2002":0.0002},{"water":"Hungary","X1992":0.0002,"X2002":0.0002},{"water":"India","X1997":0.0005,"X2002":0.0005},{"water":"Indonesia","X1992":0.0187,"X2002":0.0187},{"water":"Iran","X1992":0.003,"X1997":0.003,"X2002":0.003,"X2007":0.2},{"water":"Iraq","X1997":0.0074,"X2002":0.0074},{"water":"Ireland","X1992":0.0002,"X2002":0.0002},{"water":"Israel","X1992":0.0256,"X2002":0.0256,"X2007":0.14},{"water":"Italy","X1992":0.0973,"X2002":0.0973},{"water":"Jamaica","X1992":0.0005,"X1997":0.0005,"X2002":0.0005},{"water":"Japan","X1997":0.04,"X2002":0.04},{"water":"Jordan","X1997":0.002,"X2007":0.0098},{"water":"Kazakhstan","X1997":1.328,"X2002":1.328},{"water":"Kuwait","X1992":0.507,"X1997":0.231,"X2002":0.4202},{"water":"Lebanon","X2007":0.0473},{"water":"Libya","X2002":0.018},{"water":"Malaysia","X1992":0.0043,"X2002":0.0043},{"water":"Maldives","X1992":0.0004},{"water":"Malta","X1992":0.024,"X1997":0.031,"X2002":0.031},{"water":"Marshall Islands","X1992":0.0007},{"water":"Mauritania","X1992":0.002,"X2002":0.002},{"water":"Mexico","X1992":0.0307,"X2002":0.0307},{"water":"Morocco","X1992":0.0034,"X1997":0.0034,"X2002":0.007},{"water":"Namibia","X1992":0.0003,"X2002":0.0003},{"water":"Netherlands Antilles","X1992":0.063},{"water":"Nicaragua","X1992":0.0002,"X2002":0.0002},{"water":"Nigeria","X1992":0.003,"X2002":0.003},{"water":"Norway","X1992":0.0001,"X2002":0.0001},{"water":"Oman","X1997":0.034,"X2002":0.034,"X2007":0.109},{"water":"Peru","X1992":0.0054,"X2002":0.0054},{"water":"Poland","X1992":0.007,"X2002":0.007},{"water":"Portugal","X1992":0.0016,"X2002":0.0016},{"water":"Qatar","X1992":0.065,"X1997":0.099,"X2002":0.099,"X2007":0.18},{"water":"Saudi Arabia","X1992":0.683,"X1997":0.727,"X2002":0.863,"X2007":1.033},{"water":"Senegal","X1992":0.0001,"X2002":0.0001},{"water":"Somalia","X1992":0.0001,"X2002":0.0001},{"water":"South Africa","X1992":0.018,"X2002":0.018},{"water":"Spain","X1992":0.1002,"X2002":0.1002},{"water":"Sudan","X1992":0.0004,"X1997":0.0004,"X2002":0.0004},{"water":"Sweden","X1992":0.0002,"X2002":0.0002},{"water":"Trinidad and Tobago","X2007":0.036},{"water":"Tunisia","X1992":0.008,"X2002":0.013},{"water":"Turkey","X1992":0.0005,"X2002":0.0005,"X2007":0.0005},{"water":"United Arab Emirates","X1992":0.163,"X1997":0.385,"X2007":0.95},{"water":"United Kingdom","X1992":0.0333,"X2002":0.0333},{"water":"United States","X1992":0.58,"X2002":0.58},{"water":"Venezuela","X1992":0.0052,"X2002":0.0052},{"water":"Yemen, Rep.","X1992":0.01,"X2002":0.01}]

Minify and prettify

JSONs can come in different formats. Take these two JSONs, that are in fact exactly the same: the first one is in a minified format, the second one is in a pretty format with indentation, whitespace and new lines:

# Mini
## {"a":1,"b":2,"c":{"x":5,"y":6}} 

# Pretty
## {
 ## "a": 1,
 ## "b": 2,
 ## "c": {
 ##   "x": 5,
 ##   "y": 6
##  }
##}

Unless you’re a computer, you surely prefer the second version. However, the standard form that toJSON() returns, is the minified version, as it is more concise. You can adapt this behavior by setting the pretty argument inside toJSON() to TRUE. If you already have a JSON string, you can use prettify() or minify() to make the JSON pretty or as concise as possible.

# Convert mtcars to a pretty JSON: pretty_json
pretty_json <- toJSON(mtcars, pretty = TRUE)

# Print pretty_json
pretty_json
## [
##   {
##     "mpg": 21,
##     "cyl": 6,
##     "disp": 160,
##     "hp": 110,
##     "drat": 3.9,
##     "wt": 2.62,
##     "qsec": 16.46,
##     "vs": 0,
##     "am": 1,
##     "gear": 4,
##     "carb": 4,
##     "_row": "Mazda RX4"
##   },
##   {
##     "mpg": 21,
##     "cyl": 6,
##     "disp": 160,
##     "hp": 110,
##     "drat": 3.9,
##     "wt": 2.875,
##     "qsec": 17.02,
##     "vs": 0,
##     "am": 1,
##     "gear": 4,
##     "carb": 4,
##     "_row": "Mazda RX4 Wag"
##   },
##   {
##     "mpg": 22.8,
##     "cyl": 4,
##     "disp": 108,
##     "hp": 93,
##     "drat": 3.85,
##     "wt": 2.32,
##     "qsec": 18.61,
##     "vs": 1,
##     "am": 1,
##     "gear": 4,
##     "carb": 1,
##     "_row": "Datsun 710"
##   },
##   {
##     "mpg": 21.4,
##     "cyl": 6,
##     "disp": 258,
##     "hp": 110,
##     "drat": 3.08,
##     "wt": 3.215,
##     "qsec": 19.44,
##     "vs": 1,
##     "am": 0,
##     "gear": 3,
##     "carb": 1,
##     "_row": "Hornet 4 Drive"
##   },
##   {
##     "mpg": 18.7,
##     "cyl": 8,
##     "disp": 360,
##     "hp": 175,
##     "drat": 3.15,
##     "wt": 3.44,
##     "qsec": 17.02,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 2,
##     "_row": "Hornet Sportabout"
##   },
##   {
##     "mpg": 18.1,
##     "cyl": 6,
##     "disp": 225,
##     "hp": 105,
##     "drat": 2.76,
##     "wt": 3.46,
##     "qsec": 20.22,
##     "vs": 1,
##     "am": 0,
##     "gear": 3,
##     "carb": 1,
##     "_row": "Valiant"
##   },
##   {
##     "mpg": 14.3,
##     "cyl": 8,
##     "disp": 360,
##     "hp": 245,
##     "drat": 3.21,
##     "wt": 3.57,
##     "qsec": 15.84,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 4,
##     "_row": "Duster 360"
##   },
##   {
##     "mpg": 24.4,
##     "cyl": 4,
##     "disp": 146.7,
##     "hp": 62,
##     "drat": 3.69,
##     "wt": 3.19,
##     "qsec": 20,
##     "vs": 1,
##     "am": 0,
##     "gear": 4,
##     "carb": 2,
##     "_row": "Merc 240D"
##   },
##   {
##     "mpg": 22.8,
##     "cyl": 4,
##     "disp": 140.8,
##     "hp": 95,
##     "drat": 3.92,
##     "wt": 3.15,
##     "qsec": 22.9,
##     "vs": 1,
##     "am": 0,
##     "gear": 4,
##     "carb": 2,
##     "_row": "Merc 230"
##   },
##   {
##     "mpg": 19.2,
##     "cyl": 6,
##     "disp": 167.6,
##     "hp": 123,
##     "drat": 3.92,
##     "wt": 3.44,
##     "qsec": 18.3,
##     "vs": 1,
##     "am": 0,
##     "gear": 4,
##     "carb": 4,
##     "_row": "Merc 280"
##   },
##   {
##     "mpg": 17.8,
##     "cyl": 6,
##     "disp": 167.6,
##     "hp": 123,
##     "drat": 3.92,
##     "wt": 3.44,
##     "qsec": 18.9,
##     "vs": 1,
##     "am": 0,
##     "gear": 4,
##     "carb": 4,
##     "_row": "Merc 280C"
##   },
##   {
##     "mpg": 16.4,
##     "cyl": 8,
##     "disp": 275.8,
##     "hp": 180,
##     "drat": 3.07,
##     "wt": 4.07,
##     "qsec": 17.4,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 3,
##     "_row": "Merc 450SE"
##   },
##   {
##     "mpg": 17.3,
##     "cyl": 8,
##     "disp": 275.8,
##     "hp": 180,
##     "drat": 3.07,
##     "wt": 3.73,
##     "qsec": 17.6,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 3,
##     "_row": "Merc 450SL"
##   },
##   {
##     "mpg": 15.2,
##     "cyl": 8,
##     "disp": 275.8,
##     "hp": 180,
##     "drat": 3.07,
##     "wt": 3.78,
##     "qsec": 18,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 3,
##     "_row": "Merc 450SLC"
##   },
##   {
##     "mpg": 10.4,
##     "cyl": 8,
##     "disp": 472,
##     "hp": 205,
##     "drat": 2.93,
##     "wt": 5.25,
##     "qsec": 17.98,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 4,
##     "_row": "Cadillac Fleetwood"
##   },
##   {
##     "mpg": 10.4,
##     "cyl": 8,
##     "disp": 460,
##     "hp": 215,
##     "drat": 3,
##     "wt": 5.424,
##     "qsec": 17.82,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 4,
##     "_row": "Lincoln Continental"
##   },
##   {
##     "mpg": 14.7,
##     "cyl": 8,
##     "disp": 440,
##     "hp": 230,
##     "drat": 3.23,
##     "wt": 5.345,
##     "qsec": 17.42,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 4,
##     "_row": "Chrysler Imperial"
##   },
##   {
##     "mpg": 32.4,
##     "cyl": 4,
##     "disp": 78.7,
##     "hp": 66,
##     "drat": 4.08,
##     "wt": 2.2,
##     "qsec": 19.47,
##     "vs": 1,
##     "am": 1,
##     "gear": 4,
##     "carb": 1,
##     "_row": "Fiat 128"
##   },
##   {
##     "mpg": 30.4,
##     "cyl": 4,
##     "disp": 75.7,
##     "hp": 52,
##     "drat": 4.93,
##     "wt": 1.615,
##     "qsec": 18.52,
##     "vs": 1,
##     "am": 1,
##     "gear": 4,
##     "carb": 2,
##     "_row": "Honda Civic"
##   },
##   {
##     "mpg": 33.9,
##     "cyl": 4,
##     "disp": 71.1,
##     "hp": 65,
##     "drat": 4.22,
##     "wt": 1.835,
##     "qsec": 19.9,
##     "vs": 1,
##     "am": 1,
##     "gear": 4,
##     "carb": 1,
##     "_row": "Toyota Corolla"
##   },
##   {
##     "mpg": 21.5,
##     "cyl": 4,
##     "disp": 120.1,
##     "hp": 97,
##     "drat": 3.7,
##     "wt": 2.465,
##     "qsec": 20.01,
##     "vs": 1,
##     "am": 0,
##     "gear": 3,
##     "carb": 1,
##     "_row": "Toyota Corona"
##   },
##   {
##     "mpg": 15.5,
##     "cyl": 8,
##     "disp": 318,
##     "hp": 150,
##     "drat": 2.76,
##     "wt": 3.52,
##     "qsec": 16.87,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 2,
##     "_row": "Dodge Challenger"
##   },
##   {
##     "mpg": 15.2,
##     "cyl": 8,
##     "disp": 304,
##     "hp": 150,
##     "drat": 3.15,
##     "wt": 3.435,
##     "qsec": 17.3,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 2,
##     "_row": "AMC Javelin"
##   },
##   {
##     "mpg": 13.3,
##     "cyl": 8,
##     "disp": 350,
##     "hp": 245,
##     "drat": 3.73,
##     "wt": 3.84,
##     "qsec": 15.41,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 4,
##     "_row": "Camaro Z28"
##   },
##   {
##     "mpg": 19.2,
##     "cyl": 8,
##     "disp": 400,
##     "hp": 175,
##     "drat": 3.08,
##     "wt": 3.845,
##     "qsec": 17.05,
##     "vs": 0,
##     "am": 0,
##     "gear": 3,
##     "carb": 2,
##     "_row": "Pontiac Firebird"
##   },
##   {
##     "mpg": 27.3,
##     "cyl": 4,
##     "disp": 79,
##     "hp": 66,
##     "drat": 4.08,
##     "wt": 1.935,
##     "qsec": 18.9,
##     "vs": 1,
##     "am": 1,
##     "gear": 4,
##     "carb": 1,
##     "_row": "Fiat X1-9"
##   },
##   {
##     "mpg": 26,
##     "cyl": 4,
##     "disp": 120.3,
##     "hp": 91,
##     "drat": 4.43,
##     "wt": 2.14,
##     "qsec": 16.7,
##     "vs": 0,
##     "am": 1,
##     "gear": 5,
##     "carb": 2,
##     "_row": "Porsche 914-2"
##   },
##   {
##     "mpg": 30.4,
##     "cyl": 4,
##     "disp": 95.1,
##     "hp": 113,
##     "drat": 3.77,
##     "wt": 1.513,
##     "qsec": 16.9,
##     "vs": 1,
##     "am": 1,
##     "gear": 5,
##     "carb": 2,
##     "_row": "Lotus Europa"
##   },
##   {
##     "mpg": 15.8,
##     "cyl": 8,
##     "disp": 351,
##     "hp": 264,
##     "drat": 4.22,
##     "wt": 3.17,
##     "qsec": 14.5,
##     "vs": 0,
##     "am": 1,
##     "gear": 5,
##     "carb": 4,
##     "_row": "Ford Pantera L"
##   },
##   {
##     "mpg": 19.7,
##     "cyl": 6,
##     "disp": 145,
##     "hp": 175,
##     "drat": 3.62,
##     "wt": 2.77,
##     "qsec": 15.5,
##     "vs": 0,
##     "am": 1,
##     "gear": 5,
##     "carb": 6,
##     "_row": "Ferrari Dino"
##   },
##   {
##     "mpg": 15,
##     "cyl": 8,
##     "disp": 301,
##     "hp": 335,
##     "drat": 3.54,
##     "wt": 3.57,
##     "qsec": 14.6,
##     "vs": 0,
##     "am": 1,
##     "gear": 5,
##     "carb": 8,
##     "_row": "Maserati Bora"
##   },
##   {
##     "mpg": 21.4,
##     "cyl": 4,
##     "disp": 121,
##     "hp": 109,
##     "drat": 4.11,
##     "wt": 2.78,
##     "qsec": 18.6,
##     "vs": 1,
##     "am": 1,
##     "gear": 4,
##     "carb": 2,
##     "_row": "Volvo 142E"
##   }
## ]
# Minify pretty_json: mini_json
mini_json <- minify(pretty_json)

# Print mini_json
mini_json
## [{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16.46,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4"},{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.875,"qsec":17.02,"vs":0,"am":1,"gear":4,"carb":4,"_row":"Mazda RX4 Wag"},{"mpg":22.8,"cyl":4,"disp":108,"hp":93,"drat":3.85,"wt":2.32,"qsec":18.61,"vs":1,"am":1,"gear":4,"carb":1,"_row":"Datsun 710"},{"mpg":21.4,"cyl":6,"disp":258,"hp":110,"drat":3.08,"wt":3.215,"qsec":19.44,"vs":1,"am":0,"gear":3,"carb":1,"_row":"Hornet 4 Drive"},{"mpg":18.7,"cyl":8,"disp":360,"hp":175,"drat":3.15,"wt":3.44,"qsec":17.02,"vs":0,"am":0,"gear":3,"carb":2,"_row":"Hornet Sportabout"},{"mpg":18.1,"cyl":6,"disp":225,"hp":105,"drat":2.76,"wt":3.46,"qsec":20.22,"vs":1,"am":0,"gear":3,"carb":1,"_row":"Valiant"},{"mpg":14.3,"cyl":8,"disp":360,"hp":245,"drat":3.21,"wt":3.57,"qsec":15.84,"vs":0,"am":0,"gear":3,"carb":4,"_row":"Duster 360"},{"mpg":24.4,"cyl":4,"disp":146.7,"hp":62,"drat":3.69,"wt":3.19,"qsec":20,"vs":1,"am":0,"gear":4,"carb":2,"_row":"Merc 240D"},{"mpg":22.8,"cyl":4,"disp":140.8,"hp":95,"drat":3.92,"wt":3.15,"qsec":22.9,"vs":1,"am":0,"gear":4,"carb":2,"_row":"Merc 230"},{"mpg":19.2,"cyl":6,"disp":167.6,"hp":123,"drat":3.92,"wt":3.44,"qsec":18.3,"vs":1,"am":0,"gear":4,"carb":4,"_row":"Merc 280"},{"mpg":17.8,"cyl":6,"disp":167.6,"hp":123,"drat":3.92,"wt":3.44,"qsec":18.9,"vs":1,"am":0,"gear":4,"carb":4,"_row":"Merc 280C"},{"mpg":16.4,"cyl":8,"disp":275.8,"hp":180,"drat":3.07,"wt":4.07,"qsec":17.4,"vs":0,"am":0,"gear":3,"carb":3,"_row":"Merc 450SE"},{"mpg":17.3,"cyl":8,"disp":275.8,"hp":180,"drat":3.07,"wt":3.73,"qsec":17.6,"vs":0,"am":0,"gear":3,"carb":3,"_row":"Merc 450SL"},{"mpg":15.2,"cyl":8,"disp":275.8,"hp":180,"drat":3.07,"wt":3.78,"qsec":18,"vs":0,"am":0,"gear":3,"carb":3,"_row":"Merc 450SLC"},{"mpg":10.4,"cyl":8,"disp":472,"hp":205,"drat":2.93,"wt":5.25,"qsec":17.98,"vs":0,"am":0,"gear":3,"carb":4,"_row":"Cadillac Fleetwood"},{"mpg":10.4,"cyl":8,"disp":460,"hp":215,"drat":3,"wt":5.424,"qsec":17.82,"vs":0,"am":0,"gear":3,"carb":4,"_row":"Lincoln Continental"},{"mpg":14.7,"cyl":8,"disp":440,"hp":230,"drat":3.23,"wt":5.345,"qsec":17.42,"vs":0,"am":0,"gear":3,"carb":4,"_row":"Chrysler Imperial"},{"mpg":32.4,"cyl":4,"disp":78.7,"hp":66,"drat":4.08,"wt":2.2,"qsec":19.47,"vs":1,"am":1,"gear":4,"carb":1,"_row":"Fiat 128"},{"mpg":30.4,"cyl":4,"disp":75.7,"hp":52,"drat":4.93,"wt":1.615,"qsec":18.52,"vs":1,"am":1,"gear":4,"carb":2,"_row":"Honda Civic"},{"mpg":33.9,"cyl":4,"disp":71.1,"hp":65,"drat":4.22,"wt":1.835,"qsec":19.9,"vs":1,"am":1,"gear":4,"carb":1,"_row":"Toyota Corolla"},{"mpg":21.5,"cyl":4,"disp":120.1,"hp":97,"drat":3.7,"wt":2.465,"qsec":20.01,"vs":1,"am":0,"gear":3,"carb":1,"_row":"Toyota Corona"},{"mpg":15.5,"cyl":8,"disp":318,"hp":150,"drat":2.76,"wt":3.52,"qsec":16.87,"vs":0,"am":0,"gear":3,"carb":2,"_row":"Dodge Challenger"},{"mpg":15.2,"cyl":8,"disp":304,"hp":150,"drat":3.15,"wt":3.435,"qsec":17.3,"vs":0,"am":0,"gear":3,"carb":2,"_row":"AMC Javelin"},{"mpg":13.3,"cyl":8,"disp":350,"hp":245,"drat":3.73,"wt":3.84,"qsec":15.41,"vs":0,"am":0,"gear":3,"carb":4,"_row":"Camaro Z28"},{"mpg":19.2,"cyl":8,"disp":400,"hp":175,"drat":3.08,"wt":3.845,"qsec":17.05,"vs":0,"am":0,"gear":3,"carb":2,"_row":"Pontiac Firebird"},{"mpg":27.3,"cyl":4,"disp":79,"hp":66,"drat":4.08,"wt":1.935,"qsec":18.9,"vs":1,"am":1,"gear":4,"carb":1,"_row":"Fiat X1-9"},{"mpg":26,"cyl":4,"disp":120.3,"hp":91,"drat":4.43,"wt":2.14,"qsec":16.7,"vs":0,"am":1,"gear":5,"carb":2,"_row":"Porsche 914-2"},{"mpg":30.4,"cyl":4,"disp":95.1,"hp":113,"drat":3.77,"wt":1.513,"qsec":16.9,"vs":1,"am":1,"gear":5,"carb":2,"_row":"Lotus Europa"},{"mpg":15.8,"cyl":8,"disp":351,"hp":264,"drat":4.22,"wt":3.17,"qsec":14.5,"vs":0,"am":1,"gear":5,"carb":4,"_row":"Ford Pantera L"},{"mpg":19.7,"cyl":6,"disp":145,"hp":175,"drat":3.62,"wt":2.77,"qsec":15.5,"vs":0,"am":1,"gear":5,"carb":6,"_row":"Ferrari Dino"},{"mpg":15,"cyl":8,"disp":301,"hp":335,"drat":3.54,"wt":3.57,"qsec":14.6,"vs":0,"am":1,"gear":5,"carb":8,"_row":"Maserati Bora"},{"mpg":21.4,"cyl":4,"disp":121,"hp":109,"drat":4.11,"wt":2.78,"qsec":18.6,"vs":1,"am":1,"gear":4,"carb":2,"_row":"Volvo 142E"}]