1C. DATA TABLES
The most common structure used to store data in R is called a data table. In research terms, a data table has columns for independent variables (things we manipulate) and dependent variables (things we measure).
We will continue to work with the data from Lesson 1B.
Sometimes it is easier to rename the columns so they are meaningful to you. Use the following command to do this:
names(mydata) = c("group","eeg","rt")
Note, the c command essentially tells R that the following stuff in brackets is a list of items.
The renaming here is to specify in words a column label for the independent variable (group) and the two dependent variables (eeg, rt).
One of the reasons the table structure is so powerful is that you can use logical indexing. For example, to get all of the rt values for participants with a group code of 2 you just need to simply type mydata$rt[mydata$group==2].
The last thing we will do is define our independent variable group as a factor. This is important for doing statistical analysis as it tells R that group is an independent variable (i.e., a factor) and not data. To do this simply use
mydata$group = factor(mydata$group). mydata will look the same but R will know now that mydata$group is an independent variable.
To see how to use factors, we can run a t-test for now - although we will explain t-tests in detail later. To run the t-test use t.test(mydata$rt ~ mydata$group). With this command you are literally telling R to conduct a t-test examining if there are differences in mydata$rt as a function of group. More on this later
We will continue to work with the data from Lesson 1B.
Sometimes it is easier to rename the columns so they are meaningful to you. Use the following command to do this:
names(mydata) = c("group","eeg","rt")
Note, the c command essentially tells R that the following stuff in brackets is a list of items.
The renaming here is to specify in words a column label for the independent variable (group) and the two dependent variables (eeg, rt).
One of the reasons the table structure is so powerful is that you can use logical indexing. For example, to get all of the rt values for participants with a group code of 2 you just need to simply type mydata$rt[mydata$group==2].
The last thing we will do is define our independent variable group as a factor. This is important for doing statistical analysis as it tells R that group is an independent variable (i.e., a factor) and not data. To do this simply use
mydata$group = factor(mydata$group). mydata will look the same but R will know now that mydata$group is an independent variable.
To see how to use factors, we can run a t-test for now - although we will explain t-tests in detail later. To run the t-test use t.test(mydata$rt ~ mydata$group). With this command you are literally telling R to conduct a t-test examining if there are differences in mydata$rt as a function of group. More on this later