Skip to content

Instantly share code, notes, and snippets.

View andilabs's full-sized avatar
👨‍💻
still in love with coding!

Andrzej Kostanski andilabs

👨‍💻
still in love with coding!
View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
@andilabs
andilabs / gist:6351077
Last active December 21, 2015 18:59
BASH: replace all ocurences of 'abc' with 'XYZ' in given textfile Bash, like other shells, is just a tool for coordinating other commands. Typically you would try to use standard UNIX commands, but you can of course use Bash to invoke anything, including your own compiled programs, other shell scripts, Python and Perl scripts etc. In this case, …
sed 's/abc/XYZ/g' <infile >outfile
@andilabs
andilabs / gist:6360270
Last active December 21, 2015 20:18
datatime difference in R strptime difftime
x1<-"2013-03-03 23:26:46.315558"
x2<-"2013-03-03 23:31:53.091022"
x1 <- strptime(x1, "%Y-%m-%d %H:%M:%OS")
x2 <- strptime(x2, "%Y-%m-%d %H:%M:%OS")
x2
[1] "2013-03-03 23:31:53"
x1
[1] "2013-03-03 23:26:46"
x2-x1
Time difference of 5.112924 mins
@andilabs
andilabs / gist:6364047
Last active December 21, 2015 20:49
R
s="{'#JJ': 121, '#NN': 938, '#DT': 184, '#VB': 338, '#RB': 52}"
r1<-sapply(strsplit(s, "[^0-9_]+",as.numeric),as.numeric)
r2<-sapply(strsplit(s, "[^A-Z]+",as.numeric),as.character)
d<-data.frame(id=r2,value=r1)
r1
[,1]
[1,] NA
[2,] 121
@andilabs
andilabs / gist:6364740
Last active December 21, 2015 20:58
http://stackoverflow.com/questions/18484243/r-regex-from-string-to-two-dimensional-data-frame-in-one-command iterate over each row and split it numerical values into the columns named by the key. Example of few rows showing, how I would like it will looks like:
df <- data.frame(m = c(
"{'#JJ': 121, '#NN': 938, '#DT': 184, '#VB': 338, '#RB': 52}",
"{'#NN': 168, '#DT': 59, '#VB': 71, '#RB': 5, '#JJ': 35}",
"{'#JJ': 18, '#NN': 100, '#DT': 23, '#VB': 52, '#RB': 11}"
))
parse.one <- function(s) {
require(rjson)
y <- fromJSON(gsub("'", '"', s))
names(y) <- gsub("#", "", names(y))
@andilabs
andilabs / gist:6385254
Last active December 21, 2015 23:49
MERGE-ing (pasting) two data frames in R: all=FALSE if you want INTERSECTION (common part of both) as a result all=TRUE if you want SUM (differences fullfiled with NA) as a result http://stat.ethz.ch/R-manual/R-devel/library/base/html/merge.html
> authors
surname nationality deceased
1 Tukey US yes
2 Venables Australia no
3 Tierney US no
4 Ripley UK no
5 McNeil Australia no
> books
name title other.author
1 Tukey Exploratory Data Analysis <NA>
@andilabs
andilabs / gist:6387698
Created August 30, 2013 08:45
R checking if value == NA
subset(temp2,is.na(num_ne))
@andilabs
andilabs / gist:6388734
Created August 30, 2013 11:02
run R function on certain columns of data frame and then paste the result to the whole data frame
time_spent <- function(from,to) {
op <- options(digits.secs = 3)
x<-as.numeric((strptime(to, "%Y-%m-%d %H:%M:%OS")-strptime(from, "%Y-%m-%d %H:%M:%OS")),units="secs")
data.frame(time_spent=x)
}
dane_evaluations<-data.frame(dane_evaluations,time_spent=apply(dane_evaluations[,c('documentevaluation_start','documentevaluation_end')],1,function(x) time_spent(x[1], x[2])))
@andilabs
andilabs / gist:6389071
Created August 30, 2013 11:47
R Aggregating Data It is relatively easy to collapse data in R using one or more BY variables and a defined function. http://www.statmethods.net/management/aggregate.html
# aggregate data frame mtcars by cyl and vs, returning means
# for numeric variables
attach(mtcars)
aggdata <-aggregate(mtcars, by=list(cyl,vs),
FUN=mean, na.rm=TRUE)
print(aggdata)
detach(mtcars)