What are the most used data import functions in R

What are the most used data import functions in R


  R Interview Questions

Read csv files

> tags = read.csv("C:\\Users\\Ajay Tech\\Downloads\\ml-20m\\ml-20m\\tags.csv")

Read files with any separator

tags = read.table("C:\\Users\\FGT0008\\Downloads\\ml-20m\\ml-20m\\tags.csv",
                  sep="\t",  # tab separator
                  header = TRUE,
                  na.strings = "NA")

Read JSON files

# Read JSON file from weather.com website
> weather = fromJSON(file="http://api.openweathermap.org/data/2.5/weather?q=chicago&APPID=37a81ae1e682ac417883b0a3xxxxxx")

This result is a list

> str(weather)
List of 12
 $ coord     :List of 2
  ..$ lon: num -87.6
  ..$ lat: num 41.9
 $ weather   :List of 2
  ..$ :List of 4
  .. ..$ id         : num 701
  .. ..$ main       : chr "Mist"
  .. ..$ description: chr "mist"
  .. ..$ icon       : chr "50d"
  ..$ :List of 4
  .. ..$ id         : num 721
  .. ..$ main       : chr "Haze"
  .. ..$ description: chr "haze"
  .. ..$ icon       : chr "50d"
 $ base      : chr "stations"
 $ main      :List of 5
  ..$ temp    : num 292
  ..$ pressure: num 1013
  ..$ humidity: num 100
  ..$ temp_min: num 290
  ..$ temp_max: num 294
 $ visibility: num 4828
 $ wind      :List of 2
  ..$ speed: num 1.48
  ..$ deg  : num 258
 $ clouds    :List of 1
  ..$ all: num 1
 $ dt        : num 1.53e+09
 $ sys       :List of 6
  ..$ type   : num 1
  ..$ id     : num 966
  ..$ message: num 0.0043
  ..$ country: chr "US"
  ..$ sunrise: num 1.53e+09
  ..$ sunset : num 1.53e+09
 $ id        : num 4887398
 $ name      : chr "Chicago"
 $ cod       : num 200

The JSON stream looks like this.

{"coord":{"lon":-87.62,"lat":41.88},"weather":[{"id":701,"main":"Mist","description":"mist","icon":"50d"},{"id":721,"main":"Haze","description":"haze","icon":"50d"}],"base":"stations","main":{"temp":291.91,"pressure":1013,"humidity":100,"temp_min":290.15,"temp_max":294.15},"visibility":4828,"wind":{"speed":1.48,"deg":258},"clouds":{"all":1},"dt":1530185760,"sys":{"type":1,"id":966,"message":0.0043,"country":"US","sunrise":1530181076,"sunset":1530235782},"id":4887398,"name":"Chicago","cod":200}

Read CSV file from the web

> iris_data = read.csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")

Scrape HTML tables from the web

If you want to scrape data from HTML tables in the web, you can use the htmltab package. To get the specific HTML table location , use xpath.

Once you get the xpath, use the following code

require(htmltab)
xpath = "//*[@id='mw-content-text']/div/table"
country_iso_codes = htmltab(doc = "https://simple.wikipedia.org/wiki/List_of_U.S._states",
                            which = xpath)

And you get a data frame

> country_iso_codes
   Sl no. Abbreviation     State Name        Capital    Became a State
2       1           AL        Alabama     Montgomery December 14, 1819
3       2           AK         Alaska         Juneau   January 3, 1959
4       3           AZ        Arizona        Phoenix February 14, 1912
5       4           AR       Arkansas    Little Rock     June 15, 1836

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Ajay Tech

Subscribe now to keep reading and get access to the full archive.

Continue reading