[1] 3.141593
[1] "numeric"
Type | Description |
---|---|
Atomic element | Single value |
Vector | One-dimensional sequence of elements |
Matrix | Two-dimensional - all elements (rows and columns) are the same type |
Data frame | Spreadsheet - different columns can contain different types of data |
Array | Multi-dimensional |
List | Contains multiple objects- any combination of the above |
The different types of objects have different structures and properties
Atomic elements are single pieces of information, and can have a variety of formats:
Format | Description | Example |
---|---|---|
Integer | Number without decimals | 12 |
Numeric | Number with decimals | 3.85 |
Character | Text | “Golden cat” |
POSIX date/time | Any combination of time & date information | 19:08 14-Apr-2013 |
Logical | True or false | TRUE |
Missing information | Data are not available | NA |
Use the class()
function to find out what type of object you’re working with
Tip
Think of a vector as a single column of data, which can be one or many rows long
Vectors can have the same format as atomic elements, or more complex higher-order formats, including:
Matrices (singular = matrix) properties:
Data frames contain more complex data, including variables of various different types:
Lists are more complex composite objects:
R contains some built-in objects which can be useful for creating datasets or adding information to field data
Let’s take a look at some of them - what type of object are they?:
letters
LETTERS
month.name
month.abb
We create objects in R with the assignment operator <-
, which looks like an arrow pointing left
What the arrow <-
does here is assign that value to the object, in this case called x
:
You could use an equals sign =
instead, but it’s recommended to use the arrow. This is for two reasons:
Warning
Note that if you do use =
, it has an implicit direction:
x = y
is not the same as y = x
x = y
means that the value of y is assigned to xy -> x
also means that the value of y is assigned to x (note arrow direction)The standard approach is to put the name of the new object first, and use a left-pointing arrow
When you type the name of the object, R prints its contents
View the contents of object x
:
Remember that object and function names are case-sensitive1, so if you type a capital X instead, R will give you an error message. Try it in the playground, to see what the error looks like
For more complex objects such as the result of statistical tests, R prints a particular part of the object
Storing information as objects is not limited to a single number. For example, you can store a list of numbers (or text) as a vector, for example 2,4,6,8,10
This vector has an order; the vector 10,8,6,4,2
is not equivalent
The function c()
means concatenate
, and glues
values together into a single list:
You can use your objects in calculations, instead of numbers, for example:
When you include your vector v
in a calculation, R performs the same calculation on every value:
You can also create and use text values in R
You’re beginning to accumulate objects in R now. Let’s remind ourselves what they all are with the list function ls()
:
To delete objects you no longer need, use the remove function rm()
:
If you want to completely clear your workspace by removing all objects, you can use the rm() function and define which objects to remove by supplying R with a list of all the objects in your workspace
Use with caution!
Make sure you have saved the code you used to create the objects if you will need them again in the future