Skip to content

Instantly share code, notes, and snippets.

@masaha03
masaha03 / gist:945735
Created April 28, 2011 03:19
空文字列判定
str <- ""
system.time(for (i in 1:30000000) str == "")
system.time(for (i in 1:30000000) nchar(str) == 0)
system.time(for (i in 1:30000000) nzchar(str))
str<-rep("", 30000000)
system.time(str=="")
system.time(nchar(str)==0)
system.time(nzchar(str))
@masaha03
masaha03 / kaken_test.R
Created May 15, 2011 11:41
KAKEN APIテスト
# 科研検索APIのヘルプ http://kaken.nii.ac.jp/ja/help/#opensearch-form
# 課題検索OpenSearchテスト
test1 <- readLines("http://kaken.nii.ac.jp/opensearchk.cgi?q15=60273570", encoding="UTF-8")
cat(test1, fill=TRUE)
# 研究者検索OpenSearchテスト
test2 <- readLines('http://kaken.nii.ac.jp/opensearchr.cgi?q1="社会学"', encoding="UTF-8")
cat(test2, fill=TRUE)
# 課題RDF取得テスト
@masaha03
masaha03 / kaken_researcherNetwork.R
Created May 15, 2011 11:59
KAKENから研究者ネットワーク作成
library(XML)
# 社会学者一覧
# 研究種目が社会学の研究者を検索→研究者番号を取得→検索2ページ目以降の研究者の研究者番号をループで所得
# 取得の所要時間は5分程度
search.result <- xmlParse(enc2utf8('http://kaken.nii.ac.jp/opensearchr.cgi?count=200&q1="社会学"')) # 研究者検索
search.result <- xmlToList(search.result)
search.result <- rapply(search.result, function(x){iconv(x,"UTF-8","CP932")}, how="replace")
entries <- search.result[which(names(search.result)=="entry")]
r.names <- sapply(entries, function(x){x$title$text}) # 研究者名
tc <- setRefClass(Class="testClass",
fields = list(f1="numeric", f2="numeric"),
representation=representation(x="character")
)
t1 <- tc$new(f1=4, f2=5, x="a")
t2 <- t1
t2@x <- "b"
t1
t2
@masaha03
masaha03 / gist:1206548
Created September 9, 2011 15:40
twitteR & ROAuth
library(twitteR)
library(ROAuth)
requestURL <- "https://api.twitter.com/oauth/request_token"
authURL <- "https://api.twitter.com/oauth/authorize"
accessURL <- "https://api.twitter.com/oauth/access_token"
consumerKey <- "(Consumer key)"
consumerSecret <- "(Consumer secret)"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
<main>
<menu name="Data">
<category name="Variable">
<menuitem name="mean">Mean</menuitem>
</category>
</menu>
<menu name="Analysis">
<category name="Descriptive">
<menuitem name="cor">Correlation</menuitem>
</category>
<main>
<function name="mean" package="base" vesion="2.13.1">
<arg name="x" input="free" type="numeric" requisite="true" />
<arg name="trim" input="free" type="numeric" default="0" />
<arg name="na.rm" input="logical" default="FALSE" />
</function>
<function name="cor" package="stats" vesion="2.13.1">
<arg name="x" input="free" type="numeric; matrix; data.frame" requisite="true" />
<arg name="y" input="free" type="NULL; numeric; matrix; data.frame" default="NULL" />
@masaha03
masaha03 / gist:1416419
Created December 1, 2011 12:38
HSY色空間変換
/*
http://sourceforge.jp/projects/hsy-color/wiki/HSY%E8%89%B2%E7%A9%BA%E9%96%93%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6
*/
private int RGBtoHSYtoRGB(int rgb) {
float h;
float y;
float s;
float S;
float MAX;
@masaha03
masaha03 / gist:1655867
Created January 22, 2012 06:09
MacのRGtk2で文字化け
library(RGtk2)
win <- gtkWindowNew(show=FALSE)
win$add(gtkLabelNew("123abcあいうえお"))
win$setSizeRequest(300,300)
win$showAll()
settings <- gtkSettingsGetDefault()
settings$setStringProperty("gtk-font-name", "Hiragino Kaku Gothic Pro 32", NULL)
settings$setStringProperty("gtk-font-name", "Hiragino Mincho Pro 32", NULL)
settings$setStringProperty("gtk-font-name", "IPAGothic 32", NULL)
f <- function(i){
l <- c("Fizz", "Buzz", "FizzBuzz")
ii <- (0==i%%5)*2 + (0==i%%3)*1
print(l[ii])
print(rep(l[ii], 10)[i])
return(ifelse(0<ii, l[ii], i))
}