Part 1: Getting started

Intro to Computational Studies in Education and the Social Sciences

Author
Affiliations

Nathan Alexander, PhD

School of Education

Center for Applied Data Science and Analytics

Using Posit Cloud

Posit Cloud is a web-based platform that allows users to perform data science tasks directly in their browser. It provides a cloud-based environment similar to the traditional RStudio integrated development environment (IDE), eliminating the need for local software installation and maintenance. Users can create projects, share work with collaborators, and access features like interactive notebooks. Posit Cloud offers various plans, including a free option for casual use and premium plans for professionals, instructors, and organizations.

You can access the Posit Cloud here.

Directories

Directories help us organizer our R projects efficiently.

A common structure includes a root directory that helps set the working directory automatically. Within this root, subdirectories such as “Data” for storing datasets, “src” or “R” for R scripts and functions, “output” for results, and “plots” for visualizations are typically used. This organization enhances project management, improves collaboration, and simplifies file referencing. Using relative file paths allows for more portable code, making it easier to share projects across different computers or with collaborators.

See your current working directory

To see your current working directory, we’ll run the getwd() command.

getwd()

List files in your working directory

To view files in your working directory, we’ll run the list.files() command.

list.files()

Create a new directory

To create a new directory, we’ll run the dir.create() command.

dir.create("name_of_new_folder")

Set your working directory

To set your working directory, we’ll run the setwd() command.

setwd()

Verify the change

When you set a new working directory, you want to verify the change by using getwd().

getwd()

Packages

Packages in R are collections of functions, data, and documentation that extend the language’s capabilities. They allow users to easily share and reuse code, making R a powerful and flexible tool for data analysis and visualization.

Packages can be installed from repositories like CRAN, and once installed, they can be loaded into an R session to access their functionality. The tidyverse, for example, is a popular collection of packages that provides a consistent and user-friendly approach to data manipulation and visualization.

Install the Praise package.

# install the package
install.packages("praise", repos = "http://cran.us.r-project.org")

The downloaded binary packages are in
    /var/folders/t2/v2ypghs120z45678ty2nqfk00000gn/T//Rtmp2fxt6e/downloaded_packages

Load the Praise package

# load library
library(praise)

Now get some Praise!

# get some praise
praise()
[1] "You are bedazzling!"

Arithmetic in R

We will learn how to calculate values in R.


1 + 2  # the 'plus sign' computes the sum
[1] 3

2 - 3  # the 'minus sign' computes the difference
[1] -1

3 * 4  # the 'asterisk' computes the product
[1] 12

4 / 5 # the 'forward slash' computes the quotient
[1] 0.8

# we can also compute the sum of the first 100 positive integers
sum(1:100) 
[1] 5050

Explore different object types

For this task, we will explore three object types: numeric, character, and logic values.

Task 2-a: Compute a mathematical statement and create a numeric variable

1 + 2
[1] 3

We can assign a variable to this statement by using an assignment operator: <-

a <- 1 + 2

We can also use an equal sign to assign values: \(=\)

a = 1 + 2

Type “a” to show the value of the variable

a
[1] 3

Create a numeric variable “b” that is the product of “a” and “5”

b = a*5

Type “b” in your console to show the product of the two variables

b
[1] 15

Divide b by 4

b / 4
[1] 3.75

Take the square root of b

sqrt(b)
[1] 3.872983

Compute the natural log of b

log(b)
[1] 2.70805

Compute the common log of b

log10(b)
[1] 1.176091

Find 1 minus the square root of b

1-sqrt(b)
[1] -2.872983

Attempt to find the square root of “1 minus the square root of b” - which is a negative value

sqrt(1-b)
Warning in sqrt(1 - b): NaNs produced
[1] NaN

NaN stands for “Not a number”. This occurs because there is currently no defined value to recognize the square root of negative numbers in R. But we can compute the square root on the absolute value of this difference, if needed.

sqrt(abs(1-b))
[1] 3.741657

We can insert longer or more complex mathematical statements too. For example, we can find the absolute value of the sum of -1 and the square root of b cubed and then subtract from that the value of 3 times the square root of b.

Notice the use of parentheses.

abs(-1+sqrt(b^3)) - 3*(sqrt(b))
[1] 45.4758

We can override the original value of y to match the mathematical statement we generated above.

y <- abs(-1+sqrt(b^3)) - 3*(sqrt(b))
y
[1] 45.4758

We consider all of the previous objects to be numeric.


We can also create objects to hold non-numeric values.

There are two types of non-numeric values: character values and logic values.

Character values
character <- 'some label'
character
[1] "some label"

We can create a character value using the ‘,’ or “,” quotes.

character <- "some label"
character
[1] "some label"

Logic values

Logic values can either be TRUE or FALSE

logic_true <- TRUE
logic_false <- FALSE
logic_true
[1] TRUE
logic_false
[1] FALSE

We can also use T for TRUE and F for FALSE.

logic_true <- T
logic_false <- F
logic_true
[1] TRUE
logic_false
[1] FALSE

Variables in R

We will learn to give a variable (or character) a value.


Use the different assignment operators

y = 2 # the equal sign can be used as an assignment operator

y <-2 # a "less than" sign and dash can also be used as an assignment operator

y # R stores all values you assign, so you must "call" any variables to see their values
[1] 2

Set x equal to two added to three

x = 2 + 3
x
[1] 5

Set y equal to two minus three

y = 2 - 3
y
[1] -1

Set z equal to two times three

z = 2 * 3
z
[1] 6

Overwrite the value of y by setting y equal to x divided by z

y = x / z
y
[1] 0.8333333