You are on page 1of 4

SYCET/ CSE/LOM/IDSR

SHREEYASH COLLEGE OF ENGINEERING & LABORATORY MANUAL


TECHNOLOGY, AURANGABAD.
PRACTICAL EXPERIMENT INSTRUCTION SHEET
EXPERIMENT TITLE:
Write a R script to demonstrate the use of Data Types
1) Addition of two Numeric numbers.
2) Subtraction of two Numeric numbers.
3) Multiplication of two Integer numbers.
4) Division of two Integer numbers.
EXPERIMENT NO. : 02 SUBJECT : INTRODUCTION TO DATA SCIENCE USING R

DEPT: COMPUTER SCIENCE & ENGINEERING SEMESTER :IV

Aim: Write a R script to demonstrate the use of Data Types


1) Addition of two Numeric numbers.
2) Subtraction of two Numeric numbers.
3) Multiplication of two Integer numbers.
4) Division of two Integer numbers.

Hardware Requirement:
Intel P-IV 2.70 GHz Processor, 1 GB RAM, 256 GB HDD, 15” LCD Monitor, Keyboard,
Mouse.
Software Requirement:
R Tool, R Studio, Ubuntu 14.04 LTS

Theory:
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.

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.
1) Numeric
2) Integer
3) Complex
SYCET/ CSE/LOM/IDSR

4) Logical
5) Character
6) Raw

 Numeric Data Types


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 Types:


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:
SYCET/ CSE/LOM/IDSR

In sqrt(−1) : NaNs produced

 Logical Data Types:


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 Types:


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"

 Raw Data Types:


"Hello" is stored as 48 65 6c 6c 6f

v <- charToRaw("Hello")
print(class(v))
it produces the following result −

Output:
[1] "raw"
SYCET/ CSE/LOM/IDSR

Conclusion:
Hence we have studied data types in R.

You might also like