Go back to Index

  1. Estimate the mean of schooling years for people that were 40 years old or less and voted in the 1992 presidential election.
turnout_short 
which(turnout_short$vote == 1 & turnout_short$age <= 40)        # Cases that satisfy this condition

# Two alternative notations to get the vector of schooling years
turnout_short[turnout_short$vote==1 & turnout_short$age <= 40, "educate"]
turnout_short$educate[turnout_short$vote==1 & turnout_short$age <= 40]

# Now we can estimate the mean
mean(turnout_short$educate[turnout_short$vote==1 & turnout_short$age <= 40])
  1. The function ‘with()’ constructs a local environment defined by the data.No additional references are required.
    Syntax: with(data, ‘expression’). Use the function with() to obtain the education level for people that were 40 years old or less and voted in the 1992 presidential election.
with(turnout_short, educate[vote==1 & age <= 40])
with(turnout_short, turnout_short[vote==1 & age <= 40, "educate"])
  1. Alternatively, you can use the function ‘subset()’. ‘subset()’ returns subsets of vectors, matrices or data frames which meet conditions. Syntax: ’subset(data.frame, subset = conditions for rows, select = conditions for columns). First, from the dataset turnout_short get the people who voted using the function subset. Second, with the same function get the people that were 40 years old or less and voted Finally, get the level of schooling for these people.
subset(turnout_short, vote ==1)                     # People that voted in the 1992 presidential election
subset(turnout_short, vote ==1 & age <=40)            # People that had 40 years old or less and voted
subset(turnout_short, vote ==1 & age <=40, "educate") # Schooling years for people that had 40 years old or less and voted
  1. For the dataset turnout_short, drop the cases for people that were 30 years old or less and voted. Hint. You can use the ‘which()’ function.
turnout_short[-which(turnout_short$vote == 1 & turnout_short$age <= 30), ] # Drop the cases that satisfy the condition
turnout_short[which(turnout_short$vote==0 | turnout_short$age > 30), ] # Keep the cases that satisfy the condition