Lab session 0
Introduction to R Practical
Heather Turner
12 September 2016
-
Download
duncan.txtfrom the course website. Then, in RStudio, click on the Import Dataset button on the Environment tab in the top-right pane. Select From Local File and select theduncan.txtfile to import and view the data.The data are from the 1971 census of Canada, with the following variables
- occupation
-
named occupation
- education
-
average education in years
- income
-
average income in dollars
- women
-
percentage women in occupation
- prestige
-
prestige score for occupation, from earlier survey
- census
-
Canadian census occupation code
- type
-
type of occupation: bc (blue collar), prof (professional, mangerial and technical) and wc (white collar)
Open a new script file to work in, save it as a
.Rfile. (Just give it any file name you want, ending in .R) -
Use
summaryto get a quick summary of the variables. You will see thattypehas missing values. The commandis.na(x)will return a logical vector indicating whether each element ofxisNA(TRUE) or not (FALSE). Look up the help for the functionsubsetand find out how to use such a logical vector to obtain the subset of the data for whichtypeisNA. In summaries involvingtypethese observations will be silently excluded. -
Using
min, assign the minimum proportion of women to a name. The commandx == awill return a logical vector indicating whether each element ofxis equal toa(TRUE) or not (FALSE). Usesubsetto obtain the rows of data where the proportion of women is equal to the minimum value. Repeat the process to obtain rows corresponding to the maximum. -
Create a histogram and then a density plot of the prestige variable and compare the output. Use the code completion tools in RStudio to look at the second argument of
histand try modifying. -
Create a boxplot of years of education by occupation type.
-
Create a scatterplot of prestige against income. Look at the help for
logand then create a plot of prestige against log income with base 10.