Week 1
Group work

PSYC 70510 · Statistical Methods in Psych Lab I · Section 53950
Lab Instructor: Kaile Smith

Work through the tasks in order with your group. Write and run the code in your own R script. Keep the Week 1 lesson open in another tab so you can return to the worked examples when you need them.

Before you begin, open the supplied StatsLab_Week1_GroupWork.R script and save it inside the Project’s scripts folder.

1 Check the Project location

Print your current working directory in the Console.

Check that the output points to your PSYC-70510 Project folder. If it does not, open the .Rproj file before continuing.

Review the working-directory example

2 Import and print mtcars

Import mtcars.csv from the Project’s data folder.

Review the CSV import example

3 Inspect the data frame

Write code that answers each question:

  1. What are the column names?
  2. How many rows and columns are present?
  3. What type is each column?

Use the inspection functions from the lesson. Keep the output visible so your group can refer to the exact column names and positions in the next tasks.

Review the data-frame inspection examples

4 Describe mpg three ways

Calculate the mean, median, and standard deviation of mpg. Use a different column-selection form for each calculation:

  1. Calculate the mean by selecting mpg with $.
  2. Calculate the median by selecting mpg by its numeric column position.
  3. Calculate the standard deviation by selecting mpg by name inside square brackets.

Use the output from names() to confirm the numeric position of mpg before running the second calculation.

Review the three column-selection forms

5 Select the first 10 disp values

Select the first 10 rows of the disp column, then calculate their mean.

Your code needs to specify both rows and a column inside the square brackets.

Review row-and-column selection

6 Select the odd-numbered wt values

Create a vector containing all odd-numbered row positions in the data frame. Use that vector to select the corresponding values from wt, then calculate their mean.

Review the odd-row sequence example

7 Keep six columns in a new object

Create a new data-frame object containing these columns in this exact order:

  1. X (car names)
  2. cyl
  3. hp
  4. wt
  5. gear
  6. mpg

The course CSV has a blank header above its first column. When read.csv() imports the file, R names that car-name column X.

Create a character vector containing the six column names in the requested order. Use that vector after the comma to select the columns and assign the result to an object named selected_cars. Run names(selected_cars) to check the order.

Review nonconsecutive column selection

8 Challenge: calculate all numeric-column means

selected_cars contains one character column and five numeric columns. Calculate the mean of every numeric column with one sapply() call.

Before applying mean, use indexing to remove X. Save the five numeric columns as an object named numeric_cars, then apply mean to every column in that object.

Review how sapply() works

9 Before you finish

Compare your code with another group member’s script. Different object names are fine; the selected values and calculated results should match.

Be ready to show:

Return to the Week 1 lesson