Skip to content

Instantly share code, notes, and snippets.

@apoorv74
apoorv74 / state_elections_numbers.R
Created March 12, 2017 04:29
Scraping numbers for state elections from election commission website - http://eciresults.nic.in/
library(rvest)
col_names <- c("Constituency","Const. No.", "Leading Candidate",
"Leading Party" ,"Trailing Candidate" ,"Trailing Party", "Margin", "Status")
state_codes <- c("05","14","19","28","24")
ec_master <- c()
for(state in 1:length(state_codes)){
sample_url <- paste0('http://eciresults.nic.in/statewiseS',state_codes[state],'.htm?st=S',state_codes[state])
xpath <- '//*[@id="divACList"]/table[1]'
@apoorv74
apoorv74 / pdf_merge.R
Created November 3, 2017 12:41
Merging PDF's
library(plotflow)
library(reports)
## make a folder to store the pdfs
folder(deleteMe)
## paste the paths to pdfs together in one string w/ spaces
plotflow:::mergePDF(
in.file=paste(file.path("deleteMe", dir("deleteMe")), collapse=" "),
file="merged.pdf"
@apoorv74
apoorv74 / gsub_map.R
Created January 11, 2018 08:03
Gsub over a map
convert <- function(x) {
map <- c("\\[:alpha:\\]" = "a-zA-Z", "\\[:digit:\\]" = "0-9")
for (i in seq_along(map)) {
x <- gsub(names(map)[[i]], map[[i]], x)
}
x
}
convert("[[:alpha:]](?:(?:[[:digit:]]){1,2}(?:[[:alpha:]]){1,2})(?:(?:[[:digit:]]){6,7})")
#> [1] "[a-zA-Z](?:(?:[0-9]){1,2}(?:[a-zA-Z]){1,2})(?:(?:[0-9]){6,7})"
@apoorv74
apoorv74 / project_setup.R
Last active February 1, 2018 15:06
Create a new R project following the best practices
#!/usr/bin/env Rscript
args = commandArgs(trailingOnly = TRUE)
# What is this: A script that assists in following best practices when starting a new project
# What are these best practices:
# - Creating a project is the first step, managing individual scripts is a mess
# - Always have a git setup at the root of the project
# - A README.md file for documentation
# - A .gitignore file for ignoring files and directories, remember .DS_Store
@apoorv74
apoorv74 / fifaCharts.R
Created June 29, 2018 13:10
Plot player names as per the team formations over a football field (well, a green bg)
args <- commandArgs(trailingOnly = TRUE)
library(ggplot2)
library(ggflags)
bg_color = '#5A8E38'
# Declaring constants -----------------------------------------------------
@apoorv74
apoorv74 / traffic_analysis_ideas.md
Last active July 18, 2018 05:38
Thoughts/Ideas/Hypothesis around analysing vehicular traffic for cities
@apoorv74
apoorv74 / Data_Protection_Committee_Report.md
Last active August 26, 2018 14:34
A Free and Fair Digital Economy: Protecting Privacy, Empowering Indians - Notes

Comparison with other Nations:

  • US - No overarching data protection framework but Sector wise laws - GLB - provisions for collection and use of financial data
  • EU - GDPR (Replaces the Data Protection Directive of 1995) - Technology and Sector agnostic

Current state in India - SPD Rules were issued under Section 43A of the IT Act -

  • Against negligence in implementing and maintaining reasonable security practices and procedures while dealing with sensitive personal data or infoirmation

Entities:

@apoorv74
apoorv74 / PMJAY.r
Created March 27, 2019 10:38
Empanelled hospitals for PMJAY
html_master <- c()
for(i in 1:3){
pmjay_hospt <- 'https://hospitals.pmjay.gov.in/Search/empnlWorkFlow.htm'
req_params <- list("actionFlag"= 'ViewRegisteredHosptlsNew',
"search"= 'Y',
"appReadOnly"= 'Y',
"pageNo"= i,
"searchState"= 23,
"searchDistrict"= 411,
"searchHospType"= -1,
library(rvest)
library(readr)
# This file needs to be read as text.
x <- readr::read_file("~/Downloads/Chandigarh.xls")
# Every file will have a table, the way it is structured in this sample. We just want to extract this table and convert it to a data frame
# Check and clean the file as per the structure of the original one. Headers and Rows need to be adjusted.
y <- x %>% read_html() %>% html_table(fill = TRUE)
z <- y[[1]]
@apoorv74
apoorv74 / processing_changelog.R
Created March 15, 2020 13:22
Processing changelog for the Annual Budget 2020-2021
library(readr)
library(dplyr)
# Change These
file_path <- "~/Downloads/budget_changelog_2020/changelog_2.csv"
file_export_path <- "~/Downloads/budget_changelog_2020/changelog_updated.csv"
changelog <- read_csv(
file_path,
col_types = cols(