Skip to main content

1.4) Objects And How To Handle Them


To store anything, make up a name and use an assignment arrow: e.g. x <- 3 (“3 is assigned to x”). The arrow is simply made of a less-than or greater-than symbol and a hyphen. (Equals signs are normally used within functions, not to store data.) The name x now refers to an object that you’ve just created. Try and give your objects names that will remind you what they are: you can use upper and lower case letters, underscores and dots – but spaces and other punctuation marks aren’t allowed. If you ever want to store characters or text as the contents of an object, you must put inverted commas " " around them to show you aren’t referring to the name of some object.

D. If you type an expression such as x*2, the result is printed. If you type a command with an assignment arrow (e.g. P <- 1/3 ), the value is stored but not printed. If you type the name of an existing object, its value is printed on the screen. You can store and print at the same time by putting brackets around an assignment. Try those four things now:

# Four types of commands
1.5 * 2
P <- 1/3
P
( Q <- 1/4 )

R ignores spaces in between names and symbols. Some people write their code allbunchedup, but it’s probably easier to read if you use lots of spaces, as shown above.