Data Prep

Load Libraries

# if you haven't used a given package before, you'll need to download it first
# delete the "#" before the install function and run it to download
# then run the library function calling that package

# install.packages("naniar")

# library(naniar) # for the gg_miss-upset() command

Import Data

Import the full project data into a dataframe, call it “df”. Replace ‘DOWLOADED FILE NAME’ with the actual file name of your dataset (either for the ARC or EAMMi2).

Note: If you named your folder something else, you will also need to replace ‘Data’ with whatever the name of your folder is where you saved the dataset in.

# for the lab, you'll import your chosen project's full dataset CSV file you downloaded

# note: If you named your folder something else, you will also need to replace 'Data' with whatever the name of your folder is where you saved the project dataset in.

# df <- read.csv(file="Data/DOWLOADED FILE NAME.csv", header=T)

# df <- read.csv(file="Data/eammi2_data_final_SP25.csv", header=T)

Viewing Data

These are commands useful for viewing a data frame.

# you can also click the object (the little table picture) in the environment tab to view it in a new window

names(df)  # all the variable name in the data frame
## NULL
head(df)   # first 6 lines of data in the data frame
##                                               
## 1 function (x, df1, df2, ncp, log = FALSE)    
## 2 {                                           
## 3     if (missing(ncp))                       
## 4         .Call(C_df, x, df1, df2, log)       
## 5     else .Call(C_dnf, x, df1, df2, ncp, log)
## 6 }
str(df)    # shows all the variables in the data frame and their classification type (e.g., numeric, string, character,etc.)
## function (x, df1, df2, ncp, log = FALSE)

Subsetting Data

Open your mini codebook and get the names of your variables (first column). Then enter this list of names within the “select=c()” argument to subset those columns from the dataframe “df” into a new one “d”.

Replace “variable1, variable2,…” with your variables names.

# Make sure to keep the "ResponseId" variable first in the "select" argument

#d <- subset(df, select=c(ResponseId, variable1, variable2, variable3, variable4, variable5, variable6))

# d <- subset(df, select=c(ResponseID, gender, party_rc, pipwd, swb, mindful, efficacy))

#Your new data frame should contain 7 variables (ResponseId, + your 2 categorical, + your 4 continuous)


# names(d)  # all the variable name in the data frame
# head(d)   # first 6 lines of data in the data frame
# str(d)    # shows all the variables in the data frame and their classification type (e.g., numeric, string, character,etc.)

Missing Data

# use the gg_miss_upset() command for a visualization of your missing data

# gg_miss_upset(d[-1], nsets = 6) 
# the [-1] tells the function to ignore the first column i.e., variable -- we are doing this because here it is just the ID variable, we don't need to check it for missingness because everyone was assigned a random ID

# use the na.omit() command to create a new dataframe in which any participants with missing data are dropped from the dataframe
#d2 <- na.omit(d)

# 3182-1615

#1567/3182
# calc the total number of participants dropped, then convert to % and insert both the number and % in the text below.
# insert the total number of participants in your d2 in the text where it says N = #.

We looked at the missing data in our dataset, and found that 1567, or about 49.24%, of the participants in our sample skipped at least one item. We dropped these participants from our analysis, which is not advisable and runs the risk of dropping vulnerable groups or skewing results. However, we will proceed for the sake of this class using the reduced dataset, N = 1615.

Exporting Cleaned Data

Our last step is to export the data frame after we’ve dropped NAs so that it can be used in future HWs.

# use the "write.cvs" function to export the cleaned data
# please keep the file name as 'projectdata'
# note: you only need to change 'Data' before the slash if you named your folder something else

#write.csv(d2, file="data/projectdata.csv", row.names = F)