RとRStudio入門
- RStudioを使用してRプログラムを作成および実行します。
- Rには通常の算術演算子と数学関数があります。
-
<-を使用して変数に値を代入します。 -
ls()を使用してプログラム内の変数を一覧表示します。 -
rm()を使用してプログラム内のオブジェクトを削除します。 -
install.packages()を使用してパッケージ(ライブラリ)をインストールします。
RStudio を使ったプロジェクト管理
- RStudio を使用して一貫したレイアウトでプロジェクトを作成および管理する。
- 生データを読み取り専用として扱う。
- 生成された出力を使い捨てとみなす。
- 関数の定義と適用を分離する。
ヘルプの利用
- R のオンラインヘルプを取得するには
help()を使用します。
データ構造
-
read.csvを使用して R で表形式データを読み取ります。 - R の基本データ型は、double、integer、complex、logical、character です。
- データフレームや行列のようなデータ構造は、リストやベクトルを基にし、いくつかの属性が追加されています。
データフレームの操作
- 新しい列をデータフレームに追加するには
cbind()を使用します。 - 新しい行をデータフレームに追加するには
rbind()を使用します。 - データフレームから行を削除します。
- データフレームの構造を理解するために、
str()、summary()、nrow()、ncol()、dim()、colnames()、head()、typeof()を使用します。 -
read.csv()を使用して CSV ファイルを読み込みます。 - データフレームの
length()が何を表しているのか理解します。
データの部分集合化
- Indexing in R starts at 1, not 0.
- Access individual values by location using
[]. - Access slices of data using
[low:high]. - Access arbitrary sets of data using
[c(...)]. - Use logical operations and logical vectors to access subsets of data.
Control Flow
- Use
ifandelseto make choices. - Use
forto repeat operations.
Creating Publication-Quality Graphics with ggplot2
- Use
ggplot2to create plots. - Think about graphics in layers: aesthetics, geometry, statistics, scale transformation, and grouping.
Vectorization
- Use vectorized operations instead of loops.
Functions Explained
- Use
functionto define a new function in R. - Use parameters to pass values into functions.
- Use
stopifnot()to flexibly check function arguments in R. - Load functions into programs using
source().
Writing Data
- Save plots from RStudio using the ‘Export’ button.
- Use
write.tableto save tabular data.
Data Frame Manipulation with dplyr
- Use the
dplyrpackage to manipulate data frames. - Use
select()to choose variables from a data frame. - Use
filter()to choose data based on values. - Use
group_by()andsummarize()to work with subsets of data. - Use
mutate()to create new variables.
Data Frame Manipulation with tidyr
- Use the
tidyrpackage to change the layout of data frames. - Use
pivot_longer()to go from wide to longer layout. - Use
pivot_wider()to go from long to wider layout.
Producing Reports With knitr
- Mix reporting written in R Markdown with software written in R.
- Specify chunk options to control formatting.
- Use
knitrto convert these documents into PDF and other formats.
Writing Good Software
- Keep your project folder structured, organized and tidy.
- Document what and why, not how.
- Break programs into short single-purpose functions.
- Write re-runnable tests.
- Don’t repeat yourself.
- Be consistent in naming, indentation, and other aspects of style.