Introduction on many basics are covered other places in the R-tutorial. I encourage you to look around there if you need help with basic operations. Still, below I mention some basics that you will need.
Creating objects
R treats our data as objects. They can be single vectors, matrices, lists and data frames.
R stores nearly all data in vectors (the others object types consist of vectors). What’s contained in an vector is given within c(). An object containing a single element is a vector of length 1 (e.g. c(15)). Vector elements can be of different classes.
In the our course, we will use imported data, but it is still usefull to know how to create simple objects, which will be needed for some operations.
<- is the assignment operator. Assigns values on the right to objects on the left. In many cases it is similar to =, but using = instead of <- can lead to issues later on. Use = to specify the values of arguments in functions:
[code language=”r”]read.table(file="data/some_data.txt")[/code]
Objects containing a single element:
[code language=”r”]x <- 10 #assigning x the number 10;
X <- c(10) #does the same as above
x #Returns the value of x[/code]
We can overwrite the content we previously assigned to an object, e.g.
[code language=”r”]x <- 15 #we now assign 15 to x, the previous value is overwritten
x[/code]
To create a longer vector, which contain a series of elements we use:
[code language=”r”]y <- c(2,4,6) #the values for the vector are given in c(), separated by,
y
y <- c(x,5,x) #using previously assigned values for x to create the vector
y[/code]
Creating dataframes:
You can create dataframes directly in R, but for the course, we will import the dataframes we want to work with.
[code language=”r”]#creating example dataframe (df);
ID <- 1:16 #creating vector with numbers from 1 to 16 => IDs for 16 observations
year <- c(rep("2015", 4), rep("2016", 4), rep("2017", 4), rep("2018", 4)) #creating a vector with 4 repetitions for each year
station<-rep(c("Isfj", "Kongsfj", "Hinlopen", "MIZ"),4) #creating vector with 4 repetitions of the vector given within
gear <- rep(c("Pelagic","Benthic"),8)
values1 <- c(rnorm(8, mean=30, sd=5),rnorm(8,mean=40,sd=5)) #creating value vector from a normal distribution; e.g. abundances
values2<- c(rnorm(8,mean=55, sd=5),rnorm(8,mean=35,sd=5))
df <- data.frame(ID, year, station, gear, values1, values2) #combining the different vectors to a dataframe (all vectors need to have same length)
[/code]
We can use the objects we created in functions later on, or create new objects from combinations of functions and objects:
[code language=”r”]y <- c(2,4,6)
mean(y)# calculate the mean of the elements in vector y
mean_y <- mean(y) #saving the calculated mean in a new object for later use[/code]
Subsetting vectors and dataframes
We can access single elements in a vector or dataframe using []. The [] specifies the indices of the vector/dataframe.
[code language=”r”]x <- c(2,4,6)
x[1] #returns the first element of vector x
x[3] #returns the 3rd element of vector x
#for dataframes
Data[1,2] #Returns the element in the first column and second row
Data[5,2] #Returns the element in the 5th column and 2nd row
[/code]
If you want to access a series of elements you can use:
[code language=”r”]
x <- c(2,4,6,8,10,12)
x[2:4] #returns all values from the 2nd to the 4th element
x[c(2,3,2)] #returns the values of the 2nd, 3rd and again 2nd element of the vector.
[/code]
We can also access elements through specifying their name. This is particularly usefull in dataframes:
[code language=”r”]
x[,"Var1"] #returns variable values as vector
x["Var1"] #returns variable values as list
[/code]
You can find more about options on subsetting, including logical arguments here.