top of page

2. R specific words and definitions you need to learn

R object

R makes “objects”. Each time you run an analysis in R or import data into R it makes a new object. If you have imported data then R will likely create an object called a “dataframe”). If you ran an analysis, it will make an “object” of the results. If you don’t tell R the name you would like to give a new object, then R won’t give it a name, and you won't be able to easily find it again. So make sure you name all your newly created objects (and give them sensible names).

To give something (an object) a name in R you “point to” (using an arrow and dash symbol) the name you want the object to be called and then issue R a command/instructions. For example, “myANOVA<-aov(y~x, data=mydata)” is asking R to do an ANOVA on the dataframe called “mydata”. This dataframe contains the response/dependent variable “y” and the predictor/independent variable “x”. These variables, “y” and “x” are going to be the column headings in your data frame. In the command line, you have to type these names, "y" and "x" EXACTLY as they are in your dataframe (how to make a dataframe is explained below) otherwise R will be confused. Through running this command, you will have created the object "myANOVA".

R function

R can perform a vast array of functions. You can tell whether a command contained code asking R to perform a function as you will see words directly in front of parentheses and then some more words/symbols inside the parentheses. For example this the command for asking R to perform an ANOVA function, “aov(y~x)”. Here “aov” is the function. This code is asking R to perform an ANOVA to see if the response variable "y" is "explained by" (represented by the "~") the independent variable "x"). Other functions we will use are “lm” (aka linear model) for regression, “chisq.test”, “t.test”, “pairwise.t.test”, and many more! :D

Dollar sign “$” symbol usage in R

Depending on the analysis you want to conduct you sometimes need to tell R to look directly at individual columns in your dataset (for the analyses you will be conducting here, your datasets will be in the form of R “dataframes”). Usually is doesn’t happen, but sometimes you have to directly tell R to regard certain data points/columns/rows of data in your dataframe as either continuous or categorical variables. You can instruct R on which column of a dataframe you would like it to look at by writing a command that uses a combination of the dataframe’s name, the dollar sign and the name of the column. Let’s say you have a dataframe called “birds” comprised of two columns “temperature” and “movement” and you want R to conduct an analysis that tests whether temperature impacts kiwi movement. Sometimes when you execute your line of code it won’t work because you have not directly pointed R to the columns of data it needs to “see” to conduct the test properly. To point R to the column “temperature” you would write: “birds$temperature”. To point R to the column “movement”, you would write “birds$movement”. Here is an entire example line of code for an ANOVA:

moveANOVA<-aov(birds$movement ~ birds$temperature, data = birds)

R will now create an new ANOVA object called “moveANOVA” which tested whether movement was dependent on temperature.

R vector

In R, vector data is the same as “scale” data is SPSS - it’s what R calls a continuous variable (and a count variable too). If R has a problem understanding what type of data you have imported you may need to explain this to R directly. For example, you may have a column of continuous variable data, like temperature in the dataframe “birds” from the example above. For whatever reason, R might get confused and regard these values as being those of a categorical variable or a “factor”, rather than a “vector”. You can check whether R is regarding your data as a vector use the following command:

is.vector(birds$temperature)

If R returns the following answer

[1] FALSE

Then you need to correct the problem using this command:

birds$temperature<-as.vector(birds$temperature)

You can now check that it worked using:

is.vector(birds$temperature)

You should receive the answer:

[1] TRUE

If you ask:

is.factor(birds$temperature)

R should answer:

[1] FALSE

R factor

A factor is what R calls (and many other programs call) a categorical variable. You can follow these instructions for checking whether your categorical variable is being treated as a factor by R too, but obviously, instead of writing commands about a vector, you will be writing about a factor. Let’s say you have a dataframe called “sharedbrooder” which contained two columns of data, “yes.n

Godzilla, Australia's most famous frog

o” and “growth”. This dataframe contains a categorical or “factor” variable column “yes.no” and a continuous variable column “growth”. As in the example above, you can check that “yes.no” is a factor by using, “is.vector(sharedbrooder$yes.no)”, and if you get the answer “TRUE”, then you should use the command, “sharedbrooder$yes.no<-as.factor(shared.brooder$yes.no) to change the variable to a factor.

bottom of page