R is now a large community. Today we have 6,139 R packages. I am planning here to jot down the code snippets based on the newer packages (by following table of available packages, sorted by date of publication). The chunk of the codes will be taken either from the Vignette or from the main package documentation pdf. I also like to include stack overflow threads if there are interesting questions. These interesting and easy-to-apply codes will be applied to different data sets to develop models and assumptions. Here’s Part 1.
Five Packages used in Part 1. [Why 5? Here’s why.]
countrycode, tableone, aRxiv, exams, gender
library(countrycode)
countrycode(232,"cown","country.name")
## [1] "Andorra"
countrycode("United States","country.name","iso3c")
## [1] "USA"
countrycode("DZA","iso3c","cowc")
## [1] "ALG"
cowcodes <- c("ALG","ALB","UKG","CAN","USA")
countrycode(cowcodes,"cowc","iso3c")
## [1] "DZA" "ALB" "GBR" "CAN" "USA"
isocodes <- c(12,8,826,124,840)
var1 <- sample(1:500,5)
var2 <- sample(1:500,5)
df1 <- as.data.frame(cbind(cowcodes,var1))
df2 <- as.data.frame(cbind(isocodes,var2))
df1
## cowcodes var1
## 1 ALG 208
## 2 ALB 49
## 3 UKG 450
## 4 CAN 271
## 5 USA 288
df2
## isocodes var2
## 1 12 483
## 2 8 173
## 3 826 200
## 4 124 293
## 5 840 101
df1$iso3c <- countrycode(df1$cowcodes, "cowc", "iso3c")
df2$iso3c <- countrycode(df2$isocodes, "iso3n", "iso3c")
df3 <- merge(df1,df2,id="iso3c")
df3$country <- countrycode(df3$iso3c, "iso3c", "country.name")
df3
## iso3c cowcodes var1 isocodes var2 country
## 1 ALB ALB 49 8 173 Albania
## 2 CAN CAN 271 124 293 Canada
## 3 DZA ALG 208 12 483 Algeria
## 4 GBR UKG 450 826 200 United Kingdom
## 5 USA USA 288 840 101 United States
library(tableone)
library(survival)
## Loading required package: splines
## Simplest use case
head(pbc)
## id time status trt age sex ascites hepato spiders edema bili chol
## 1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261
## 2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302
## 3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176
## 4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244
## 5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279
## 6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248
## albumin copper alk.phos ast trig platelet protime stage
## 1 2.60 156 1718.0 137.95 172 190 12.2 4
## 2 4.14 54 7394.8 113.52 88 221 10.6 3
## 3 3.48 210 516.0 96.10 55 151 12.0 4
## 4 2.54 64 6121.8 60.63 92 183 10.3 4
## 5 3.53 143 671.0 113.15 72 136 10.9 3
## 6 3.98 50 944.0 93.00 63 NA 11.0 3
dim(pbc)
## [1] 418 20
CreateTableOne(data = pbc)
##
## Overall
## n 418
## id (mean (sd)) 209.50 (120.81)
## time (mean (sd)) 1917.78 (1104.67)
## status (mean (sd)) 0.83 (0.96)
## trt (mean (sd)) 1.49 (0.50)
## age (mean (sd)) 50.74 (10.45)
## sex = f (%) 374 (89.5)
## ascites (mean (sd)) 0.08 (0.27)
## hepato (mean (sd)) 0.51 (0.50)
## spiders (mean (sd)) 0.29 (0.45)
## edema (mean (sd)) 0.10 (0.25)
## bili (mean (sd)) 3.22 (4.41)
## chol (mean (sd)) 369.51 (231.94)
## albumin (mean (sd)) 3.50 (0.42)
## copper (mean (sd)) 97.65 (85.61)
## alk.phos (mean (sd)) 1982.66 (2140.39)
## ast (mean (sd)) 122.56 (56.70)
## trig (mean (sd)) 124.70 (65.15)
## platelet (mean (sd)) 257.02 (98.33)
## protime (mean (sd)) 10.73 (1.02)
## stage (mean (sd)) 3.02 (0.88)
library(vcd)
## Loading required package: grid
head(Arthritis)
## ID Treatment Sex Age Improved
## 1 57 Treated Male 27 Some
## 2 46 Treated Male 29 None
## 3 77 Treated Male 30 None
## 4 17 Treated Male 32 Marked
## 5 36 Treated Male 46 Marked
## 6 23 Treated Male 58 Marked
dim(Arthritis)
## [1] 84 5
CreateTableOne(data = Arthritis)
##
## Overall
## n 84
## ID (mean (sd)) 42.50 (24.39)
## Treatment = Treated (%) 41 (48.8)
## Sex = Male (%) 25 (29.8)
## Age (mean (sd)) 53.36 (12.77)
## Improved (%)
## None 42 (50.0)
## Some 14 (16.7)
## Marked 28 (33.3)
library(aRxiv)
deconv <- arxiv_search('au:"Dragan Slavkov Hajdukovic"', limit=21)
nrow(deconv)
## [1] 21
deconv[, c('title', 'authors')]
## title
## 1 Testing existence of antigravity
## 2 Black Holes and Gravitational Properties of Antimatter
## 3 Dark energy, antimatter gravity and geometry of the Universe
## 4 Vacuum fluctuations, the size of extra spatial dimensions and\n microscopic black holes at CERN
## 5 Can the new Neutrino Telescopes reveal the Gravitational Properties of\n Antimatter?
## 6 A new equation of state for dark energy
## 7 Dark matter, dark energy and gravitational proprieties of antimatter
## 8 On the relation between mass of pion, fundamental physical constants and\n cosmological parameters
## 9 A few provoking relations between dark energy, dark matter and pions
## 10 What would be outcome of a Big Crunch?
## 11 On the vacuum fluctuations, Pioneer Anomaly and Modified Newtonian\n Dynamics
## 12 Is dark matter an illusion created by the gravitational polarization of\n the quantum vacuum?
## 13 Do we live in the universe successively dominated by matter and\n antimatter?
## 14 On the absolute value of the neutrino mass
## 15 Quantum vacuum and dark matter
## 16 Quantum Vacuum and Virtual Gravitational Dipoles: The solution to the\n Dark Energy Problem?
## 17 Can observations inside the Solar System reveal the gravitational\n properties of the quantum vacuum?
## 18 Trans-Neptunian objects: a laboratory for testing the existence of\n non-Newtonian component of the gravitational force
## 19 The signatures of new physics, astrophysics and cosmology?
## 20 A new model of dark matter distribution in galaxies
## 21 Virtual gravitational dipoles: The key for the understanding of the\n Universe?
## authors
## 1 Dragan Slavkov Hajdukovic
## 2 Dragan Slavkov Hajdukovic
## 3 Dragan Slavkov Hajdukovic
## 4 Dragan Slavkov Hajdukovic
## 5 Dragan Slavkov Hajdukovic
## 6 Dragan Slavkov Hajdukovic
## 7 Dragan Slavkov Hajdukovic
## 8 Dragan Slavkov Hajdukovic
## 9 Dragan Slavkov Hajdukovic
## 10 Dragan Slavkov Hajdukovic
## 11 Dragan Slavkov Hajdukovic
## 12 Dragan Slavkov Hajdukovic
## 13 Dragan Slavkov Hajdukovic
## 14 Dragan Slavkov Hajdukovic
## 15 Dragan Slavkov Hajdukovic
## 16 Dragan Slavkov Hajdukovic
## 17 Dragan Slavkov Hajdukovic
## 18 Dragan Slavkov Hajdukovic
## 19 Dragan Slavkov Hajdukovic
## 20 Dragan Slavkov Hajdukovic
## 21 Dragan Slavkov Hajdukovic
library(gender)
gender("Bobby")
## $name
## [1] "Bobby"
##
## $proportion_male
## [1] 0.9724
##
## $proportion_female
## [1] 0.0276
##
## $gender
## [1] "male"
##
## $year_min
## [1] 1932
##
## $year_max
## [1] 2012
gender("Nick")
## $name
## [1] "Nick"
##
## $proportion_male
## [1] 0.998
##
## $proportion_female
## [1] 0.002
##
## $gender
## [1] "male"
##
## $year_min
## [1] 1932
##
## $year_max
## [1] 2012
gender("Lorretta")
## $name
## [1] "Lorretta"
##
## $proportion_male
## [1] 0
##
## $proportion_female
## [1] 1
##
## $gender
## [1] "female"
##
## $year_min
## [1] 1932
##
## $year_max
## [1] 2012
gender("Bobby", method = "ipums", years = 1900)
## $name
## [1] "Bobby"
##
## $proportion_male
## [1] 0
##
## $proportion_female
## [1] 1
##
## $gender
## [1] "female"
##
## $year_min
## [1] 1900
##
## $year_max
## [1] 1900
gender("Bobby", method = "ssa", years = 1950)
## $name
## [1] "Bobby"
##
## $proportion_male
## [1] 0.9737
##
## $proportion_female
## [1] 0.0263
##
## $gender
## [1] "male"
##
## $year_min
## [1] 1950
##
## $year_max
## [1] 1950
gender("Bobby", method = "ssa", years = 2000)
## $name
## [1] "Bobby"
##
## $proportion_male
## [1] 0.9825
##
## $proportion_female
## [1] 0.0175
##
## $gender
## [1] "male"
##
## $year_min
## [1] 2000
##
## $year_max
## [1] 2000