How to Access Datasets in R

[This article was first published on R Language in Datazar Blog on Medium, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Have you spent hours, pulling your hair out trying to figure out how to access datasets in R? Once imported to a variable, columns from a dataset (eg: CSV) can be very tricky to access. Sometimes columns contain spaces, funky characters or other incosistencies. Here are some examples on how to access the data from CSV and JSON datasets.

CSV

We’ll use this CSV dataset since it has *very* long column names.
  1. Read File: csvDataset<-read.csv("Global Carbon Emissions Record 1751-2013")

2. Access Columns: csvDataset$Total.carbon.emissions.from.fossil.fuel.consumption.and.cement.production..million.metric.tons.of.C.

As you can see here, the blank spaces AND parantheses got replaced by a period . .This is actually pretty useful because you can just use the period for other special characters, helping you get to variables faster.

You can always head(csvDataset) to look at the column names (and first few rows) to get the actual column names used to call them.

JSON

We’ll use this simple JSON dataset from NASA showing meteorite impacts.

For JSON, we’re going to load an external library.

  1. Load rjson library: library(rjson)
  2. Read File: jsonDataset<-fromJSON(file="city_country_meteor.json")
  3. Access an Object: jsonDataset[1] (gives you the first object)

Link to datasets and example R notebook: https://www.datazar.com/project/p556632b8-4760-4d16-b787-2dbe74b3b1a4/files


How to Access Datasets in R was originally published in Datazar Blog on Medium, where people are continuing the conversation by highlighting and responding to this story.

To leave a comment for the author, please follow the link and comment on their blog: R Language in Datazar Blog on Medium.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)