My experience on my daily works... helping others ease each other

Saturday, June 13, 2020

Caching an Inverse of a Matrix Using R



Caching an Inverse of a Matrix Using R 

Matrix inversion is usually a costly computation and there may be some benefit to caching the inverse of a matrix rather than computing it repeatedly.

This is an example code to cache an inverse of a matrix which then called if:

1. The new matrix computation similar to the previous matrix
2. Matrix is invertible — Read Invertible Matrix
3. The structure and content of the new matrix is equal to previous

The program consists of two functions

  1. A function to cache the matrix and the inverse. It is similar to getter and setter in many programming languages like Java and C.
  2. A function which will be call for the computational process of the matrix

Here are parts of the code:

Setting the matrix
Setting the inverse matrix
Verification on the matrix
Setting the new inverse

Complete source code is reachable at https://github.com/masteramuk/ProgrammingAssignment2

If you like the post, do buy me a coffee please :)

Buy Me A Coffee
Share:

Monday, June 1, 2020

Reading entire URL content is really easy using R

In my good old days, reading the entire content of a website is not easy. The process of web scraping and getting the required data requires lots of programming and a few tools. A friend of mine even developed and sold the tool which he called it (during the development) as myrobot. He developed using PHP.

Now, it is much easier and one of the many ways is using R.

Here are the steps (which requires you to write ONLY two lines of code)

  1. Connect to the website using URL command
    con <- url ([the website url], “r”)
  2. Then, read the website
    x <- readLines(con)
  3. Do whatever you wish with the data. In this example, I print out the head of the website and also copy the whole content to a file.
    head(x)
    dput(x, “readFromUrlExample.html”)

There you go.

Result of the head(x) function
Snapshot of the content of the file copied into readFromUrlExamplehtml

The sample source code can be retrieved at 

https://github.com/masteramuk/LearnR-Coursera/blob/master/sample-ReadFromUrl.R

Share:

About Me

Somewhere, Selangor, Malaysia
An IT by profession, a beginner in photography

Blog Archive

Blogger templates