データフレーム関連メモ
・行名・列名(ラベル)の変更
colnames, rownames
ref) 行列やデータフレームの列名・変数名の変更
・テーブルとして表示
rmarkdownで使う場合は、xtable, kable など
xtableで行番号ラベルを消すには、行ラベルを設定すればよい。
HTML用にはhtmlTableも使える
以下のサイトがよくまとまっている
ref) Tables with htmlTable and some alternatives
2015年9月13日日曜日
2015年8月31日月曜日
[R] ファイナンス関連計算メモ
・対数収益率
diff(log(x))
diff(log(c(100,101,102,103)))
0.009950331 0.009852296 0.009756175
# データフレームにreturn列を追加 (行数が減るので、先頭に0を追加する)
> head(returns)
date close
1 2004-05-19 10967
2 2004-05-20 10862
3 2004-05-21 11070
> returns$return <- c(0, diff(log(returns$close)))
date close return
1 2004-05-19 10967 0.000000000
2 2004-05-20 10862 -0.009620304
3 2004-05-21 11070 0.018968287
2015年1月16日金曜日
[R] ggplot2メモ
ggplot2関連メモあれこれ
scale_x_datetime() – Time series graph x-axis control
・日付・時刻の構造(R言語の仕様)
https://sites.google.com/site/leihcrev/r/date-and-time
・カンマ区切り
scale_y_continuous(label=comma)
http://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/
□ 軸
・時間軸の書式scale_x_datetime() – Time series graph x-axis control
・日付・時刻の構造(R言語の仕様)
https://sites.google.com/site/leihcrev/r/date-and-time
・カンマ区切り
scale_y_continuous(label=comma)
□ グラフ
・Candlestick charthttp://www.perdomocore.com/2012/using-ggplot-to-make-candlestick-charts-alpha/
2015年1月15日木曜日
[R, kdb] Rからkdbを使うメモ
kdb+とR関連メモあれこれ
・kxwiki
http://code.kx.com/wiki/Cookbook/IntegratingWithR
・起動時に呼ぶ
Rは行数が多いと非常に重くなるので行数に注意
数万行を超えると厳しい
・String
kdbのStringをRに渡すとListになってしまうので、symbolにキャストする
・datetime
datetimeはRで対応していないので、timestampにキャストする
・時刻
時刻型が無いみたいなので、適宜floatでカバーする
たとえば、hourで表示したい場合は、 hour: (0.001*`int$`time$time)%3600 のような形で気合でカバーする
・kxwiki
http://code.kx.com/wiki/Cookbook/IntegratingWithR
・起動時に呼ぶ
# load q server
source("c:/r/qServer.R"); Sys.setenv(TZ="GMT")
# milisec
options(digits.secs=3)
・行数注意Rは行数が多いと非常に重くなるので行数に注意
数万行を超えると厳しい
・String
kdbのStringをRに渡すとListになってしまうので、symbolにキャストする
・datetime
datetimeはRで対応していないので、timestampにキャストする
・時刻
時刻型が無いみたいなので、適宜floatでカバーする
たとえば、hourで表示したい場合は、 hour: (0.001*`int$`time$time)%3600 のような形で気合でカバーする
2014年10月15日水曜日
[R] timeとtimezone
Rのtimezone関連あれこれ
□ List of tz database time zones
http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
□ help(Sys.time)
Get Current Date, Time and Timezone
Sys.time()
Sys.Date()
Sys.timezone()
□ 時刻型への変換
as.POSIXct, as.POSIXlt
□ List of tz database time zones
http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
□ help(Sys.time)
Get Current Date, Time and Timezone
Sys.time()
Sys.Date()
Sys.timezone()
□ 時刻型への変換
as.POSIXct, as.POSIXlt
as.POSIXct("15/03/31", format="%y/%m/%d")
[1] "2015-03-31 JST"
登録:
投稿 (Atom)