Thursday, January 29, 2015

R: Numeric variable


Abstract: Manipulations of numeric type variables in R are introduced.


Create a numeric variable
> a=3.43543
> a
[1] 3.43543

Trim number
> round(3.43456, digits=2) #Round a number with two digits left after the dot:
[1] 3.43
> round(3.53456) # round the number
[1] 4
> floor(3.53456) # return the smallest integer
[1] 3
> ceiling(3.13) # return the highest integer
[1] 4
> trunc(3.43543) # return the integer part
[1] 3
> trunc(3.03543) # return the integer part
[1] 3

Specify the format of the number
> a=0.00000003245678 # give a very small value
> a # here is the default export
[1] 3.245678e-08
> format(a, scientific=T) #different export format
[1] "3.245678e-08"
> format(a, scientific=F)
[1] "0.00000003245678"
> format(a, scientific=F, digits=2)
[1] "0.000000032"
> format(a, scientific=T, digits=2)
[1] "3.2e-08"

Mathematics
> 3.453/4 # divide
[1] 0.86325
> 3.453*4 #multiply
[1] 13.812
> 3.453+4.5 #plus
[1] 7.953
> 3.453-2.5 #substract
[1] 0.953
> 3.453**3 # 3.453 to the power of 3
[1] 41.17084
> sqrt(3.453) #the square root of 3.453
[1] 1.858225
> exp(3.453) #3.453 cubed
[1] 31.59504
> log2(3.453) # logarithm of 3.453 with the base=2
[1] 1.78785
> log(3.453) # logarithm of 3.453 with the base=e
[1] 1.239243
> log10(3.453) # logarithm of 3.453 with the base=10
[1] 0.5381966

Other operations
> pi #get pi
[1] 3.141593
> format(pi, digits=10)
[1] "3.141592654"

> nchar(as.character(235443)) # it is millions
[1] 6



writing date: 2015.01.27

No comments:

Post a Comment