Rの型システム概説
double、integer、character、logical、complex型を理解します。
「Rの型システム概説」はCoddyKit上の無料R Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはR Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 R Academyコースには全4レッスンが含まれています。
R の型システム
R には豊富な型システムがあります。すべてのオブジェクトには class(高水準のカテゴリ)と type(低水準の格納モード)があります。両方を理解すると、正しく効率的なコードを書き、型に関するエラーをデバッグしやすくなります。
# Check class and type of various objects
cat('class(42L) :', class(42L), '
')
cat('typeof(42L) :', typeof(42L), '
')
cat('class(3.14) :', class(3.14), '
')
cat('typeof(3.14) :', typeof(3.14), '
')
cat('class(TRUE) :', class(TRUE), '
')
cat('class("hello"):', class('hello'), '
')class() と typeof() の違い
class(x) は、R のオブジェクト指向システムで使用される高水準のカテゴリ(例: 'integer'、'numeric'、'matrix')を返します。typeof(x) は、低水準の C の格納型(例: 'integer'、'double'、'closure')を返します。
m <- matrix(1:4, nrow = 2)
cat('class(matrix) :', class(m), '
') # matrix array
cat('typeof(matrix) :', typeof(m), '
') # integer
f <- factor(c('a', 'b', 'a'))
cat('class(factor) :', class(f), '
') # factor
cat('typeof(factor) :', typeof(f), '
') # integer (stored as int!)
cat('class(list) :', class(list()), '
') # list
cat('typeof(list) :', typeof(list()), '
') # listis.numeric() — 数値チェック
is.numeric(x) は、integer と double の値、および数値行列に対して TRUE を返します。特定の格納モードではなく、数値のスーパータイプであるかを確認します。
cat('is.numeric(3.14):', is.numeric(3.14), '
') # TRUE (double)
cat('is.numeric(3L): ', is.numeric(3L), '
') # TRUE (integer)
cat('is.numeric(TRUE):', is.numeric(TRUE), '
') # FALSE
cat('is.numeric("3"): ', is.numeric('3'), '
') # FALSE
cat('is.numeric(NA): ', is.numeric(NA), '
') # FALSE (NA is logical)is.integer() と is.double() の違い
is.integer() は整数の格納形式(L サフィックスまたは as.integer() で作成されたもの)であるかを正確に判定します。is.double() は浮動小数点(double)の格納形式であるかを判定します。どちらも numeric のサブセットです。
x_int <- 5L
x_dbl <- 5.0
cat('--- x_int = 5L ---
')
cat('is.integer:', is.integer(x_int), '
') # TRUE
cat('is.double: ', is.double(x_int), '
') # FALSE
cat('is.numeric:', is.numeric(x_int), '
') # TRUE
cat('--- x_dbl = 5.0 ---
')
cat('is.integer:', is.integer(x_dbl), '
') # FALSE
cat('is.double: ', is.double(x_dbl), '
') # TRUE
cat('is.numeric:', is.numeric(x_dbl), '
') # TRUEis.character() — 文字列チェック
is.character(x) は、x が character(文字列)ベクトルの場合に TRUE を返します。テキストであるべき入力の検証や、数値型の NA と character 型の NA の区別に使用します。
greetings <- c('hello', 'world', 'R')
numbers <- c(1, 2, 3)
numeric_string <- '42'
cat('is.character(greetings): ', is.character(greetings), '
')
cat('is.character(numbers): ', is.character(numbers), '
')
cat('is.character(numeric_string): ', is.character(numeric_string), '
')
cat('is.character(NA_character_): ', is.character(NA_character_), '
')is.logical() — 論理値チェック
is.logical(x) は、x に logical(Boolean)値が含まれている場合に TRUE を返します。型指定のない NA は、デフォルトでは logical 値であることに注意してください。
flags <- c(TRUE, FALSE, TRUE, NA)
cat('is.logical(flags): ', is.logical(flags), '
') # TRUE
cat('is.logical(NA): ', is.logical(NA), '
') # TRUE (default NA is logical)
cat('is.logical(1): ', is.logical(1), '
') # FALSE
cat('is.logical("TRUE"):', is.logical('TRUE'), '
') # FALSE
cat('typeof(NA): ', typeof(NA), '
') # logicalR の型階層
R の atomic 型は、単純なものから複雑なものへと次の階層を形成します: logical < integer < double < complex < character。ベクトル内で型を混在させると、R はすべての要素を存在する中で最も複雑な型に変換します。
# Type hierarchy in action
cat('c(TRUE, 1L) type: ', typeof(c(TRUE, 1L)), '
') # integer
cat('c(1L, 1.5) type: ', typeof(c(1L, 1.5)), '
') # double
cat('c(1.5, 1+0i) type: ', typeof(c(1.5, 1+0i)), '
') # complex
cat('c(1+0i, "a") type: ', typeof(c(1+0i, 'a')), '
') # character
cat('c(TRUE, "x") type: ', typeof(c(TRUE, 'x')), '
') # characterプログラムによる型の確認
型を確認する関数を作成して、オブジェクトのリストを監査できます。class()、typeof()、length() を組み合わせると、任意のオブジェクトの構造を包括的にまとめられます。
describe_type <- function(x, label) {
cat(label, ': class =', class(x), ', typeof =', typeof(x),
', length =', length(x), '
')
}
describe_type(42L, 'integer literal')
describe_type(3.14, 'double literal')
describe_type(TRUE, 'logical')
describe_type('hello', 'character')
describe_type(c(1,2,3), 'numeric vector')
describe_type(list(1,'a'), 'list')storage.mode() と mode()
storage.mode(x) は typeof(x) と似ていますが、S3 の命名規約を使用します(例: 'double' の代わりに 'double' を返します)。mode(x) はやや粗い分類を返し、integer と double を 'numeric' としてまとめます。
x_int <- 5L
x_dbl <- 5.0
cat('mode(5L): ', mode(x_int), '
') # numeric
cat('mode(5.0): ', mode(x_dbl), '
') # numeric
cat('storage.mode(5L): ', storage.mode(x_int), '
') # integer
cat('storage.mode(5.0):', storage.mode(x_dbl), '
') # double
# mode() lumps int+dbl; storage.mode() / typeof() distinguish themリストとデータフレームの型の確認
リストやデータフレームなどの複雑なオブジェクトでは、sapply(df, class) によって各列に class() を適用し、列の型を示す名前付きベクトルを取得できます。これはデータフレームのスキーマをすばやく監査する方法です。
df <- data.frame(
id = 1:3,
name = c('Alice', 'Bob', 'Carol'),
score = c(88.5, 79.0, 92.3),
passed = c(TRUE, TRUE, TRUE)
)
cat('Column types:
')
print(sapply(df, class))
cat('Column typeof:
')
print(sapply(df, typeof))型システムのまとめ
R の型確認ツールのクイックリファレンスを示します。
class(x)— 高水準の S3 クラス名typeof(x)— 低水準の C の格納型is.numeric(x)— integer または double の場合に TRUEis.integer(x)— 整数の格納形式の場合のみ TRUEis.double(x)— double(浮動小数点)の場合のみ TRUEis.character(x)— 文字列の場合に TRUEis.logical(x)— TRUE、FALSE、NA の場合に TRUE
x <- 42L
cat('class(42L): ', class(x), '
')
cat('typeof(42L): ', typeof(x), '
')
cat('is.numeric(42L): ', is.numeric(x), '
')
cat('is.integer(42L): ', is.integer(x), '
')
cat('is.double(42L): ', is.double(x), '
')
cat('is.character(42L):', is.character(x), '
')理解度チェック
R で is.numeric(5L) は何を返しますか?
復習: R の型システム
よくできました!このレッスンの重要なポイントは次のとおりです。
class(x)は高水準の型を返し、typeof(x)は低水準の格納型を返しますis.numeric()は integer と double の両方に対して TRUE ですis.integer()とis.double()によって、2 つの数値サブタイプを区別できます- R の型階層(低 → 高): logical → integer → double → complex → character
- ベクトル内で型を混在させると、最も高い型への自動変換が行われます
sapply(df, class)でデータフレームのすべての列の型を監査できます
# Type audit function
audit_types <- function(x, name = 'x') {
cat(name, ': class=', class(x), ', typeof=', typeof(x),
', is.numeric=', is.numeric(x), '
')
}
audit_types(1L, 'integer')
audit_types(1.0, 'double')
audit_types(TRUE, 'logical')
audit_types('hello', 'character')
audit_types(1+2i, 'complex')よくある質問
「Rの型システム概説」レッスンは無料ですか?
はい。「Rの型システム概説」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、R Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 R Academyコースには全4レッスンが含まれています。
「Rの型システム概説」で何を学びますか?
double、integer、character、logical、complex型を理解します。 ブラウザで直接実行するハンズオンコードでR Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
R Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのR Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「Rの型システム概説」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このR Academyレッスンでコードを書いて実行できますか?
はい。すべてのR Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Rの型システム概説
- 数値型間の変換
- 論理型と文字型の変換
- 強制変換の落とし穴とベストプラクティス