Jump to content

Statistical Analysis: an Introduction using R/R/Storing objects

From Wikibooks, open books for an open world


R is what is known as an "object-oriented" program. Everything (including the numbers you have just typed) is a type of object. Later we will see why this concept is so useful. For the time being, you need only note that you can give a name to an object, which has the effect of storing it for later use. Names can be assigned by using the arrow-like signs <- and -> as demonstrated in the exercise below. Which sign you use depends on whether you prefer putting the name first or last (it may be helpful to think of -> as "put into" and <- as "set to"). Unlike many statistical packages, R does not usually display the results of analyses you perform. Instead, analyses usually end up by producing an object which can be stored. Results can then be obtained from the object at leisure. For this reason, when doing statistics in R, you will often find yourself naming and storing objects. The name you choose should consist of letters, numbers, and the "." character[1], and should not start with a number.
Input:
0.001 -> small.num                #Store the number 0.0001 under the name "small.num" (i.e. put 0.0001 into small.num)
big.num <- 10 * 100               #You can put the name first if you reverse the arrow (set big.num to 10000).
big.num small.num 1               #Now you can treat big.num and small.num as numbers, and use them in calculations
my.result <- big.num small.num 2  #And you can store the result of any calculation
my.result                         #To look at the stored object, just type its name
pi                                #There are some named objects that R provides for you
Result:
> 0.001 -> small.num #Store the number 0.0001 under the name "small.num" (i.e. put 0.0001 into small.num)

> big.num <- 10 * 100 #You can put the name first if you reverse the arrow (set big.num to 10000). > big.num small.num 1 #Now you can treat big.num and small.num as numbers, and use them in calculations [1] 1001.001 > my.result <- big.num small.num 2 #And you can store the result of any calculation > my.result #To look at the stored object, just type its name [1] 1002.001 > pi #There are some named objects that R provides for you [1] 3.141593

Note that when the end result of a command is to store (assign) an object, as on input lines 1, 2, and 4, R doesn't print anything to the screen.


Notes

[edit | edit source]
  1. If you are familiar with computer programming languages, you may be used to using the underscore ("_") character in names. In R, "." is usually used in its place.

R as a calculator · Functions