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.
- Read File: csvDataset
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.
For JSON, we’re going to load an external library.
- Load rjson library: library(rjson)
- Read File: jsonDataset
- Access an Object: jsonDataset (gives you the first object)
Link to datasets and example R notebook: https://www.datazar.com/project/p556632b8-4760-4d16-b787-2dbe74b3b1a4/files
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more…
Source:: R News