R Data Types

Initially in developing any application or doing any programming in any programming language, you might have seen that variables are required to store data that you will be using within a program. Also you might have noticed (usually in common or most programming languages like C or C++) that those variables are assigned with a type and that variable has to assign that type of data only. In this chapter you will learn about the concept of data types that R programming uses within its script.

What are Data types in R?

There are several basic data types in R which are of frequent occurrence in coding R calculations and programs. Though seemingly in the clear, they can at a halt deliver surprises. Here you will try to understand all of the different forms of data type well by direct testing with the R code.

Here is the list of all the data types provided by R:
  • Numeric
  • Integer
  • Complex
  • Logical
  • Character

Numeric Data type

Decimal values are referred as numeric data types in R. This is the default working out data type. If you assign a decimal value for any variable x like given below, x will become a numeric type.

> g = 62.4       # assign a decimal value to g

> g              # print the variable's value - g

Integer Data type

If you want to create any integer variable in R, you have to invoke the as.integer() function to define any integer type data. You can be certain that y is definitely an integer by applying the is.integer() function.

> s = as.integer(3)

> s          # print the value of s

Fortunately, you can drive in a numeric value into an integer with this above mentioned as.integer() function like this:

> as.integer(3.14)    # drives in a numeric value

But it will work like type casting where the value of 3.14 gets changed to 3.

Complex Data type

A complex value for coding in R can be defined using the pure imaginary values 'i'.

> k = 1 + 2i     # creating a complex number

> k              # printing the value of k

The below mentioned example gives an error since −1 is not a complex value.

> sqrt(−1)       # square root of −1

And the error message will be something like this:

Warning message:

In sqrt(−1) : NaNs produced

Logical Data type

A logical value is mostly created when comparison between variables are done. Example will be like:

>   a = 4; b = 6               # sample values

>   g = a > b              # is a larger than b?

>   g               # print the logical value

Output:

[1] False

Character data type

A character object can be used for representing string values in R. You have to convert objects into character values using the as.character() function within your code like this:

> g = as.character(62.48)

> g              # prints the character string

Output:

[1] "3.14"
> class(s)       # print the class name of s

Output:

[1] "character"

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.