0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RStudio - R言語の基礎 #03、モルモットの歯の成長に関するデータを分析

Last updated at Posted at 2023-02-07

Googleデータアナリティックスプロフェッショナルを受講しています。
今回はR言語のパッケージ・tidyverseの基本的な機能について学びました。

R言語のパッケージ

R言語では、ユーザーが作成した再利用可能なR言語の関数・ドキュメントが含まれたパッケージを再利用することができます。インストールされているパッケージを確認するには、installed.packages()というコマンドを実行します。
他にも、packagesペインからもアクティブなパッケージを確認できます。

R007.png

Tidyverse

データ分析に活用できるパッケージとして、多くの場合tidyverseが使われます。
tidyverseにはコアとなる8つのパッケージ(ggplot2, tibble, tidyr, readr, purrr, dplyr, stringr, forcats)が含まれています。

パイプ演算子を使う

#dplyrの関数を使ってフィルタリングする
library(dplyr)

#モルモットの歯の成長に関するデータ
data("ToothGrowth")
View(ToothGrowth)
summary(ToothGrowth)

#アスコルビン酸、オレンジジュースの投薬量が0.5である場合をフィルタリングする
filtered_tg <- filter(ToothGrowth, dose == 0.5)
View(filtered_tg)

#薬量が0.5であるケースを歯の伸長で並べる
filtered_tg_arranged <- arrange(filtered_tg, len)
View(filtered_tg_arranged)

#フィルタリング・ソートをネストにして一度に行うことも可能
filtered_tg_nest <- arrange(filter(ToothGrowth, dose == 0.5), len)
View(filtered_tg_nest)

#パイプ演算子を使うと、どのような処理が行われるか記述しやすくなる
#ctrl + shift + m - パイプ演算子を入力
filteres_toothgrowth <- ToothGrowth %>%
	filter(dose==0.5) %>%
	arrange(len)

#投薬した薬ごとに分類 → 平均値を比較する 
filteres_toothgrowth <- ToothGrowth %>%
	filter(dose==0.5) %>%
	group_by(supp) %>%
	summarize(mean_len=mean(len, na.rm=T), .group="drop")

View(filteres_toothgrowth)

R008.png

R009.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?