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.
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
mtcarsImport mtcars.csv from the Project’s data folder.
cars.Write code that answers each question:
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
mpg three waysCalculate the mean, median, and standard deviation of mpg. Use a different column-selection form for each calculation:
mpg with $.mpg by its numeric column position.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
disp valuesSelect 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
wt valuesCreate 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
Create a new data-frame object containing these columns in this exact order:
X (car names)cylhpwtgearmpgThe 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
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.
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: