Every SQL users know that one has to finish composing all SQL codes and then run them all at one time, resulting in a poor ability for interactctive data analytics. However, the simple and easy-to-understand query syntax of SQL is always welcomed by programmers. As powerful computation and analysis tools, R language and esProc are surely need to offer the similar query syntax. In the last SQL basic functions article, we have discuss implement basic SQL functions like retrieve data of the entire table, where, order, group & sum. Let’s talk more today.
The example data is from 2 tables of the classical Northwind database:
Orders table with the main fields: OrderID, EmployeeID, OrderDate, Freight, CustomerID
Customer table with the main fields: CustomerID, CompanyName
Join: Perform left join on Orders table and Customers table by CustomerID.
Select * from Orders left join Customers on Orders.CustomerID =Customers.CustomerID
Comments: The join of SQL equals to join of esProc or the merge of R. Similarly, the left join of SQL equals to join@1 of esProc, or merge(...all.x=TRUE) of R. Obviously, esProc is more alike SQL in the respects of both the syntax conventions and the literal meanings.
Distinct: Remove the duplicate CustomerID
SQL solution: select distinct CustomerID from Orders
R solution: unique(B2$CustomerID)
esProc solution: =B2.id(CustomerID)
Comments: The keywords of the two solutions respectively differ to that of SQL. However, their usages are basically the same to that of SQL. In which, R is the typical function style, and esProc is the typical object style.
Like: Search for the record with Island in ShipName
SQL solution: select * from Orders where ShipName like '%Island%'
R solution: subset(A1,grepl("Island",ShipName,ignore.case = TRUE))
esProc solution: =A1.select(like@c(ShipName ,"*Island*"))
Comments: R supports several means to match, including the regular expressions, and is more powerful than esProc in this respect. The usages of esProc are more close to that of SQL, and fit for those who are familiar with SQL.
When come to complex data calculation, SQL is replaceable in some aspects actually. Just as you see from above comparison, esProc has a coding style more close to that of SQL.
R is more resourceful in details, ideal for the programmers and mathematicians. In addition, supporting the regular expressions and other functions makes R more open as a preferred analysis tool for programmers.