Hi everyone! Welcome to PSYC 3990.
#Creating an RMarkdown file
Select File, New File, RMarkdown. Put in your title, Author, and select PDF. Then click OK.
#Saving your RMarkdown file
Click File, Save As, and then save in the file path where you will be pulling a majority of your data from.
#Installing packages
You can use one of the following codes to download each package. Replace “ggplot2” with the correct package you want to install. When you use the code dep = TRUE, this just means that it will download any of the other requirements for these packages without always asking you about it.
install.packages
## function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos,
## type), method, available = NULL, destdir = NULL, dependencies = NA,
## type = getOption("pkgType"), configure.args = getOption("configure.args"),
## configure.vars = getOption("configure.vars"), clean = FALSE,
## Ncpus = getOption("Ncpus", 1L), verbose = getOption("verbose"),
## libs_only = FALSE, INSTALL_opts, quiet = FALSE, keep_outputs = FALSE,
## ...)
## {
## if (!is.character(type))
## stop("invalid 'type'; must be a character string")
## type2 <- .Platform$pkgType
## if (type == "binary") {
## if (type2 == "source")
## stop("type 'binary' is not supported on this platform")
## else type <- type2
## if (type == "both" && (!missing(contriburl) || !is.null(available)))
## stop("specifying 'contriburl' or 'available' requires a single type, not type = \"both\"")
## }
## if (is.logical(clean) && clean)
## clean <- "--clean"
## if (is.logical(dependencies) && is.na(dependencies))
## dependencies <- if (!missing(lib) && length(lib) > 1L)
## FALSE
## else c("Depends", "Imports", "LinkingTo")
## get_package_name <- function(pkg) {
## gsub("_[.](zip|tar[.]gz|tar[.]bzip2|tar[.]xz)", "", gsub(.standard_regexps()$valid_package_version,
## "", basename(pkg)))
## }
## getConfigureArgs <- function(pkg) {
## if (.Platform$OS.type == "windows")
## return(character())
## if (length(pkgs) == 1L && length(configure.args) && length(names(configure.args)) ==
## 0L)
## return(paste0("--configure-args=", shQuote(paste(configure.args,
## collapse = " "))))
## pkg <- get_package_name(pkg)
## if (length(configure.args) && length(names(configure.args)) &&
## pkg %in% names(configure.args))
## config <- paste0("--configure-args=", shQuote(paste(configure.args[[pkg]],
## collapse = " ")))
## else config <- character()
## config
## }
## getConfigureVars <- function(pkg) {
## if (.Platform$OS.type == "windows")
## return(character())
## if (length(pkgs) == 1L && length(configure.vars) && length(names(configure.vars)) ==
## 0L)
## return(paste0("--configure-vars=", shQuote(paste(configure.vars,
## collapse = " "))))
## pkg <- get_package_name(pkg)
## if (length(configure.vars) && length(names(configure.vars)) &&
## pkg %in% names(configure.vars))
## config <- paste0("--configure-vars=", shQuote(paste(configure.vars[[pkg]],
## collapse = " ")))
## else config <- character()
## config
## }
## get_install_opts <- function(pkg) {
## if (!length(INSTALL_opts))
## character()
## else paste(INSTALL_opts[[get_package_name(pkg)]], collapse = " ")
## }
## if (missing(pkgs)) {
## if (!interactive())
## stop("no packages were specified")
## if (.Platform$OS.type == "windows" || .Platform$GUI ==
## "AQUA" || (capabilities("tcltk") && capabilities("X11") &&
## suppressWarnings(tcltk::.TkUp))) {
## }
## else stop("no packages were specified")
## if (is.null(available)) {
## av <- available.packages(contriburl = contriburl,
## method = method, ...)
## if (missing(repos))
## repos <- getOption("repos")
## if (type != "both")
## available <- av
## }
## else av <- available
## if (NROW(av)) {
## pkgs <- select.list(sort(unique(rownames(av))), multiple = TRUE,
## title = "Packages", graphics = TRUE)
## }
## }
## if (.Platform$OS.type == "windows" && length(pkgs)) {
## pkgnames <- get_package_name(pkgs)
## inuse <- search()
## inuse <- sub("^package:", "", inuse[grep("^package:",
## inuse)])
## inuse <- pkgnames %in% inuse
## if (any(inuse)) {
## warning(sprintf(ngettext(sum(inuse), "package %s is in use and will not be installed",
## "packages %s are in use and will not be installed"),
## paste(sQuote(pkgnames[inuse]), collapse = ", ")),
## call. = FALSE, domain = NA, immediate. = TRUE)
## pkgs <- pkgs[!inuse]
## }
## }
## if (!length(pkgs))
## return(invisible())
## if (missing(lib) || is.null(lib)) {
## lib <- .libPaths()[1L]
## if (!quiet && length(.libPaths()) > 1L)
## message(sprintf(ngettext(length(pkgs), "Installing package into %s\n(as %s is unspecified)",
## "Installing packages into %s\n(as %s is unspecified)"),
## sQuote(lib), sQuote("lib")), domain = NA)
## }
## ok <- dir.exists(lib) & (file.access(lib, 2) == 0L)
## if (length(lib) > 1 && any(!ok))
## stop(sprintf(ngettext(sum(!ok), "'lib' element %s is not a writable directory",
## "'lib' elements %s are not writable directories"),
## paste(sQuote(lib[!ok]), collapse = ", ")), domain = NA)
## if (length(lib) == 1L && .Platform$OS.type == "windows") {
## ok <- dir.exists(lib)
## if (ok) {
## fn <- file.path(lib, paste0("_test_dir_", Sys.getpid()))
## unlink(fn, recursive = TRUE)
## res <- try(dir.create(fn, showWarnings = FALSE))
## if (inherits(res, "try-error") || !res)
## ok <- FALSE
## else unlink(fn, recursive = TRUE)
## }
## }
## if (length(lib) == 1L && !ok) {
## warning(gettextf("'lib = \"%s\"' is not writable", lib),
## domain = NA, immediate. = TRUE)
## userdir <- unlist(strsplit(Sys.getenv("R_LIBS_USER"),
## .Platform$path.sep))[1L]
## if (interactive()) {
## ans <- askYesNo(gettext("Would you like to use a personal library instead?"),
## default = FALSE)
## if (!isTRUE(ans))
## stop("unable to install packages")
## lib <- userdir
## if (!file.exists(userdir)) {
## ans <- askYesNo(gettextf("Would you like to create a personal library\n%s\nto install packages into?",
## sQuote(userdir)), default = FALSE)
## if (!isTRUE(ans))
## stop("unable to install packages")
## if (!dir.create(userdir, recursive = TRUE))
## stop(gettextf("unable to create %s", sQuote(userdir)),
## domain = NA)
## .libPaths(c(userdir, .libPaths()))
## }
## }
## else stop("unable to install packages")
## }
## lib <- normalizePath(lib)
## if (length(pkgs) == 1L && missing(repos) && missing(contriburl)) {
## if ((type == "source" && any(grepl("[.]tar[.](gz|bz2|xz)$",
## pkgs))) || (type %in% "win.binary" && endsWith(pkgs,
## ".zip")) || (startsWith(type, "mac.binary") && endsWith(pkgs,
## ".tgz"))) {
## repos <- NULL
## message("inferring 'repos = NULL' from 'pkgs'")
## }
## if (type == "both") {
## if (type2 %in% "win.binary" && endsWith(pkgs, ".zip")) {
## repos <- NULL
## type <- type2
## message("inferring 'repos = NULL' from 'pkgs'")
## }
## else if (startsWith(type2, "mac.binary") && endsWith(pkgs,
## ".tgz")) {
## repos <- NULL
## type <- type2
## message("inferring 'repos = NULL' from 'pkgs'")
## }
## else if (grepl("[.]tar[.](gz|bz2|xz)$", pkgs)) {
## repos <- NULL
## type <- "source"
## message("inferring 'repos = NULL' from 'pkgs'")
## }
## }
## }
## if (length(pkgs) == 1L && is.null(repos) && type == "both") {
## if ((type2 %in% "win.binary" && endsWith(pkgs, ".zip")) ||
## (startsWith(type2, "mac.binary") && endsWith(pkgs,
## ".tgz"))) {
## type <- type2
## }
## else if (grepl("[.]tar[.](gz|bz2|xz)$", pkgs)) {
## type <- "source"
## }
## }
## if (is.null(repos) && missing(contriburl)) {
## tmpd <- destdir
## nonlocalrepos <- any(web <- grepl("^(http|https|ftp)://",
## pkgs))
## if (is.null(destdir) && nonlocalrepos) {
## tmpd <- file.path(tempdir(), "downloaded_packages")
## if (!file.exists(tmpd) && !dir.create(tmpd))
## stop(gettextf("unable to create temporary directory %s",
## sQuote(tmpd)), domain = NA)
## }
## if (nonlocalrepos) {
## df <- function(p, destfile, method, ...) download.file(p,
## destfile, method, mode = "wb", ...)
## urls <- pkgs[web]
## for (p in unique(urls)) {
## this <- pkgs == p
## destfile <- file.path(tmpd, basename(p))
## res <- try(df(p, destfile, method, ...))
## if (!inherits(res, "try-error") && res == 0L)
## pkgs[this] <- destfile
## else {
## pkgs[this] <- NA
## }
## }
## }
## }
## if (type == "both") {
## if (type2 == "source")
## stop("type == \"both\" can only be used on Windows or a CRAN build for macOS")
## if (!missing(contriburl) || !is.null(available))
## type <- type2
## }
## getDeps <- TRUE
## if (type == "both") {
## if (is.null(repos))
## stop("type == \"both\" cannot be used with 'repos = NULL'")
## type <- "source"
## contriburl <- contrib.url(repos, "source")
## if (missing(repos))
## repos <- getOption("repos")
## available <- available.packages(contriburl = contriburl,
## method = method, fields = "NeedsCompilation", ...)
## pkgs <- getDependencies(pkgs, dependencies, available,
## lib, ...)
## getDeps <- FALSE
## av2 <- available.packages(contriburl = contrib.url(repos,
## type2), method = method, ...)
## bins <- row.names(av2)
## bins <- pkgs[pkgs %in% bins]
## srcOnly <- pkgs[!pkgs %in% bins]
## binvers <- av2[bins, "Version"]
## hasArchs <- !is.na(av2[bins, "Archs"])
## needsCmp <- !(available[bins, "NeedsCompilation"] %in%
## "no")
## hasSrc <- hasArchs | needsCmp
## srcvers <- available[bins, "Version"]
## later <- as.numeric_version(binvers) < srcvers
## action <- getOption("install.packages.compile.from.source",
## "interactive")
## if (!nzchar(Sys.which(Sys.getenv("MAKE", "make"))))
## action <- "never"
## if (any(later)) {
## msg <- ngettext(sum(later), "There is a binary version available but the source version is later",
## "There are binary versions available but the source versions are later")
## cat("\n", paste(strwrap(msg, indent = 2, exdent = 2),
## collapse = "\n"), ":\n", sep = "")
## out <- data.frame(binary = binvers, source = srcvers,
## needs_compilation = hasSrc, row.names = bins,
## check.names = FALSE)[later, ]
## print(out)
## cat("\n")
## if (any(later & hasSrc)) {
## if (action == "interactive" && interactive()) {
## msg <- ngettext(sum(later & hasSrc), "Do you want to install from sources the package which needs compilation?",
## "Do you want to install from sources the packages which need compilation?")
## res <- askYesNo(msg)
## if (is.na(res))
## stop("Cancelled by user")
## if (!isTRUE(res))
## later <- later & !hasSrc
## }
## else if (action == "never") {
## cat(" Binaries will be installed\n")
## later <- later & !hasSrc
## }
## }
## }
## bins <- bins[!later]
## if (length(srcOnly)) {
## s2 <- srcOnly[!(available[srcOnly, "NeedsCompilation"] %in%
## "no")]
## if (length(s2)) {
## msg <- ngettext(length(s2), "Package which is only available in source form, and may need compilation of C/C++/Fortran",
## "Packages which are only available in source form, and may need compilation of C/C++/Fortran")
## msg <- c(paste0(msg, ": "), sQuote(s2))
## msg <- strwrap(paste(msg, collapse = " "), exdent = 2)
## message(paste(msg, collapse = "\n"), domain = NA)
## if (action == "interactive" && interactive()) {
## res <- askYesNo("Do you want to attempt to install these from sources?")
## if (is.na(res))
## stop("Cancelled by user")
## if (!isTRUE(res))
## pkgs <- setdiff(pkgs, s2)
## }
## else if (action == "never") {
## cat(" These will not be installed\n")
## pkgs <- setdiff(pkgs, s2)
## }
## }
## }
## if (length(bins)) {
## if (type2 == "win.binary")
## .install.winbinary(pkgs = bins, lib = lib, contriburl = contrib.url(repos,
## type2), method = method, available = av2, destdir = destdir,
## dependencies = NULL, libs_only = libs_only,
## quiet = quiet, ...)
## else .install.macbinary(pkgs = bins, lib = lib, contriburl = contrib.url(repos,
## type2), method = method, available = av2, destdir = destdir,
## dependencies = NULL, quiet = quiet, ...)
## }
## pkgs <- setdiff(pkgs, bins)
## if (!length(pkgs))
## return(invisible())
## message(sprintf(ngettext(length(pkgs), "installing the source package %s",
## "installing the source packages %s"), paste(sQuote(pkgs),
## collapse = ", ")), "\n", domain = NA)
## flush.console()
## }
## else if (getOption("install.packages.check.source", "yes") %in%
## "yes" && (type %in% "win.binary" || startsWith(type,
## "mac.binary"))) {
## if (missing(contriburl) && is.null(available) && !is.null(repos)) {
## contriburl2 <- contrib.url(repos, "source")
## if (missing(repos))
## repos <- getOption("repos")
## av1 <- tryCatch(suppressWarnings(available.packages(contriburl = contriburl2,
## method = method, ...)), error = function(e) e)
## if (inherits(av1, "error")) {
## message("source repository is unavailable to check versions")
## available <- available.packages(contriburl = contrib.url(repos,
## type), method = method, ...)
## }
## else {
## srcpkgs <- pkgs[pkgs %in% row.names(av1)]
## available <- available.packages(contriburl = contrib.url(repos,
## type), method = method, ...)
## bins <- pkgs[pkgs %in% row.names(available)]
## na <- srcpkgs[!srcpkgs %in% bins]
## if (length(na)) {
## msg <- sprintf(ngettext(length(na), "package %s is available as a source package but not as a binary",
## "packages %s are available as source packages but not as binaries"),
## paste(sQuote(na), collapse = ", "))
## cat("\n ", msg, "\n\n", sep = "")
## }
## binvers <- available[bins, "Version"]
## srcvers <- binvers
## OK <- bins %in% srcpkgs
## srcvers[OK] <- av1[bins[OK], "Version"]
## later <- as.numeric_version(binvers) < srcvers
## if (any(later)) {
## msg <- ngettext(sum(later), "There is a binary version available (and will be installed) but the source version is later",
## "There are binary versions available (and will be installed) but the source versions are later")
## cat("\n", paste(strwrap(msg, indent = 2, exdent = 2),
## collapse = "\n"), ":\n", sep = "")
## print(data.frame(binary = binvers, source = srcvers,
## row.names = bins, check.names = FALSE)[later,
## ])
## cat("\n")
## }
## }
## }
## }
## if (.Platform$OS.type == "windows") {
## if (startsWith(type, "mac.binary"))
## stop("cannot install macOS binary packages on Windows")
## if (type %in% "win.binary") {
## .install.winbinary(pkgs = pkgs, lib = lib, contriburl = contriburl,
## method = method, available = available, destdir = destdir,
## dependencies = dependencies, libs_only = libs_only,
## quiet = quiet, ...)
## return(invisible())
## }
## have_spaces <- grep(" ", pkgs)
## if (length(have_spaces)) {
## p <- pkgs[have_spaces]
## dirs <- shortPathName(dirname(p))
## pkgs[have_spaces] <- file.path(dirs, basename(p))
## }
## pkgs <- gsub("\\", "/", pkgs, fixed = TRUE)
## }
## else {
## if (startsWith(type, "mac.binary")) {
## if (!grepl("darwin", R.version$platform))
## stop("cannot install macOS binary packages on this platform")
## .install.macbinary(pkgs = pkgs, lib = lib, contriburl = contriburl,
## method = method, available = available, destdir = destdir,
## dependencies = dependencies, quiet = quiet, ...)
## return(invisible())
## }
## if (type %in% "win.binary")
## stop("cannot install Windows binary packages on this platform")
## if (!file.exists(file.path(R.home("bin"), "INSTALL")))
## stop("This version of R is not set up to install source packages\nIf it was installed from an RPM, you may need the R-devel RPM")
## }
## cmd0 <- file.path(R.home("bin"), "R")
## args0 <- c("CMD", "INSTALL")
## output <- if (quiet)
## FALSE
## else ""
## env <- character()
## tlim <- Sys.getenv("_R_INSTALL_PACKAGES_ELAPSED_TIMEOUT_")
## tlim <- if (is.na(tlim))
## 0
## else tools:::get_timeout(tlim)
## outdir <- getwd()
## if (is.logical(keep_outputs)) {
## if (is.na(keep_outputs))
## keep_outputs <- FALSE
## }
## else if (is.character(keep_outputs) && (length(keep_outputs) ==
## 1L)) {
## if (!dir.exists(keep_outputs) && !dir.create(keep_outputs,
## recursive = TRUE))
## stop(gettextf("unable to create %s", sQuote(keep_outputs)),
## domain = NA)
## outdir <- normalizePath(keep_outputs)
## keep_outputs <- TRUE
## }
## else stop(gettextf("invalid %s argument", sQuote("keep_outputs")),
## domain = NA)
## if (length(libpath <- .R_LIBS())) {
## if (.Platform$OS.type == "windows") {
## oldrlibs <- Sys.getenv("R_LIBS")
## Sys.setenv(R_LIBS = libpath)
## on.exit(Sys.setenv(R_LIBS = oldrlibs))
## }
## else env <- paste0("R_LIBS=", shQuote(libpath))
## }
## if (is.character(clean))
## args0 <- c(args0, clean)
## if (libs_only)
## args0 <- c(args0, "--libs-only")
## if (!missing(INSTALL_opts)) {
## if (!is.list(INSTALL_opts)) {
## args0 <- c(args0, paste(INSTALL_opts, collapse = " "))
## INSTALL_opts <- list()
## }
## }
## else {
## INSTALL_opts <- list()
## }
## if (verbose)
## message(gettextf("system (cmd0): %s", paste(c(cmd0, args0),
## collapse = " ")), domain = NA)
## if (is.null(repos) && missing(contriburl)) {
## update <- cbind(path.expand(pkgs), lib)
## for (i in seq_len(nrow(update))) {
## if (is.na(update[i, 1L]))
## next
## args <- c(args0, get_install_opts(update[i, 1L]),
## "-l", shQuote(update[i, 2L]), getConfigureArgs(update[i,
## 1L]), getConfigureVars(update[i, 1L]), shQuote(update[i,
## 1L]))
## status <- system2(cmd0, args, env = env, stdout = output,
## stderr = output, timeout = tlim)
## if (status > 0L)
## warning(gettextf("installation of package %s had non-zero exit status",
## sQuote(update[i, 1L])), domain = NA)
## else if (verbose) {
## cmd <- paste(c(cmd0, args), collapse = " ")
## message(sprintf("%d): succeeded '%s'", i, cmd),
## domain = NA)
## }
## }
## return(invisible())
## }
## tmpd <- destdir
## nonlocalrepos <- !all(startsWith(contriburl, "file:"))
## if (is.null(destdir) && nonlocalrepos) {
## tmpd <- file.path(tempdir(), "downloaded_packages")
## if (!file.exists(tmpd) && !dir.create(tmpd))
## stop(gettextf("unable to create temporary directory %s",
## sQuote(tmpd)), domain = NA)
## }
## av2 <- NULL
## if (is.null(available)) {
## filters <- getOption("available_packages_filters")
## if (!is.null(filters)) {
## available <- available.packages(contriburl = contriburl,
## method = method, ...)
## }
## else {
## f <- setdiff(available_packages_filters_default,
## c("R_version", "duplicates"))
## av2 <- available.packages(contriburl = contriburl,
## filters = f, method = method, ...)
## f <- available_packages_filters_db[["R_version"]]
## f2 <- available_packages_filters_db[["duplicates"]]
## available <- f2(f(av2))
## }
## }
## if (getDeps)
## pkgs <- getDependencies(pkgs, dependencies, available,
## lib, ..., av2 = av2)
## foundpkgs <- download.packages(pkgs, destdir = tmpd, available = available,
## contriburl = contriburl, method = method, type = "source",
## quiet = quiet, ...)
## if (length(foundpkgs)) {
## if (verbose)
## message(gettextf("foundpkgs: %s", paste(foundpkgs,
## collapse = ", ")), domain = NA)
## update <- unique(cbind(pkgs, lib))
## colnames(update) <- c("Package", "LibPath")
## found <- pkgs %in% foundpkgs[, 1L]
## files <- foundpkgs[match(pkgs[found], foundpkgs[, 1L]),
## 2L]
## if (verbose)
## message(gettextf("files: %s", paste(files, collapse = ", \n\t")),
## domain = NA)
## update <- cbind(update[found, , drop = FALSE], file = files)
## if (nrow(update) > 1L) {
## upkgs <- unique(pkgs <- update[, 1L])
## DL <- .make_dependency_list(upkgs, available)
## p0 <- .find_install_order(upkgs, DL)
## update <- update[sort.list(match(pkgs, p0)), ]
## }
## if (Ncpus > 1L && nrow(update) > 1L) {
## tlim_cmd <- character()
## if (tlim > 0) {
## if (nzchar(timeout <- Sys.which("timeout"))) {
## tlim_cmd <- c(shQuote(timeout), "-s INT", tlim)
## }
## else warning("timeouts for parallel installs require the 'timeout' command")
## }
## args0 <- c(args0, "--pkglock")
## tmpd2 <- file.path(tempdir(), "make_packages")
## if (!file.exists(tmpd2) && !dir.create(tmpd2))
## stop(gettextf("unable to create temporary directory %s",
## sQuote(tmpd2)), domain = NA)
## mfile <- file.path(tmpd2, "Makefile")
## conn <- file(mfile, "wt")
## deps <- paste(paste0(update[, 1L], ".ts"), collapse = " ")
## deps <- strwrap(deps, width = 75, exdent = 2)
## deps <- paste(deps, collapse = " \\\n")
## cat("all: ", deps, "\n", sep = "", file = conn)
## aDL <- .make_dependency_list(upkgs, available, recursive = TRUE)
## for (i in seq_len(nrow(update))) {
## pkg <- update[i, 1L]
## fil <- update[i, 3L]
## args <- c(args0, get_install_opts(fil), "-l",
## shQuote(update[i, 2L]), getConfigureArgs(fil),
## getConfigureVars(fil), shQuote(fil), ">", paste0(pkg,
## ".out"), "2>&1")
## cmd <- paste(c("MAKEFLAGS=", tlim_cmd, shQuote(cmd0),
## args), collapse = " ")
## deps <- aDL[[pkg]]
## deps <- deps[deps %in% upkgs]
## deps <- if (length(deps))
## paste(paste0(deps, ".ts"), collapse = " ")
## else ""
## cat(paste0(pkg, ".ts: ", deps), paste("\t@echo begin installing package",
## sQuote(pkg)), paste0("\t@", cmd, " && touch ",
## pkg, ".ts"), paste0("\t@cat ", pkg, ".out"),
## "", sep = "\n", file = conn)
## }
## close(conn)
## cwd <- setwd(tmpd2)
## on.exit(setwd(cwd))
## status <- system2(Sys.getenv("MAKE", "make"), c("-k -j",
## Ncpus), stdout = output, stderr = output, env = env)
## if (status > 0L) {
## pkgs <- update[, 1L]
## tss <- sub("[.]ts$", "", dir(".", pattern = "[.]ts$"))
## failed <- pkgs[!pkgs %in% tss]
## for (pkg in failed) system(paste0("cat ", pkg,
## ".out"))
## warning(gettextf("installation of one or more packages failed,\n probably %s",
## paste(sQuote(failed), collapse = ", ")), domain = NA)
## }
## if (keep_outputs)
## file.copy(paste0(update[, 1L], ".out"), outdir)
## file.copy(Sys.glob(paste0(update[, 1L], "*.zip")),
## cwd)
## file.copy(Sys.glob(paste0(update[, 1L], "*.tgz")),
## cwd)
## file.copy(Sys.glob(paste0(update[, 1L], "*.tar.gz")),
## cwd)
## setwd(cwd)
## on.exit()
## unlink(tmpd2, recursive = TRUE)
## }
## else {
## tmpd2 <- tempfile()
## if (!dir.create(tmpd2))
## stop(gettextf("unable to create temporary directory %s",
## sQuote(tmpd2)), domain = NA)
## outfiles <- file.path(tmpd2, paste0(update[, 1L],
## ".out"))
## for (i in seq_len(nrow(update))) {
## outfile <- if (keep_outputs)
## outfiles[i]
## else output
## fil <- update[i, 3L]
## args <- c(args0, get_install_opts(fil), "-l",
## shQuote(update[i, 2L]), getConfigureArgs(fil),
## getConfigureVars(fil), shQuote(fil))
## status <- system2(cmd0, args, env = env, stdout = outfile,
## stderr = outfile, timeout = tlim)
## if (!quiet && keep_outputs)
## writeLines(readLines(outfile))
## if (status > 0L)
## warning(gettextf("installation of package %s had non-zero exit status",
## sQuote(update[i, 1L])), domain = NA)
## else if (verbose) {
## cmd <- paste(c(cmd0, args), collapse = " ")
## message(sprintf("%d): succeeded '%s'", i, cmd),
## domain = NA)
## }
## }
## if (keep_outputs)
## file.copy(outfiles, outdir)
## unlink(tmpd2, recursive = TRUE)
## }
## if (!quiet && nonlocalrepos && !is.null(tmpd) && is.null(destdir))
## cat("\n", gettextf("The downloaded source packages are in\n\t%s",
## sQuote(normalizePath(tmpd, mustWork = FALSE))),
## "\n", sep = "", file = stderr())
## libs_used <- unique(update[, 2L])
## if (.Platform$OS.type == "unix" && .Library %in% libs_used) {
## message("Updating HTML index of packages in '.Library'")
## make.packages.html(.Library)
## }
## }
## else if (!is.null(tmpd) && is.null(destdir))
## unlink(tmpd, TRUE)
## invisible()
## }
## <bytecode: 0x7fd4680b03d0>
## <environment: namespace:utils>
#When you need to use a package
You need to reference the package in your code if you are using something like ggplot2 or Psych. RStudio will automatically reference knitr and RMarkdown.
library(ggplot2)
Go into your files in the bottom right quadrant. Select the file path where you will be pulling a majority of your data from. Click the “More” button at the top and then “Set as Working Directory”. Copy the information from your Console (bottom left quadrant) into the chunk below. Click Run.
setwd("~/Downloads")
Save your data file from eLC as a .csv file. Make sure to save it in the location where you will be pulling a majority of your data from. Then, type in the title of whatever you saved into the chunk below. Click Run.
data<-read.csv('PSYC3990Lab1.2.csv')
#Knitting your RMarkdown
You will need to install another package in order to knit your files to PDF. There are two different packages you can use:
-Latex -TinyTex
We will be using TinyTex.
#Learning to run descriptives in R
-Race, Gender, Age, Participant Self-Control
Race: Nominal Gender: Nominal Age: Interval Participant Self-Control: Make item in excel
#Let’s just take a look at your data
This will show you the number of observations (aka participants) you have in your data set, the number of variables you have, and the type of variables you have. “Int” stands for integer and “Chr” stands for character.
str(data)
## 'data.frame': 400 obs. of 56 variables:
## $ Durationinseconds: int 294 139 123 220 256 127 207 234 406 281 ...
## $ ID : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Q23 : chr "Mad for Ads" "Mad for Ads" "Mad for Ads" "Mad for Ads" ...
## $ Sccond : chr "High" "High" "High" "High" ...
## $ samSC : int 5 5 6 6 6 6 6 6 6 6 ...
## $ samFriends : int 4 4 7 4 4 5 6 4 6 7 ...
## $ extraversion : int 6 6 7 2 5 7 6 5 3 6 ...
## $ samSimilar : int 3 5 5 6 5 6 5 5 5 6 ...
## $ gengrat : int 5 3 5 6 7 7 6 7 7 7 ...
## $ agreeable : int 4 5 6 5 6 7 5 6 6 6 ...
## $ Samtime : int 5 4 5 6 6 6 6 6 5 7 ...
## $ openness : int 6 4 6 4 7 5 6 5 2 5 ...
## $ neurotic : int 3 2 2 4 1 6 2 2 3 2 ...
## $ samlikework : int 6 4 6 5 7 7 7 6 7 5 ...
## $ genvalue : int 7 4 6 6 7 6 6 7 7 7 ...
## $ Favor : chr "CWB" "CWB" "CWB" "CWB" ...
## $ expect : int 1 1 1 1 1 1 1 1 1 1 ...
## $ stategrat : int 5 5 6 6 7 6 6 7 6 6 ...
## $ reward : int 3 6 5 2 5 2 4 4 3 5 ...
## $ promote : int 3 6 5 6 7 5 6 6 5 7 ...
## $ statevalue : int 5 5 6 6 7 6 6 7 7 7 ...
## $ feelgrat : int 6 6 6 5 6 6 6 7 5 6 ...
## $ attencheck : int 5 5 5 5 5 5 5 5 5 5 ...
## $ say : chr "What is up Sam?" "How are you doing" "Hi Sam, what are you working on?" "Thanks for consistently offering up good work. You will go far with that work ethic." ...
## $ partsc_1 : int 2 3 4 2 2 2 3 2 2 3 ...
## $ partsc_2 : int 1 1 5 1 1 1 2 1 3 2 ...
## $ partsc_3 : int 2 3 4 1 1 2 3 1 2 3 ...
## $ partsc_4 : int 3 2 3 3 4 1 1 4 4 1 ...
## $ partsc_5 : int 4 2 4 3 4 4 3 4 5 2 ...
## $ partsc_6 : int 3 2 2 5 4 3 4 3 4 2 ...
## $ partsc_7 : int 1 4 4 1 1 3 3 1 2 3 ...
## $ partsc_8 : int 2 3 4 1 1 3 2 1 2 4 ...
## $ partsc_9 : int 1 2 4 2 1 3 2 1 1 4 ...
## $ partsc_10 : int 2 3 3 1 1 4 2 1 1 4 ...
## $ age : int 44 32 44 27 33 30 33 41 42 37 ...
## $ gender : chr "F" "M" "M" "F" ...
## $ race : chr "White" "White" "White" "White" ...
## $ engfirst : int 1 1 1 1 1 1 1 1 1 1 ...
## $ fluent : chr "Yes" "Yes" "Yes" "Yes" ...
## $ employeed : chr "Yes" "Yes" "Yes" "No" ...
## $ currentjob : chr "dispatcher" "Principal" "technician" "" ...
## $ recentjob : chr "" "" "" "Teller" ...
## $ pincome : chr "65000" "55000" "35000" "0" ...
## $ hincome : chr "200000" "80000" "35000" "33000" ...
## $ recode_1 : chr "4" "3" "2" "4" ...
## $ recode_2 : chr "5" "5" "1" "5" ...
## $ recode_3 : chr "4" "3" "2" "5" ...
## $ partsc_4.1 : int 3 2 3 3 4 1 1 4 4 1 ...
## $ partsc_5.1 : int 4 2 4 3 4 4 3 4 5 2 ...
## $ partsc_6.1 : int 3 2 2 5 4 3 4 3 4 2 ...
## $ recode_7 : chr "5" "2" "2" "5" ...
## $ recode_8 : chr "4" "3" "2" "5" ...
## $ recode_9 : chr "5" "4" "2" "4" ...
## $ recode_10 : chr "4" "3" "3" "5" ...
## $ self_control : chr "4.1" "2.9" "2.3" "4.4" ...
## $ X : chr "*Here is an example of the code you will use to calculate the average self-control across each participant." "" "Once you calculate for all 400 observations, copy column BC and paste it into your csv file. Reload the csv fil"| __truncated__ "" ...
#This will give you the means and SD’s of all of your variables.
library(psych)
##
## Attaching package: 'psych'
## The following objects are masked from 'package:ggplot2':
##
## %+%, alpha
describe(data)
#To determine count, download the following package and use it: plyr
#Determine the frequencies/count of nominal variables.
```r
(data$race)
## [1] "White" "White" "White"
## [4] "White" "White" "Asian"
## [7] "White" "White" "White"
## [10] "White" "Other" "White"
## [13] "White" "Hispanic" "White"
## [16] "White" "White" "White"
## [19] "White" "White" "White"
## [22] "White" "White" "White"
## [25] "White" "White" "African American"
## [28] "White" "Asian" "White"
## [31] "White" "White" "White"
## [34] "White" "White" "White"
## [37] "White" "White" "White"
## [40] "African American" "White" "White"
## [43] "White" "African American" "White"
## [46] "White" "White" "Other"
## [49] "White" "White" "White"
## [52] "African American" "White" "White"
## [55] "White" "White" "Hispanic"
## [58] "Other" "White" "White"
## [61] "White" "White" "White"
## [64] "White" "White" "Other"
## [67] "White" "White" "White"
## [70] "White" "White" "White"
## [73] "White" "White" "African American"
## [76] "Asian" "White" "White"
## [79] "Hispanic" "Asian" "White"
## [82] "White" "White" "White"
## [85] "White" "White" "Other"
## [88] "African American" "White" "White"
## [91] "African American" "White" "White"
## [94] "African American" "White" "White"
## [97] "White" "White" "White"
## [100] "White" "White" "African American"
## [103] "White" "White" "Other"
## [106] "White" "White" "White"
## [109] "African American" "White" "White"
## [112] "White" "White" "White"
## [115] "White" "White" "White"
## [118] "White" "White" "hispanic"
## [121] "African American" "White" "Asian"
## [124] "White" "White" "White"
## [127] "White" "White" "White"
## [130] "Asian" "White" "White"
## [133] "White" "Other" "White"
## [136] "White" "African American" "White"
## [139] "White" "Other" "White"
## [142] "White" "White" "White"
## [145] "White" "White" "White"
## [148] "White" "White" "White"
## [151] "White" "White" "White"
## [154] "Hispanic" "White" "White"
## [157] "White" "Other" "African American"
## [160] "White" "White" "White"
## [163] "African American" "White" "Hispanic"
## [166] "White" "White" "White"
## [169] "White" "White" "White"
## [172] "White" "White" "White"
## [175] "White" "White" "African American"
## [178] "White" "Asian" "Other"
## [181] "Hispanic" "White" "White"
## [184] "White" "White" "White"
## [187] "White" "White" "White"
## [190] "White" "White" "White"
## [193] "White" "Asian" "White"
## [196] "White" "White" "African American"
## [199] "White" "White" "Other"
## [202] "Asian" "White" "White"
## [205] "White" "White" "White"
## [208] "White" "White" "White"
## [211] "White" "White" "White"
## [214] "Asian" "White" "White"
## [217] "White" "Other" "Asian"
## [220] "White" "White" "Hispanic"
## [223] "20" "White" "White"
## [226] "Asian" "White" "African American"
## [229] "White" "White" "White"
## [232] "White" "White" "Asian"
## [235] "White" "Asian" "White"
## [238] "Other" "White" "Other"
## [241] "Female" "White" "Asian"
## [244] "Hispanic" "White" "Other"
## [247] "White" "White" "White"
## [250] "White" "White" "White"
## [253] "White" "White" "White"
## [256] "White" "White" "White"
## [259] "White" "Asian" "White"
## [262] "White" "Asian" "White"
## [265] "White" "White" "White"
## [268] "White" "White" "White"
## [271] "White" "White" "White"
## [274] "White" "Asian" "White"
## [277] "White" "White" "Asian"
## [280] "asian" "African American" "White"
## [283] "White" "White" "White"
## [286] "White" "Asian" "White"
## [289] "White" "African American" "White"
## [292] "Other" "White" "White"
## [295] "White" "White" "White"
## [298] "White" "White" "White"
## [301] "White" "Asian" "White"
## [304] "White" "White" "White"
## [307] "Hispanic" "White" "White"
## [310] "Other" "White" "African American"
## [313] "White" "White" "White"
## [316] "White" "White" "White"
## [319] "White" "White" "White"
## [322] "White" "White" "Other"
## [325] "Other" "White" "White"
## [328] "Other" "White" "White"
## [331] "White" "Asian" "African American"
## [334] "White" "White" "African American"
## [337] "White" "White" "White"
## [340] "White" "White" "Asian"
## [343] "White" "White" "White"
## [346] "White" "Asian" "African American"
## [349] "White" "African American" "Hispanic"
## [352] "White" "White" "African American"
## [355] "Asian" "White" "White"
## [358] "White" "Asian" "White"
## [361] "Other" "White" "White"
## [364] "White" "White" "White"
## [367] "White" "White" "White"
## [370] "White" "White" "African American"
## [373] "White" "White" "White"
## [376] "White" "African American" "White"
## [379] "White" "White" "White"
## [382] "White" "White" "African American"
## [385] "White" "White" "Asian"
## [388] "White" "White" "White"
## [391] "White" "Asian" "White"
## [394] "White" "African American" "Other"
## [397] "African American" "White" "White"
## [400] ""
(data$gender)
## [1] "F" "M" "M" "F" "M" "M"
## [7] "M" "M" "F" "F" "M" "F"
## [13] "F" "M" "M" "M" "M" "M"
## [19] "F" "M" "F" "F" "F" "M"
## [25] "F" "M" "F" "M" "M" "F"
## [31] "F" "F" "M" "M" "M" "M"
## [37] "F" "F" "F" "M" "M" "M"
## [43] "F" "F" "F" "F" "F" "M"
## [49] "M" "F" "M" "F" "M" "F"
## [55] "F" "M" "M" "M" "F" "F"
## [61] "M" "F" "F" "M" "M" "F"
## [67] "M" "M" "F" "F" "M" "M"
## [73] "F" "F" "M" "F" "M" "F"
## [79] "M" "F" "F" "M" "M" "F"
## [85] "F" "M" "F" "M" "M" "M"
## [91] "M" "M" "F" "F" "M" "F"
## [97] "F" "F" "F" "M" "F" "M"
## [103] "M" "M" "F" "M" "M" "F"
## [109] "M" "M" "M" "F" "M" "F"
## [115] "M" "M" "M" "M" "F" "F"
## [121] "M" "F" "M" "M" "F" "F"
## [127] "F" "M" "M" "M" "M" "F"
## [133] "M" "M" "F" "F" "F" "F"
## [139] "F" "M" "M" "F" "F" "F"
## [145] "F" "F" "F" "F" "F" "M"
## [151] "M" "M" "M" "F" "F" "M"
## [157] "F" "M" "M" "F" "F" "M"
## [163] "F" "M" "M" "F" "M" "F"
## [169] "F" "M" "M" "F" "F" "M"
## [175] "F" "F" "M" "F" "M" "F"
## [181] "F" "F" "M" "F" "F" "F"
## [187] "F" "M" "M" "F" "F" "F"
## [193] "M" "M" "M" "M" "M" "F"
## [199] "M" "F" "F" "F" "F" "F"
## [205] "M" "M" "F" "F" "F" "M"
## [211] "M" "M" "F" "F" "F" "F"
## [217] "F" "M" "M" "M" "F" "M"
## [223] "M" "M" "F" "M" "M" "M"
## [229] "F" "Nonbinary" "M" "F" "M" "F"
## [235] "M" "F" "F" "M" "M" "F"
## [241] "Nonbinary" "M" "M" "F" "M" "Nonbinary"
## [247] "F" "F" "M" "F" "F" "F"
## [253] "M" "F" "M" "F" "M" "M"
## [259] "M" "F" "M" "M" "M" "F"
## [265] "F" "F" "F" "F" "M" "F"
## [271] "F" "M" "M" "F" "M" "F"
## [277] "M" "F" "M" "F" "M" "M"
## [283] "M" "F" "F" "F" "M" "M"
## [289] "F" "M" "M" "M" "F" "F"
## [295] "M" "F" "F" "F" "F" "M"
## [301] "M" "M" "F" "F" "F" "F"
## [307] "M" "M" "M" "F" "M" "M"
## [313] "M" "M" "F" "F" "M" "F"
## [319] "M" "M" "F" "F" "F" "F"
## [325] "M" "M" "F" "F" "M" "F"
## [331] "F" "F" "M" "F" "F" "F"
## [337] "M" "F" "M" "F" "F" "F"
## [343] "F" "F" "F" "M" "F" "M"
## [349] "F" "M" "F" "M" "M" "F"
## [355] "F" "F" "F" "M" "M" "F"
## [361] "M" "F" "M" "F" "F" "F"
## [367] "F" "F" "M" "F" "F" "F"
## [373] "M" "F" "F" "F" "M" "F"
## [379] "M" "F" "F" "F" "F" "F"
## [385] "M" "M" "F" "F" "F" "F"
## [391] "F" "F" "F" "M" "F" "M"
## [397] "M" "F" "M" ""
Keep in mind that if you need more information from your nominal variables aka you are trying to determine the percentage of the sample that was “Female”, you can just do some simple math in R.
Percentnonbinary<-3/400*100
Percentnonbinary
## [1] 0.75
#Determine the Means and SD of interval variables. na.rm means you are removing any of the missing values from the table.
mean(data$self_control,na.rm = TRUE)
## Warning in mean.default(data$self_control, na.rm = TRUE): argument is not
## numeric or logical: returning NA
## [1] NA
sd(data$self_control,na.rm = TRUE)
## Warning in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm =
## na.rm): NAs introduced by coercion
## [1] 0.7744623
#How will you create the Participant Self-Control variable?
You need to create a variable that contains a participants average level of self-control based on multiple items. I prefer to do this in Excel and then add it to my .csv file.
Which items do we need to reverse code if we are looking at a measure of self control? 1,2,3,7,8,9,10
Reverse code items in Excel. Use the formulas I have included in Excel as an example. Find the average for self-control for each ID. Copy the self_control variable into your csv or Excel file. Import file into RStudio. Run your descriptive statistics.
#Example of APA Table
Here is a helpful link for learning how to create APA Tables. There are packages that allow you to do this in R but for now, I would recommend you all use Word to create your table.
https://apastyle.apa.org/style-grammar-guidelines/tables-figures/tables
#Writing up your Sample Characteristics in APA
Here is an example of writing about your sample characteristics in APA. Make sure you include all of the variables listed in your assignment (e.g., race, gender, age, participant self-control)
Participants included 96 mothers (Mage = 38.67, SD = 6.53) and their 7-12-year-old children (M = 9.27, SD = 1.38). For inclusion within the study, participants were required to be English-speaking and serve as maternal figures to the children (e.g., step-mother, adoptive mother, custodial grandmother). Of the 96 children, 44 identified as female and 53 identified as male. A majority of mothers identified as White (91%), completed a 4-year degree or higher (74%), and reported an income of $50,000/year or higher (73%). Four mother-child dyads were excluded from the original sample of 100 dyads due to child behavioral problems or difficulty comprehending English. Participants were recruited in a small town and the surrounding rural area within an Appalachian community in the southeastern United States. The population in this region is primarily White (93.5%; U.S. Census Bureau, 2019).