using R for Cricket Analysis #rstats

New Zealand just made it to their first ever world cup final ( yes it is cricket) and they made it with a thrilling six ( like a home run) for the last ball. Congrats to New Zealand .Of course R was created in New Zealand too and Hadley Wickham is from New Zealand

I recently installed the rvest package from https://github.com/hadley/rvest and its now on CRAN as well

 

rvest helps you scrape information from web pages. It is designed to work with magrittr to make it easy to express common web scraping tasks, inspired by libraries like beautiful soup.

library(rvest)
lego_movie <- html("http://www.imdb.com/title/tt1490017/")

rating <- lego_movie %>% 
  html_nodes("strong span") %>%
  html_text() %>%
  as.numeric()
rating
#> [1] 7.9

cast <- lego_movie %>%
  html_nodes("#titleCast .itemprop span") %>%
  html_text()
cast
#>  [1] "Will Arnett"     "Elizabeth Banks" "Craig Berry"    
#>  [4] "Alison Brie"     "David Burrows"   "Anthony Daniels"
#>  [7] "Charlie Day"     "Amanda Farinos"  "Keith Ferguson" 
#> [10] "Will Ferrell"    "Will Forte"      "Dave Franco"    
#> [13] "Morgan Freeman"  "Todd Hansen"     "Jonah Hill"

poster <- lego_movie %>%
  html_nodes("#img_primary img") %>%
  html_attr("src")
poster
#> [1] "http://ia.media-imdb.com/images/M/MV5BMTg4MDk1ODExN15BMl5BanBnXkFtZTgwNzIyNjg3MDE@._V1_SX214_AL_.jpg"

The most important functions in rvest are:

  • Create an html document from a url, a file on disk or a string containing html with html().
  • Select parts of a document using css selectors: html_nodes(doc, "table td") (or if you’ve a glutton for punishment, use xpath selectors with html_nodes(doc, xpath = "//table//td")). If you haven’t heard of selectorgadget, make sure to read vignette("selectorgadget") to learn about it.
  • Extract components with html_tag() (the name of the tag), html_text() (all text inside the tag), html_attr() (contents of a single attribute) and html_attrs() (all attributes).
  • (You can also use rvest with XML files: parse with xml(), then extract components using xml_node(), xml_attr(), xml_attrs(), xml_text() and xml_tag().)
  • Parse tables into data frames with html_table().
  • Extract, modify and submit forms with html_form(), set_values() and submit_form().
  • Detect and repair encoding problems with guess_encoding() and repair_encoding().
  • Navigate around a website as if you’re in a browser with html_session(), jump_to(), follow_link(), back(), forward(), submit_form() and so on. (This is still a work in progress, so I’d love your feedback.)

 

While Hadley Wickham seems busy with reading excel files ( see https://github.com/hadley/readxl) maybe using rvest can help in more sports analysis now!

https://decisionstats.com/2013/04/25/using-r-for-cricket-analysis-rstats-ipl/

Meanwhile I am searching for equivalent of readHTMLtable

Install R on Ubuntu 12

sudo gedit /etc/apt/sources.list

THEN ADD TO THE NOTEPAD FILE
deb http://cran.rstudio.com/bin/linux/ubuntu precise/

SAVE AND CLOSE

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
sudo apt-get update
sudo apt-get install r-base
sudo apt-get install r-base-dev

RSTUDIO

sudo apt-get install libjpeg62

wget http://download1.rstudio.org/rstudio-0.98.1103-i386.deb

sudo dpkg -i rstudio-0.98.1103-i386.deb

 

 

Installing VMWare Player on Ubuntu 12

Screenshot from 2015-03-18 06:34:22Download from https://my.vmware.com/web/vmware/free#desktop_end_user_computing/vmware_player/6_0

 

uname -r

sudo apt-get install build-essential linux-headers-`uname -r`

 cd Downloads

~/Downloads$ ls -l

gksudo bash VMware-Player-6.0.5-2443746.i386.bundle

This starts the Wizard and walks you through the rest of the installation

 

Unblocking Wireless on Dell Inspiron on Ubuntu 12

Due to recent shenanigans—

To unblock all hardware

rfkill unblock all
rfkill list all

For wireless drivers

  1. Open the Terminal
  2. Install the build dependencies if you don’t have it already (but it is installed by default):sudo apt-get install build-essential
  3. Type the commands (steps 3-9): wget https://www.kernel.org/pub/linux/kernel/projects/backports/stable/v3.18.1/backports-3.18.1-1.tar.xzHere you can get the latest ones from kernel.org site
  4. tar xvf backports-3.18.1-1.tar.xz
  5. cd backports-3.18.1-1/
  6. make defconfig-ath9k
  7. make
  8. sudo make install
    • (type your password)
  9. sudo update-initramfs-u
    • (type your password if needed)
  10. Reboot your pc. #sudo reboot
  11. Remember that after a Kernel update (sudo apt-get dist-upgrade) we have to repeat the steps 4 to 9.

Sources-

http://askubuntu.com/questions/406531/cant-reach-wi-fi-signal-on-ubuntu-but-can-do-it-on-other-os-devices-ath9k

http://askubuntu.com/questions/139036/how-do-i-fix-a-wireless-is-disabled-by-hardware-switch-error

Markdown with R Commander #rstats

Just training and fiddling with spatial analytics

Markdown with R Commander

Ajay Ohri

2015-03-15

> library(maptools)
> library(raster)
> adm <- getData('GADM', country='IND', level=2)
> mahadm=adm[adm$NAME_1=="Maharashtra",]
> head(mahadm,20)
      PID ID_0 ISO NAME_0 ID_1      NAME_1 ID_2         NAME_2 NL_NAME_2                    VARNAME_2   TYPE_2 ENGTYPE_2
306 17478  105 IND  India   21 Maharashtra  306     Ahmednagar                             Ahmadnagar District  District
307 17479  105 IND  India   21 Maharashtra  307          Akola                                        District  District
308 17480  105 IND  India   21 Maharashtra  308       Amravati           Amaravati, Amraoti, Amaraoti District  District
309 17481  105 IND  India   21 Maharashtra  309     Aurangabad                                        District  District
310 17482  105 IND  India   21 Maharashtra  310       Bhandara                                        District  District
311 17483  105 IND  India   21 Maharashtra  311            Bid                     Bir|Beed|Bhir|Bidh District  District
312 17484  105 IND  India   21 Maharashtra  312        Buldana                                        District  District
313 17485  105 IND  India   21 Maharashtra  313     Chandrapur                                 Chanda District  District
314 17486  105 IND  India   21 Maharashtra  314          Dhule                  Dhulia, West Khandesh District  District
315 17487  105 IND  India   21 Maharashtra  315    Garhchiroli                                        District  District
316 17488  105 IND  India   21 Maharashtra  316        Gondiya                                        District  District
317 17489  105 IND  India   21 Maharashtra  317 Greater Bombay                                        District  District
318 17490  105 IND  India   21 Maharashtra  318        Hingoli                                        District  District
319 17491  105 IND  India   21 Maharashtra  319        Jalgaon                          East Khandesh District  District
320 17492  105 IND  India   21 Maharashtra  320          Jalna                                        District  District
321 17493  105 IND  India   21 Maharashtra  321       Kolhapur                                        District  District
322 17494  105 IND  India   21 Maharashtra  322          Latur                Kulaba, Kolaba, Kolabad District  District
323 17495  105 IND  India   21 Maharashtra  323         Nagpur                                        District  District
324 17496  105 IND  India   21 Maharashtra  324         Nanded                                 Nander District  District
325 17497  105 IND  India   21 Maharashtra  325      Nandurbar                                        District  District
> mahadm$pop=as.factor(sample(1:10,34,T))
> mahadm$pop2=as.factor(sample(1:10,34,T))
> mahadm$pop3=as.factor(sample(1:10,34,T))
> head(mahadm,20)
      PID ID_0 ISO NAME_0 ID_1      NAME_1 ID_2         NAME_2 NL_NAME_2                    VARNAME_2   TYPE_2 ENGTYPE_2
306 17478  105 IND  India   21 Maharashtra  306     Ahmednagar                             Ahmadnagar District  District
307 17479  105 IND  India   21 Maharashtra  307          Akola                                        District  District
308 17480  105 IND  India   21 Maharashtra  308       Amravati           Amaravati, Amraoti, Amaraoti District  District
309 17481  105 IND  India   21 Maharashtra  309     Aurangabad                                        District  District
310 17482  105 IND  India   21 Maharashtra  310       Bhandara                                        District  District
311 17483  105 IND  India   21 Maharashtra  311            Bid                     Bir|Beed|Bhir|Bidh District  District
312 17484  105 IND  India   21 Maharashtra  312        Buldana                                        District  District
313 17485  105 IND  India   21 Maharashtra  313     Chandrapur                                 Chanda District  District
314 17486  105 IND  India   21 Maharashtra  314          Dhule                  Dhulia, West Khandesh District  District
315 17487  105 IND  India   21 Maharashtra  315    Garhchiroli                                        District  District
316 17488  105 IND  India   21 Maharashtra  316        Gondiya                                        District  District
317 17489  105 IND  India   21 Maharashtra  317 Greater Bombay                                        District  District
318 17490  105 IND  India   21 Maharashtra  318        Hingoli                                        District  District
319 17491  105 IND  India   21 Maharashtra  319        Jalgaon                          East Khandesh District  District
320 17492  105 IND  India   21 Maharashtra  320          Jalna                                        District  District
321 17493  105 IND  India   21 Maharashtra  321       Kolhapur                                        District  District
322 17494  105 IND  India   21 Maharashtra  322          Latur                Kulaba, Kolaba, Kolabad District  District
323 17495  105 IND  India   21 Maharashtra  323         Nagpur                                        District  District
324 17496  105 IND  India   21 Maharashtra  324         Nanded                                 Nander District  District
325 17497  105 IND  India   21 Maharashtra  325      Nandurbar                                        District  District
    pop pop2 pop3
306   5    2    6
307   8    5    1
308   2    4    4
309   2    6   10
310   8    7    5
311   9   10    1
312   8    9    4
313   4    9    3
314   1    8    7
315   9    5    7
316   7    8    4
317   9    8    6
318   1   10    3
319   1    8    6
320   8    6   10
321   8    4    8
322   4    9    2
323   5    3    5
324   4    8   10
325   1    5    1
> par(mfrow=c(3,1))
> plot(mahadm,col=mahadm$pop)
1
> plot(mahadm,col=mahadm$pop2)
> plot(mahadm,col=mahadm$pop3)