Skip to content

Instantly share code, notes, and snippets.

View janeshdev's full-sized avatar

Janesh Devkota janeshdev

View GitHub Profile
@janeshdev
janeshdev / dateandtime.R
Last active December 15, 2015 00:19
Combine date and time into one variable with POSIXct class
jd1 <- read.csv("datetime.csv")
## Then a new variable called timestamp will be added to the dataframe jd1.
jd1 <- transform(jd1, timestamp=as.POSIXct(paste(Date,Time),format="%Y/%m/%d %H:%M"))
@janeshdev
janeshdev / regression.R
Last active December 18, 2015 11:19
Regression
# GET EQUATION AND R-SQUARED AS STRING
# SOURCE: http://goo.gl/K4yh
lm_eqn = function(df){
m = lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
@janeshdev
janeshdev / insetgraphs.R
Created June 13, 2013 19:15
Create inset graphs in ggplot2
library(ggplot2)
library(grid)
# A viewport taking up a fraction of the plot area
vp <- viewport(width = 0.4, height = 0.4, x = 0.1, y = 0.9,just=c("left","top"))
b_plot <- ggplot(cars,aes(speed,dist))+geom_line()+xlim(10,15)
b_plot
## Let us assign a sample date as first with hours, minutes and seconds.
## It is also recommended to use the time zone
> date <- as.POSIXct("2013-06-14 16:34:26",tz="GMT")
## Convert to Julian date using the following function
> jddd <- julian(date, origin = as.POSIXct("2013-01-01 00:00:00",tz="GMT"))
> jddd
Time difference of 164.6906 days
@janeshdev
janeshdev / displayonebyone.tex
Created June 19, 2013 22:38
This gist shows how to display the items in beamer one by one using \only overlay
\documentclass{beamer}
\usetheme{CambridgeUS}
\setbeamertemplate{items}[ball]
\usefonttheme[onlylarge]{structurebold}
\usecolortheme[RGB={205,173,0}]{structure}
\setbeamerfont*{frametitle}{size=\normalsize,series=\bfseries}
\useoutertheme{infolines}
%Pacakges
\usepackage{xcolor}
@janeshdev
janeshdev / multiplot.R
Created June 23, 2013 19:36
It can take any number of plot objects as arguments, or if it can take a list of plot objects passed to plotlist.
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
@janeshdev
janeshdev / Lanczosfilter.m
Created June 24, 2013 00:55
Lanczos filter code by Carlos Adrián Vargas Aguilera. [email protected]
function [y,coef,window,Cx,Ff] = lanczosfilter(x,varargin)
%LANCZOSFILTER Filters a time series via Lanczos method (cosine filter).
% [Y,coef,window,Cx,Ff] = LANCZOSFILTER(X,dT,Cf,M,pass) Filters the time
% series via the Lanczos filter in the frequency space (FFT), where
%
% INPUTS:
% X - Time series
% dT - Sampling interval (default: 1)
% Cf - Cut-off frequency (default: half Nyquist)
% M - Number of coefficients (default: 100)
@janeshdev
janeshdev / 0_reuse_code.js
Created February 19, 2014 00:06
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@janeshdev
janeshdev / velxy_velcomponent.R
Last active August 29, 2015 13:56
This function was created to convert the velocity with magnitude and direction from EFDC similar to x and y velocity components. #velocityxy #velocity
# A function to convert velocity xy EFDC file to velocity component file
velxy_convert <- function(file){
process_pp_template <- function(path) {
# Read data using quicktoolsr function
dat <- readts_EE(path)
# Reshape the data from long format to wide format
dat_mod <- dcast(dat, time ~ id)
return(dat_mod)
}
# Read the file
@janeshdev
janeshdev / read_multiple_files.R
Last active August 24, 2016 20:23
Read multiple csv files in R #csv #multiple #files
# Import files under folder name fawn
# It is very important to include the option (full.names=TRUE). It it is not included
# then you will get the error something like
# "Error in file(file, "rt") : cannot open the connection In addition: Warning message:
#In file(file, "rt") : cannot open file 'FAWN_2002.csv': No such file or directory
mydata=ldply(list.files(path="data/fawn/",pattern="csv",full.names=TRUE),function(filename) {
dum=read.csv(filename)
dum$filename=filename
return(dum)