R – Datatypes

R Programming language has some basic data types.

  • Numeric
  • Integer
  • Complex
  • Logical
  • Character

 

Numeric Datatype:

Numeric Datatype can be assigned a decimal or integer value.

Examples:

    > a =5.7 # assign a decimal value
    > a
    [1]5.7
    > class(a) # prints the class name of “a”
    [1] “numeric”
    > b = 8 # assign an integer value and it will be saved as a numeric value
    > b
    [1]8
    > class (b) # prints the class name of “b”

Integer Datatype:

Integer datatype can be assigned using as.integer () function.

Examples:

    > x = as.integer (5)
    > x   # print value of x
    > [1]5
    > class(x) #print class name of x
    [1] “integer”
    > is.integer (x) # checks if x is an integer
    [1] TRUE





Complex Datatype:

Complex datatype is used to assign complex numbers

Examples:

    > x = 3 + 9y #creates a complex number
    > x # prints the value of x

[1] 3 +9y

    > class(x) # print class name of x
    [1]” complex”

Logical Datatype:

Logical datatype is created using comparison operator

Examples:

    > a =5, b =7, c =10
    > d=a +b < c # is d less than c?
    > d
    [1] FALSE
    > class (d)
    [1] “logical”

Character Datatype:

Character datatype is used for assigning string values

Eamples:

    > a = as.character(2.842)
    > a # print the character string
    [1] 2.842
    > class(a) # print class name of a
    [1] “character”