Rで解析:痒いところに手が届く「broman」パッケージの紹介

Rの解析に役に立つ記事
スポンサーリンク

本パッケージに収録されている多くのコマンドは「基本的なコマンドを組み合わせる」ことで再現が可能です。でも、再現しようとすると手がかかります。なお、「broman」パッケージ名の由来は作者が「Broman」氏だからです。

抄録されているコマンドから目に付いたものを紹介します。他のコマンドはパッケージヘルプを参照ください。

パッケージのバージョンは0.59-5。R version 3.2.1でコマンドを確認しています。


スポンサーリンク

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("broman")

実行コマンドの紹介

詳細はコメント、パッケージヘルプを確認してください。

#パッケージの読み込み
library("broman")

#数字にカンマを加える:add_commasコマンド
#注意出力が文字列になります
add_commas(c(1, 22, 444, 5555, 66666, 666666))
[1] "1"       "22"      "444"     "5,555"   "66,666"  "666,666"

#マウスで任意の場所に矢印を図に加える:arrowlocatorコマンド
#1回目のクリックで始点、2回目のクリックで終点を指定します
plot(0, 0, type = "n", xlab = "", ylab = "", xlim = c(0, 1), ylim = c(0, 1))
arrowlocator(col = "blue", lwd = 2)

#一致を調査:cfコマンド
#matrix, data.frame, list, vectorの調査が可能です
x <- c(5, 8, 9, NA, 3, NA)
y <- c(5, 2, 9, 4, NA, NA)
cf(x,y)
[1]  TRUE FALSE  TRUE FALSE FALSE  TRUE

#&#25351;&#23450;&#12375;&#12383;&#33394;,alpha&#20516;&#12398;&#12459;&#12521;&#12540;&#12467;&#12540;&#12489;&#12434;&#21462;&#24471;:colwalpha&#12467;&#12510;&#12531;&#12489;
colwalpha(c("blue", "red"), alpha = 0.5)
[1] "#0000FF7F" "#FF00007F"

#&#34892;&#21015;&#12398;&#34892;&#26041;&#21521;&#37197;&#21015;&#12398;&#19968;&#33268;&#12434;&#35519;&#26619;:compare_rows&#12467;&#12510;&#12531;&#12489;
#method&#12458;&#12503;&#12471;&#12519;&#12531;:prop_mismatches;&#19968;&#33268;&#12399;1,&#19981;&#19968;&#33268;0,
#                 rms_difference;&#27604;&#36611;&#23550;&#35937;&#12392;&#12398;&#24046;&#20998;
y <- matrix(sample(1:4, 5, replace=TRUE), ncol = 1)
[,1]
[1,]    3
[2,]    1
[3,]    4
[4,]    2
[5,]    3
compare_rows(y, method = "rms_difference")
[,1]
[1,]    3
[2,]    1
[3,]    4
[4,]    2
[5,]    3

#&#25968;&#20516;&#12434;16&#36914;&#25968;&#12395;&#22793;&#25563;:convert2
convert2hex(333)
[1] "14d"

#&#12487;&#12540;&#12479;&#12501;&#12524;&#12540;&#12512;&#12398;&#20869;&#23481;&#12434;&#12456;&#12463;&#12475;&#12523;&#39080;&#12395;&#12503;&#12525;&#12483;&#12488;:excel_fig
df <- data.frame(id = c(101, 102, 103, 104, 105),
                    "&#24615;&#21029;" = c("&#30007;", "&#30007;", "&#22899;", "&#22899;", "&#22899;"),
                    weight = c(22.3, 15.8, 19.7, 50, 60),
                    stringsAsFactors = FALSE)
#Mac&#12391;&#25991;&#23383;&#21270;&#12369;&#38450;&#27490;
par(family = "HiraKakuProN-W3")
#&#12503;&#12525;&#12483;&#12488;
excel_fig(df, col_names = TRUE)

#R&#12398;&#32066;&#20102;:exit&#12467;&#12510;&#12531;&#12489;
#&#27880;&#24847;:RStudio&#12391;&#20351;&#29992;&#12377;&#12427;&#12392;&#24375;&#21046;&#32066;&#20102;&#12375;&#12414;&#12377;
exit()

#plot&#12467;&#12510;&#12531;&#12489;&#12391;ggplot2&#39080;&#12398;&#22259;&#12434;&#12503;&#12525;&#12483;&#12488;:grayplot&#12467;&#12510;&#12531;&#12489;
x <- rnorm(100)
y <- x + rnorm(100, 0, 0.7)
grayplot(x, y, col = "blue", pch = 16)

#hist&#12464;&#12521;&#12512;&#12434;&#25551;&#20889;:histlines&#12467;&#12510;&#12531;&#12489;
x <- rnorm(1000, mean = 20, sd = 5)
plot(histlines(x, breaks = 60, use = "density"),
     type = "l", lwd = 2, xlab = "x", ylab = "Density", las = 1)

#&#20170;&#26085;&#12398;&#26085;&#20184;&#12369;&#12434;&#21462;&#24471;:kbdate&#12467;&#12510;&#12531;&#12489;
#format&#12458;&#12503;&#12471;&#12519;&#12531;:dateonly;&#24180;&#26376;&#26085;,standard;&#26332;&#26085;&#26376;&#26178;&#38291;&#24180;
kbdate("standard")
#windows&#12391;&#12398;&#20986;&#21147;
[1] "&#26408; 8 06 22:51:39 2015"
#mac&#12391;&#12398;&#20986;&#21147;
[1] "&#26408; 8 06 22&#26178;51&#20998;53&#31186; 2015"

#&#37325;&#35079;&#12377;&#12427;&#12487;&#12540;&#12479;&#12398;&#25968;&#12434;&#34920;&#31034;:lenuniq&#12467;&#12510;&#12531;&#12489;
x <- c(1, 2, 1, 3, 1, 1, 2, 2, 3, NA, NA, 1)
lenuniq(x, na.rm = FALSE)
[1] 4

#&#23567;&#25968;&#28857;&#20197;&#19979;&#12398;&#26689;&#25968;&#12434;&#25351;&#23450;:myround&#12467;&#12510;&#12531;&#12489;
myround(51.01, digits = 10)
[1] "51.0100000000"

#&#22793;&#25968;&#12434;.&#12391;&#32080;&#21512;:paste.&#12467;&#12510;&#12531;&#12489;
x <- 3
y <- 4
paste.(x, y)
[1] "3.4"
#&#19979;&#35352;&#12392;&#21516;&#12376;
paste(x, y, sep = ".")
[1] "3.4"

#&#36196;&#12363;&#12425;&#38738;&#12398;&#12459;&#12521;&#12540;&#12497;&#12524;&#12483;&#12488;&#12434;&#20316;&#25104;:revrainbow&#12467;&#12510;&#12531;&#12489;
x <- matrix(1:100, ncol = 10)
image(x, col = revrainbow())

出力例

・excel_figコマンド

excelplot

・grayplotコマンド

grayplot

・histlinesコマンド

histlines

・revrainbowコマンド

revrainbow

少しでも、あなたのウェブや実験の解析が楽になりますように!!

タイトルとURLをコピーしました