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.
If you’d like, you can install base R and RStudio which offers several advantages over Posit Cloud, especially for those who need offline access or prefer to work with local files and resources directly on their computer. This can be especially beneficial for managing large datasets or complex project structures, as well as for users who frequently work in areas with limited internet connectivity.
Follow the steps below in order.
– Visit the Comprehensive R Archive Network (CRAN) website.
– Click “Download R” and follow the installation prompts.
For Windows: Choose “Install R for the first time” and run the downloaded .exe file.
For macOS: Download the .pkg file and follow the instructions.
For Linux: Follow system-specific installation steps or build from source if necessary.
– Leave default options selected during installation unless customization is needed.
– Go to the official RStudio website.
– Select the download button that corresponds to your operating system.
– Run the downloaded installer:
For Windows: Execute the .exe file and follow the setup wizard.
For macOS: Open the .dmg file, drag the RStudio icon to Applications, and eject the disk image.
For Linux: Install using system tools based on your Linux distribution.
– Complete installation by leaving default options selected unless customization is required.
– Open RStudio after installation.
– Ensure it detects your installed version of R automatically during setup.
– Run a the command print("Hello, World!")
to confirm
functionality.
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.
To see your current working directory, we’ll run the
getwd()
command.
getwd()
To view files in your working directory, we’ll run the
list.files()
command.
list.files()
To create a new directory, we’ll run the dir.create()
command.
dir.create("name_of_new_folder")
To set your working directory, we’ll run the setwd()
command.
setwd()
When you set a new working directory, you want to verify the change
by using getwd()
.
getwd()
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 package
install.packages("praise", repos = "http://cran.us.r-project.org")
##
## The downloaded binary packages are in
## /var/folders/54/02xngdrx2277pg5hxjx3z8040000gn/T//Rtmp6YgkM2/downloaded_packages
# load library
library(praise)
# get some praise
praise()
## [1] "You are cat's meow!"
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.
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 <- '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 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
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