أرقام R


أعداد

توجد ثلاثة أنواع من الأرقام في R:

  • 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)