12.07.2015 Views

R dummies

R dummies

R dummies

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

way the value is stored in memory.R has two main modes for storing numbers. The standard mode is double.In this mode, every number uses 64 bits of memory. The number also is storedin three parts. One bit indicates the sign of the number, 52 bits represent thedecimal part of the number, and the remaining bits represent the exponent.This way, you can store numbers as big as 1.8 × 10 308 in only 64 bits. Theinteger mode takes only 32 bits of memory, and the numbers are representedas binary integers in the memory. So, the largest integer is about 2.1 billion,or, more exactly, 2 31 – 1. That’s 31 bits to represent the number itself, 1 bit torepresent the sign of the number, and –1 because you start at 0.You should use integers if you want to do exact integer calculations onsmall integers or if you want to save memory. Otherwise, the mode doubleworks just fine.You force R to store a number as an integer by adding L after it, as in thefollowing example:> x is.integer(x)[1] TRUEWhatever mode is used to store the value, is.numeric() returns TRUE inboth cases.Creating vectorsTo create a vector from a simple sequence of integers, for example, you usethe colon operator (:). The code 3:7 gives you a vector with the numbers 3 to 7,and 4:-3 creates a vector with the numbers 4 to –3, both in steps of 1. To makebigger or smaller steps in a sequence, use the seq() function. This function’s byargument allows you to specify the amount by which the numbers should increaseor decrease. For a vector with the numbers 4.5 to 2.5 in steps of 0.5, for example,you can use the following code:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!