3
3

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 5 years have passed since last update.

Rのデータ型

Last updated at Posted at 2020-04-13

本稿の目的・対象

以下のことを知りたい人・思い出したい人向けのメモ的postです。

  • Rのデータ型の種類はどのようなものがあるの?
  • typeってなに?
  • modeってなに?
  • classってなに?
  • データ型はどうやって調べるの?
  • データ型はどうやって変換するの?

Rのデータ型

R のオブジェクトは、type、mode および class の 3 種類の型が存在する。

type

typeは、データを保存できる変数のオブジェクトに対する型であり、ベクトル型、リスト型などがある。

mode

modeは、オブジェクトに格納されている要素に対する型であり、理論型(logic)、実数型(numeric)、整数型(integer)、文字型(character)、日付と時間(Date/POSIXct)、関数型などがある。

class

class は、オブジェクトの属性に対する型であり、行列型、因子型、データフレーム型などがある。
特にclass が定義されていない場合は、mode または type の型を継承する

データ型の調べ方

typeを調べる時・・・typeof()
modeを調べる時・・・mode()
classを調べる時・・・class()

R
x <- c(1.1, 2.2, 3.3, 4.4, 5.5)
typeof(x)
[1] "double"

mode(x)
[1] "numeric"

class(x)
[1] "numeric"

データ型を変換する

Rのオブジェクト型は相互に変換可能である。変換の指定方法を表にまとめる(検査の仕方もついでにまとめる)

mode numeric integer complex character logical ※bool
変換 mode() is.numeric() is.integer() is.complex() is.character() is.logical
検査 storage.mode() as.numeric() as.integer() as.complex() as.character() as.logical()

ここで、ベクトルの型の間には以下の大小関係がある.

character > complex > numeric > logical > NULL

ベクトルの要素は全て同じ型である必要があるので,異なった型のデータを集めてベクトルを作る時、上記の大小関係に従って「最も大きい」型に強制的に揃えられる.

参考URL

R-source 25

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?