R番号


数字

Rには3つの数値タイプがあります。

  • numeric
  • integer
  • complex

数値タイプの変数は、それらに値を割り当てると作成されます。

x <- 10.5   # numeric
y <- 10L    # integer
z <- 1i     # complex

数値

データ型numericはRで最も一般的な型であり、10.5、55、787のように、小数点付きまたは小数点なしの任意の数値が含まれます。

x <- 10.5
y <- 55

# Print values of x and y
x
y

# Print the class name of x and y
class(x)
class(y)

整数

整数は、小数を含まない数値データです。これは、小数を含む必要のある変数を作成しないことが確実な場合に使用されます。変数を作成するには、整数値の後にinteger 文字を使用する必要があります。L

x <- 1000L
y <- 55L

# Print values of x and y
x
y

# Print the class name of x and y
class(x)
class(y)

複雑

虚数部としてcomplex「」が付いた数値は次のように記述されます。i

x <- 3+5i
y <- 5i

# Print values of x and y
x
y

# Print the class name of x and y
class(x)
class(y)

型変換

次の機能を使用して、あるタイプから別のタイプに変換できます。

  • as.numeric()
  • as.integer()
  • as.complex()

x <- 1L # integer
y <- 2 # numeric

# convert from integer to numeric:
a <- as.numeric(x)

# convert from numeric to integer:
b <- as.integer(y)

# print values of x and y
x
y

# print the class name of a and b
class(a)
class(b)