Tuesday, September 22, 2015

How are numerical quantites handled in R language?

Numeric is the basic data type to represent numbers n the R language. Decimal is the  default computational data type in R.

You assign a decimal value using the following:
> x=3.1429  Assigns a value 3.1429 to a variable x

> x               Print to screen the value of x

[1] 3.1429     Value is printed
Arithmetic Operations:

> 2*x          -What is 2 times x?
[1] 6.2858

> 2+x        -What is 2 added to x?
[1] 5.1429

> x-1         -What is 1 taken away from x?
[1] 2.1429

> x/2         -What is x divided into two?
[1] 1.57145

> x=3.1419
> x^2            -What is x raised to power 2
[1] 9.871536

> x^x            -What is x raised to itself
[1] 36.4862
> sqrt (x^x)  -Square root of x raised to itself
[1] 6.040381
This is a screen shot of how you use the GUI to carry out the above computations:


So how would you assign an integer value to a varible y?

You do so by using the integer function as shown in the following:

> y=as.integer(6)    Assign an integer value of 6 to y
> y                          Print y to screen
[1] 6

> class(y)                Print the class name for y
[1] "integer"

> is.integer(y)          test whether y is an integer
[1] TRUE
Another way to declare a integer constant is to use an L after the integer you want to declare:

> z=4L       -using L after the number declares it as an integer

> class(z)   -test the data type of z
[1] "integer"

You can force a decimal value to an integer but cannot force a string to an integer!

> y=as.integer("John")  Trying to make "John" to an integer
Warning message:
NAs introduced by coercion   Message from R. R does not like it.
R can handle all the mathematical functions you have used in your highschool maths like trgnometric, logarthmic fucntions as well.

Find more about numeric constants in your library that gets  installed with R installation by issuing the command in R as shown here:

Which opens up (

Can R handle complex numbers?

Yes it can handle complex numbers. Complex numbers can have real and imaginary parts like in,
1+2i, where 1 is the real part and 2i is imaginary

Here is how it is used

> x=1+2i  -x is a complex number with real and imaginary parts (it hasto be 2i and not i2)
> y=1-2i  -y is another complex number with real and imaginary parts

> x*y        -What happens when you multiply them?
[1] 5+0i    -you get a real number (real part is 5 and imaginary component is zero

No comments: