Skip to content

Instantly share code, notes, and snippets.

View ajjain's full-sized avatar

Abhishek Jain ajjain

  • Impetus Technologies Inc.
  • Indore
View GitHub Profile
@ajjain
ajjain / writingdata.R
Created September 9, 2015 09:37
writing data to files in R
# dumping textual data
# stores data and its meta data too in a file.
# no need to read to read meta data back, so tables can be created
# back quickly. Only downside is space efficency
# dput & dget command generates the R code and can read back. Also
# dput can work on single R object
# create a data frame
y <- data.frame(a=1, b="abc")
@ajjain
ajjain / readdata.R
Created September 9, 2015 09:03
Importing/reading data in R
# reading a small or moderately sized CSV data file
csvdata <- read.csv("data/sample.csv")
tabdata <- read.table("data/sample.csv")
#---------------------------------------------------------------------
# reading larger dataset
# read only an initial data set
initdata <- read.table("data/sample.csv", nrows=100, sep=",", header=T)
@ajjain
ajjain / names.R
Created September 9, 2015 07:47
naming values in R
# names in R
#------------------------------------------------------------
# Vectors can have names
v1 <- 1:3
# print names for v1 this shall return null
names(v1)
# assigning names to values
names(v1) <- c("a", "b", "c")
@ajjain
ajjain / matrix.R
Created September 9, 2015 07:30
Matrices in R
# matrix in R
m <- matrix(nrow=2, ncol=3)
# get the dimensions
dim(m)
attributes(m)
# matrix are filled column wise
n <- matrix(1:6, nrow=2, ncol=3)
@ajjain
ajjain / na_nan.R
Created September 8, 2015 12:25
NA & NaN in R
x <- c(1,2,NA,3)
is.na(x)
is.nan(x)
y <- c(1,2,NaN,3)
is.na(y)
is.nan(y)
z <- c(1,2,NaN,NA)
is.na(z)
@ajjain
ajjain / factor.R
Created September 8, 2015 12:24
Understanding Factors in R
# factor for categorical data can be ordered or unordered
# eg. gender (M, F), size (S, M, L, XL, XXL)
# (M, F) or (S, M, L, XL, XXL) are called levels
x <- factor(c("S", "M", "L", "XL", "XXL", "S", "XL", "XXL"))
# represent the class of x which is factor
class(x)
# strips out the class and shows the factors represents as integer
@ajjain
ajjain / vector.R
Created September 8, 2015 12:23
Understanding vectors in R
# C stands for CONCAT and helps in creating vector
x <- c(1,2,3)
y1 <- c(T, F, T, F)
y2 <- c(TRUE, FALSE)
# sequence of numbers
z <- 1:100
# complex numbers
a <- c(1+2i, 3+4i)
@ajjain
ajjain / BizService.java
Created June 26, 2014 04:31
MyBatis+Managed+Transaction
public class BizService {
private SqlSessionFactory sqlSessionFactory;
/**
* Initalize the
*
* @param config the config
*/
private void init(QueryEngineConfiguration config) {
InputStream inputStream;
@ajjain
ajjain / book.sql
Last active August 29, 2015 14:03
PostgreSQL+MyBatis+Insert+Autogenerated ID
CREATE TABLE BOOK (
ID SERIAL PRIMARY KEY,
NAME VARCHAR (50) UNIQUE NOT NULL,
TITLE VARCHAR (100),
DESCRIPTION VARCHAR (200)
);
@ajjain
ajjain / Polymorphism.scala
Created December 18, 2013 03:59
Example for understanding polymorphism in Scala.
package learn.scala.polymorphism
trait List[T] {
def isEmpty: Boolean
def head: T
def tail: List[T]
}
class Nil[T] extends List[T] {
def isEmpty: Boolean = true