Friday 17 October 2014

Functions for Arithmatic

The following are some of the useful functions of arithmatic in R apart from +, -, *, /, ^ and sqrt()


To set limits before using scientific notation for large numbers, you can set the 'scipen' parameter within options(). The default limit is 10^12 for displaying in R console and 10^7 for the graphic device.

The below is larger than 10^13, and R reverts to using scientific notation.

31423190532586

3.142319e+13


By setting the option to scipen=15, the same number is retained in its natural form.

options(scipen=15)
31423190532586

31423190532586


For plotting:

plot(1:10,seq(0,100000000,length=10))
 


















options(scipen=8)
plot(1:10,seq(0,100000000,length=10))




















To round the figure to significant figures, you can use the below function.

signif(12645.654,digits=2)

13000


signif(0.00034526,digits=2)

0.00035



For rounding figures, you can do the following.

round(12645.654)  #rounds to integer

12646


round(12645.654,digits=2) #rounds to second decimal place

12645.65


round(0.00034526,digits=5)

0.00035



To find ceiling (smallest integer large than the given number) of a figure, you can do the following.

ceiling(12645.654)  

12646


ceiling(0.00034526)  

1


To find ceiling (largest integer smaller than the given number) of a figure, you can do the following.

floor(12645.654)

12645


floor(0.00034526)  

0



To find absolute figure, you can do the following.

abs(-243)

243


To find the remainder of a division, you can use '%%', but this does not work well with large numbers

15%%2

1


To find quotient of a division (integer part of division output), you can use '%/%'.

15%/%2

7




No comments:

Post a Comment