Skip to content

Instantly share code, notes, and snippets.

View nanxstats's full-sized avatar

Nan Xiao nanxstats

View GitHub Profile
require('mvtnorm')
set.seed(1001)
rho = 0.25
sigma = matrix(0, 500, 500)
for (i in 1:500) {
for (j in 1:500) {
sigma[i, j] = rho^(abs(i - j))
#!/usr/bin/python
import sys, os, re, time
from subprocess import Popen, PIPE
import json
import requests
__license__ = "MIT"
__doc__ = """
This script is meant as an example of Seven Bridges Genomics IGOR API usage.
Running it will annotate all files in specified directory, upload these files, process them using the specified pipeline and download the results.
@nanxstats
nanxstats / connectivity.R
Created May 31, 2012 13:21
Connectivity Simulation
sim <- function(i, neighbor = 3) {
relate = t(replicate(41, sample(1:41, neighbor))) # 随机生成各节点的邻居
mzqidx = setdiff(16:41, setdiff(unique(as.vector(relate[1:15, ])), 1:15)) # 正向没中枪行的下标
if(length(mzqidx) > 0) { # 仅处理存在没中枪行的情况
mzq = relate[mzqidx, ] # 取出正向没中枪的行组成矩阵
if(is.vector(mzq) == TRUE) { # 处理只有单行没中枪的情况 同样做成矩阵
mzq = t(as.matrix(relate[mzqidx, ]))
}
zqx = sapply(apply(mzq, 1, intersect, 1:15), length) # 统计反向中枪情况
if (any(zqx == 0)) {
@nanxstats
nanxstats / test.R
Created September 30, 2012 16:03
R Code for Reading HTML Tables with XML
require(XML)
pg1 = 'http://www.wxtj.gov.cn/tjxx/tjsj/ydzyzb/index.shtml'
pg2 = 'http://www.wxtj.gov.cn/tjxx/tjsj/ydzyzb/index_2.shtml'
pg3 = 'http://www.wxtj.gov.cn/tjxx/tjsj/ydzyzb/index_3.shtml'
pg4 = 'http://www.wxtj.gov.cn/tjxx/tjsj/ydzyzb/index_4.shtml'
url1 = htmlTreeParse(pg1, useInternal = TRUE)
url2 = htmlTreeParse(pg2, useInternal = TRUE)
url3 = htmlTreeParse(pg3, useInternal = TRUE)
@nanxstats
nanxstats / parse.R
Created September 30, 2012 16:43
Simple XML Parsing
require(XML)
doc = xmlParse('score.xml',
useInternal = TRUE, encoding = 'utf-8')
tab = data.frame(name = sapply(getNodeSet(doc, path = "//ROW//XM"), xmlValue),
mail = sapply(getNodeSet(doc, path = "//ROW//DZYX"), xmlValue),
phone = sapply(getNodeSet(doc, path = "//ROW//LXDH"), xmlValue))
@nanxstats
nanxstats / ord.R
Created October 24, 2012 12:24
R function to convert a string into a vector of its ASCII numbers
ord <- function(x) {
s <- x[1]
intbits <- .Machine$sizeof.long * 4
bits <- raw(nchar(s) * intbits)
r <- charToRaw(x)
idx <- if (.Platform$endian == "little") 1:8 else 25:32
for (i in 1:nchar(s)) {
bits[idx + ((i - 1) * intbits)] <- rawToBits(r[i])
@nanxstats
nanxstats / mg.csv
Created December 7, 2012 07:16
libsvm regression dataset - mg
We can make this file beautiful and searchable if this error is corrected: It looks like row 10 should actually have 7 columns, instead of 6 in line 9.
1.1335086e+00, 1.0653587e+00, 8.4250325e-01, 7.0782984e-01, 1.0178784e+00, 1.2476827e+00, 9.0000000e-01
1.1645714e+00, 1.1355961e+00, 5.5885374e-01, 7.9370192e-01, 1.0346794e+00, 1.0067861e+00, 9.0000000e-01
1.0315895e+00, 1.1533842e+00, 4.2572016e-01, 1.0558440e+00, 1.1640420e+00, 7.1297054e-01, 9.0000000e-01
7.9058261e-01, 1.0678134e+00, 7.5043060e-01, 1.2000510e+00, 1.0670009e+00, 5.0349420e-01, 9.4136145e-01
6.3878936e-01, 8.1349649e-01, 9.7291951e-01, 1.2998549e+00, 1.0026658e+00, 5.8392542e-01, 1.1188041e+00
6.4385427e-01, 6.6246660e-01, 9.3785503e-01, 1.1857500e+00, 7.7000347e-01, 9.4327347e-01, 1.2161867e+00
9.7290237e-01, 6.2210481e-01, 1.0653587e+00, 8.4250325e-01, 7.0782984e-01, 1.0178784e+00, 1.2476827e+00
1.1330941e+00, 9.3890430e-01, 1.1355961e+00, 5.5885374e-01, 7.9370192e-01, 1.0346794e+00, 1.0067861e+00
1.1738960e+00, 1.1287058e+00, 1.1533842e+00, 4.2572016e-01, 1.0558440e+00, 1.1640420e+00, 7.1297054e-01
1.2210669e+00, 1.1699535e+00, 1.0678134e+00, 7.5043060e-01, 1.2000510e+00, 1.0670009e+00
x = read.csv('http://www.dataanalysis.cn/doc/a/1/qq.csv')[, -1]
x$time = as.POSIXct(x$time, format = '%Y/%m/%d %H:%M:%S', tz = 'GMT+8')
summary(x$id)[1:10]
# 7cha18 6cha4376 4cha3875 8cha083 4cha698
# 1511 1238 1100 695 533
# 1cha65314 acha@vip.qq.com 2cha1 5cha80 3cha4233
# 440 380 354 304 300
x$time = as.factor(as.Date(x$time, tz = 'GMT+8'))
summary(unique(x)$id)[1:10]
# 6cha4376 4cha3875 7cha18 acha@vip.qq.com 4cha698
@nanxstats
nanxstats / qiegao.text
Created December 24, 2012 05:26
ASCII Qie Gao via: redis-server launch screen
_._
_.-``__ ''-._
_.-`` `. `_. ''-._
.-`` __ ```\/ __ ''-._
( `. `_. `. ``_. )
|`-._ ```\/` __ ```\/ _.-'|
| `-._ `. `_. _.-' |
`-._ `-._ ```\/` _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
require(RCurl)
require(rjson)
require(XML)
url = 'http://cn.soccerway.com/a/block_competition_matches?block_id=page_competition_1_block_competition_matches_7&callback_params=%7B%22page%22%3A%22-1%22%2C%22round_id%22%3A%2217950%22%2C%22outgroup%22%3A%22%22%2C%22view%22%3A%222%22%7D&action=changePage&params=%7B%22page%22%3A0%7D'
doc = getURL(url)
txt = fromJSON(doc)$commands[[1]]$parameters$content
tab = readHTMLTable(htmlParse(txt, encoding = 'utf-8'))[[1]]