Saturday, November 21, 2020

Data Types in R Programming

 In programming languages, we need to use various variables to store various information. Variables are the reserved memory location to store values. As we create a variable in our program, some space is reserved in memory.

In R, there are several data types such as integer, string, etc. The operating system allocates memory based on the data type of the variable and decides what can be stored in the reserved memory.

There are the following data types which are used in R programming:

R Programming Data Types

Data typeExampleDescription
LogicalTrue, FalseIt is a special data type for data with only two possible values which can be construed as true/false.
Numeric12,32,112,5432Decimal value is called numeric in R, and it is the default computational data type.
Integer3L, 66L, 2346LHere, L tells R to store the value as an integer,
ComplexZ=1+2i, t=7+3iA complex value in R is defined as the pure imaginary value i.
Character'a', '"good'", "TRUE", '35.4'In R programming, a character is used to represent string values. We convert objects into character values with the help ofas.character() function.
RawA raw data type is used to holds raw bytes.

Let's see an example for better understanding of data types:

  1. #Logical Data type  
  2. variable_logical<- TRUE  
  3. cat(variable_logical,"\n")  
  4. cat("The data type of variable_logical is ",class(variable_logical),"\n\n")  
  5.   
  6. #Numeric Data type  
  7. variable_numeric<- 3532  
  8. cat(variable_numeric,"\n")     
  9. cat("The data type of variable_numeric is ",class(variable_numeric),"\n\n")  
  10.   
  11. #Integer Data type  
  12. variable_integer<- 133L  
  13. cat(variable_integer,"\n")   
  14. cat("The data type of variable_integer is ",class(variable_integer),"\n\n")  
  15.   
  16. #Complex Data type  
  17. variable_complex<- 3+2i  
  18. cat(variable_complex,"\n")  
  19. cat("The data type of variable_complex is ",class(variable_complex),"\n\n")  
  20.   
  21. #Character Data type  
  22. variable_char<- "Learning r programming"  
  23. cat(variable_char,"\n")  
  24. cat("The data type of variable_char is ",class(variable_char),"\n\n")  
  25.   
  26. #Raw Data type  
  27. variable_raw<- charToRaw("Learning r programming")  
  28. cat(variable_raw,"\n")  
  29. cat("The data type of variable_char is ",class(variable_raw),"\n\n")  

When we execute the following program, it will give us the following output:

R Programming Data Types

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...