Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

You have to close the Data Editor window in order to proceed.

For Mac users: The Mac version of RStudio requires the X Window system for some
functions, like edit() , to work. Apple used to include this capability with the
Mac,
but not any more. Nowadays, you have to download and install XQuartz.
Extracting data from a data frame
Suppose you want to do a quick check on the average empathy scores for people
with blue eyes versus people with green eyes versus people with hazel eyes.
The first task is to extract the empathy scores for each eye color and create
vectors:
> e.blue <- e$empathy_score[e$feye_color=="blue"]
> e.green <- e$empathy_score[e$feye_color=="green"]
> e.hazel <- e$empathy_score[e$feye_color=="hazel"]
Note the double equal-sign ( == ) in brackets. This is a logical operator. Think of
it as
“if e$feye_color is equal to ‘blue.’”
The double equal-sign ( a==b ) distinguishes the logical operator (“if a equals b”)
from the assignment operator (a=b; “set a equal to b”).
Next, you create a vector of the averages:
> e.averages <- c(mean(e.blue),mean(e.green),mean(e.hazel))
Then you use length() to create a vector of the number of scores in each eye-
color group:
> e.amounts <- c(length(e.blue), length(e.green),
length(e.hazel))
And then you create a vector of the colors:
> colors <- c("blue","green","hazel")
Now you create a 3-column data frame with color in one column, the correspond-
ing average empathy in the next column, and the number of scores in each eye
color group in the last column:
> e.averages.frame <- data.frame(color=colors,
average=e.averages, n=e.amounts)

You might also like