Basic R Commands
Basic R Commands
Fabian Fopp
11/25/2020
Basic R commands
To assign an object we use the assignemnt arrow “<–”.
x <- 5
In this case x is a number, but we can also assign more complex stuctures such as vectors, matrices, dataframes or lists. Results of operations
assigned to objects are usually not printed in R. If we put the entire command in brackets we can see the result in the console.
## [1] 1 2 3 4 5 6 7 8 9 10
str(x)
## int [1:10] 1 2 3 4 5 6 7 8 9 10
x is a one dimensional array (=vector). The data type of this vector is “int” which stands for integer. There are also other data types such as
numeric, character and factor.
This time the data type of x is classified as character (“chr”). Imagine however x stores different treatment levels in an experiment, such as a low
nitrogen, medium nitrogen and high nitrogen treatment. In this case we want to treat x as a factor with different levels (in this case three).
To access an element of a vector we should first know how long this vector is. We can do this using the length() function:
length(x)
## [1] 5
We can then use square brackets for the indexing. The index of an element is basically the address (position) inside of the vector in which it is
stored.
x[3]
## [1] "ti_1"
## [1] "TI_1"
As mentioned before, we store all observation data from the field in a dataframe. A dataframe has two dimensions with n rows and m columns. A
dataframe can (in contrast to a matrix) also store different data types in different columns.
Now we will have a look at how to access a certain element of a dataframe (or matrix). Let’s take our dataframe z as an example, which has three
columns and five rows. If we don’t know the dimensions of our dataframe we can use the dim() funtion to find out:
dim(z)
## [1] 5 3
The indexing of dataframes works similar to the indexing of vectors except that we are now dealing with two dimensions. The first value in the
square brackets is the index of the row, the second one is the index of the column.
z[1, 2]
## [1] "low"
z[, 2]
Especially if we are dealing with large dataframes this can get tedious and we can easily make mistakes when accidentally selecting the wrong
column index. It makes things much easier if we name the different columns of our dataframe.
z$treatment[1]
## [1] "low"
If we want to print the whole treatment column we can just omit the square brackets with the index:
z$treatment
Finally, if we want to select multiple rows but not all of them, we can do that using the colon.
z$treatment[3:nrow(z)]
If we check the structure of our dataframe z we can see that R treats all columns as factors. We want to change the plot_id column to character
and the ph column to numeric (so that we can also perform calculations later). When converting factor to numeric it is important to convert it to
character first:
str(z)