Advent of 2020, Day 11 – Using Azure Databricks Notebooks with R Language for data analytics

Series of Azure Databricks posts:

We looked into SQL language and how to get some basic data preparation done. Today we will look into R and how to get started with data analytics.

Creating a data.frame (or getting data from SQL Table)

Create a new notebook (Name: Day11_R_AnalyticsTasks, Language: R) and let’s go. Now we will get data from SQL tables and DBFS files.

We will be using a database from Day10 and the table called temperature.

%sql
USE Day10;

SELECT * FROM temperature

For getting SQL query result into R data.frame, we will use SparkR package.

library(SparkR)

Getting Query results in R data frame (using SparkR R library)

temp_df <- sql("SELECT * FROM temperature")

With this temp_df data.frame we can start using R or SparkR functions. For example viewing the content of the data.frame.

showDF(temp_df)

This is a SparkR data.frame. you can aslo create a R data.frame by using as.data.frame function.

df <- as.data.frame(temp_df)

Creating standard R data.frame and it can be used with any other R packages.

Importing CSV file into R data.frame

Another way to get data into R data.frame is to feed data from CSV file. And in this case, SparkR library will again come in handy. Once data in data.frame, it can be used with other R libraries.

Day6 <- read.df("dbfs:/FileStore/Day6Data_dbfs.csv", source = "csv", header="true", inferSchema = "true")
head(Day6)

Doing simple analysis and visualisations

Once data is available in data.frame and it can be used for analysis and visualisations. Let’s load ggplot2.

library(ggplot2)
p <- ggplot(df, aes(date, mean_daily_temp)) 
p <- p + geom_jitter() + facet_wrap(~city)
p

And make the graph smaller and give it a theme.

options(repr.plot.height = 500, repr.plot.res = 120)
p + geom_point(aes(color = city)) + geom_smooth() + 
   theme_bw()

Once again, we can use other data wrangling packages. Both dplyr and ggplot2 are preinstalled on Databricks Cluster.

library(dplyr)

When you load a library, nothing might be returned as a result. In case of warning, Databricks will display the warnings. Dplyr package can be used as any other package absolutely normally, without any limitations.

df %>%
  dplyr::group_by(city) %>%
  dplyr::summarise(
       n = dplyr::n()
      ,mean_pos = mean(as.integer(df$mean_daily_temp))
 ) 
#%>% dplyr::filter( as.integer(df$date) > "2020/12/01")

But note(!), dplyr functions might not work, and it is due to the collision of function names with SparkR library. SparkR has same functions (arrange, between, coalesce, collect, contains, count, cume_dist,
dense_rank, desc, distinct, explain, filter, first, group_by, intersect, lag, last, lead, mutate, n, n_distinct, ntile,
percent_rank, rename, row_number, sample_frac, select, sql, summarize, union)
. In other to solve this collision, either detach (detach(“package:dplyr”)) the dplyr package, or we instance the package by: dplyr::summarise instead of just summarise.

Creating a simple linear regression

We can also use many of the R packages for data analysis, and in this case I will run simple regression, trying to predict the daily temperature. Simply run the regression function lm().

model <- lm(mean_daily_temp ~ city + date, data = df)
model

And run base r function summary() to get model insights.

summary(model)
confint(model)

In addition, you can directly install any missing or needed package in notebook (R engine and Databricks Runtime environment version should be applied). In this case, I am running a residualPlot() function from extra installed package car.

install.packages("car")
library(car)
residualPlot(model)

Azure Databricks will generate RMarkdown notebook when using R Language as Kernel language. If you want to create a IPython notebook, make Python as Kernel language and use %r for switching to R Language. Both RMarkdown notebook and HTML file (with included results) are included and available on Github.

Tomorrow we will check and explore how to use Python to do data engineering, but mostly the data analysis tasks. So, stay tuned.

Complete set of code and Notebooks will be available at the Github repository.

Happy Coding and Stay Healthy!

Tagged with: , , , , , , , , ,
Posted in Azure Databricks, Uncategorized
22 comments on “Advent of 2020, Day 11 – Using Azure Databricks Notebooks with R Language for data analytics
  1. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Like

  2. […] article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here) […]

    Like

  3. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  4. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  5. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  6. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  7. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  8. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  9. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  10. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  11. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  12. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  13. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  14. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  15. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  16. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  17. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  18. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  19. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  20. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

  21. […] Dec 11: Using Azure Databricks Notebooks with R Language for data analytics […]

    Like

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...