<- 1
my_first_object my_first_object
[1] 1
Now we have a very fancy calculator lets see what R can actually be more useful for. One of the most basic but at the same time useful things is the ability to save objects in R. An object can be anything with something assigned to it. You can store a number, text or whole datasets in an object. Another way to think about objects is that they are like pointers. You assign a name to an object which then points to something. Whenever you call the name of the object it refers to whatever it points to. This means that you can easily create objects and then make calculations using the objects. These calculations can then be easily reused no matter what is inside the object as long as you don’t get any errors.
Creating an object in R is actually very simple. You just assign whatever you want to store in the object to a name you want to use to refer to your object. There are a few ways in which you can make the assignment: <-
, =
or sometimes ->
. After creating an object it should appear in your global environment. When you want to access whatever is stored in your object you can just use the name of the object.
You can also easily update/change an object though as a general rule in R you can’t change an object once you created it. The way to actually change it is to recreate an object by assigning a new value to the same name.
You can perform basic mathematical operations on objects just like you would make them on values. You can also assign such result to a new object
If we now update one of the objects and repeat the same command we will get updated result.
You can also make a number of logical operations. These will generally either be comparisons (is something larger than something else?) or logical operation (just like logic 101 operations like and, or, not). These operations result in a boolean value.
Comparisons are fairly straightforward. You make them with >
, <
and =
. One thing to remember is that to test equality you need double equality sign: ==
. A single =
is used for assigning objects:
[1] TRUE
[1] FALSE
There are a few things to keep in mind when making comparisons, especially of other types of objects. If you compare a true/false value to a number, R will convert the boolean to 0 or 1. If you compare strings R will start with the first letter and if they are the same it will move on to the second letter and so on. Alphabetical order determines which string is ‘larger’. Note that for some special signs (like polish signs etc) this might get weird. Another thing to note is that R is case sensitive - ‘A’ is not the same as ‘a’.
The other type of logical operations allow you to create more complicated comparisons. Generally &
evaluates as TRUE
only if both parts of the expression are TRUE
. |
(or) evaluates to true only if at least one part of the expression is TRUE
. The last operator is the not one: !
. It turns TRUE
into FALSE
and FALSE
into TRUE
.
[1] FALSE
[1] TRUE
[1] FALSE
These operation will be especially useful when we will be filtering datasets (e.g. we want only those observations that have age higher than some value and come from a given region).
One might think that perhaps you can add strings together? Ultimately maybe something will come out of it? Lets test it out:
You can’t add strings together the same way you would with numbers. WWorking with strings will require some functions. We’ll dig into more details on functions in the future but for now you can think about them as operations that take some input, do something with it and produce some output. Using function generally looks like this function_name(arguments). Arguments of the functions are the inputs.
You can put together two strings using paste0()
and cat()
functions. If you want to learn more about them you can look up their documentation with ?paste0
and ?cat
. Notice that cat()
inserts a space between the words and paaste0
does not.
If you are not sure what type of value you are working with you can use class()
to check what type it is.
There are situations in which you might want to change the class of the object you are working with. A common situation is when you load a dataset and a variable that should be numeric is loaded as character. It is possible to convert one type into another (but remember about the hierarchy, not everything can be converted to any other type). As a general rule all functions for converting types have the form as.
so e.g. as.numeric()
will convert a value to a numeric one.
However, be mindful that if something can’t be converted to the desired type R will try to coerce it anyway and will produce missing values (coded in R as NA
).
[1] NA
[1] "logical"
Notice that the code above does produce an object of class logical
but it stores only NA
. This can be sometimes tricky because you might not know that something went wrongSome other things will work however - you can convert numeric values into logical ones. 0
will be converted to FALSE
and everything else into TRUE
.
p
and q
):!(p & q) | !(p | !q)
a
, b
, c
, d
, e
and assign them values 1
, 15
, 3
, 4.5
, 6
. Calculate the mean of all these elements and then calculate the sum of squared differences of each value from the mean.TRUE == "TRUE"
? Why?