Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Basic R Commands

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Basic R Commands

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

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.

(x <- seq(1, 10))

## [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.

x <- c("GR_1", "GR_2", "ti_1", "VS_1", "VS_2")


str(x)

## chr [1:5] "GR_1" "GR_2" "ti_1" "VS_1" "VS_2"

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).

y <- as.factor(c("low", "low", "high", "medium", "low"))


str(y)

## Factor w/ 3 levels "high","low","medium": 2 2 1 3 2

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"

(x[3] <- "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.

z <- as.data.frame(cbind(c("GR_1", "GR_2", "TI_1", "VS_1", "VS_2"),


c("low", "low", "high", "medium", "low"), c(4, 4, 8, 6, 5)))

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]

## [1] "low" "low" "high" "medium" "low"

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.

colnames(z) <- c("plot_id", "treatment", "ph")

We can now access the columns by names using the $ sign.

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

## [1] "low" "low" "high" "medium" "low"

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)]

## [1] "high" "medium" "low"

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)

## 'data.frame': 5 obs. of 3 variables:


## $ plot_id : chr "GR_1" "GR_2" "TI_1" "VS_1" ...
## $ treatment: chr "low" "low" "high" "medium" ...
## $ ph : chr "4" "4" "8" "6" ...

z$plot_id <- as.character(z$plot_id)


z$ph <- as.numeric(as.character(z$ph))
str(z)

## 'data.frame': 5 obs. of 3 variables:


## $ plot_id : chr "GR_1" "GR_2" "TI_1" "VS_1" ...
## $ treatment: chr "low" "low" "high" "medium" ...
## $ ph : num 4 4 8 6 5

You might also like