Section 1.3 What Is R and RStudio?
R is a computer software that can be used for crime data analysis. R is popular among people who handle and analyze data for several reasons. First, it is free. Anybody can use this software without having to pay a subscription fee. Second, R allows people to conduct various statistical analyses and compute graphical images, from very simple to advanced statistics. A problem with R is that it is not easy to use, especially for those who have not used it before. Writing codes can be overwhelming, but Rβs user interface does not help make it easy. RStudio is a software that makes R more user-friendly by providing various functions and support.
Subsection 1.3.1 Setting Up R and RStudio
Now that we have discussed why data analysis is important in the criminal justice field, let us analyze the data to see the value of analysis. You will need to install R and RStudio, and you can use R Is for Rams to follow the instructions and complete the installation process successfully.
Subsection 1.3.2 Calculating Using R
While R is incredibly flexible and supports numerous statistical packages, at its core, it functions as a big, powerful calculator. The basic use of R helps you analyze criminal justice data. If you follow the instructions below, you will not have major trouble producing the same outcome. Since R is a big calculator, we will do some calculations.
Subsubsection 1.3.2.1 Step 1. Launch RStudio
Open the RStudio application on your computer.
Subsubsection 1.3.2.2 Step 2. Choose Script or Console
You can work with R in RStudio using either the script editor or the console. The console lets you execute commands interactively, while the script editor lets you write and save scripts for more complex tasks. Today, we will use the console.
Subsubsection 1.3.2.3 Step 3. Type R Code
In the R console of RStudio, you can directly type R code.
#calculate multiplications
5*4*3*2*1
## [1] 120
#you can also do the same thing using the following syntax
factorial(5)
## [1] 120
Have you noticed the hashtag β#β before certain descriptions? The hashtag signals to R that the text following it on the same line is a comment and should not be processed or saved as part of the code. Comments are not essential for code execution but are valuable for explaining and recalling the purpose of the code. Annotating code is considered a best practice in programming.
A vector is used to store collected elements of the same data type in R. These elements include numbers and characters. It may be easier to see examples to understand the concept of vector.
#calculate multiplications
5*4*3*2*1
## [1] 120
#you can also do the same thing using the following syntax
factorial(5)
## [1] 120
#you can create numeric vectors
a1 <- 1
a2 <- 2
#you can create character vectors
b1 <- "criminal"
b2 <- "justice"
Did you notice that
<- was used here? In R, the <- symbol is used as an assignment operator to assign values to variables. It is used to create or update vectors by assigning a value or expression to a vector name. This operator is often referred to as the βleft arrowβ operator. Here, we created two vectors, a1 and a2, and assigned 1 and 2, respectively. As you can see above, when you create character vectors, you need to enclose the text or characters within double quotation marks. If you want to check if vector values are properly assigned, you can type a vector name.
#you can double check the vector values
a1
## [1] 1
a2
## [1] 2
b1
## [1] "criminal"
b2
## [1] "justice"
Let us continue to use R as a calculator. We can calculate using the vectors we created earlier. Using vectors makes our analysis much easier and clearer. A vector can contain multiple elements, and we can write simple code to perform more complex calculations.
#you can add numeric vectors
a1+a2
## [1] 3
#you can multiply numeric vectors
a1*a2
## [1] 2
#you can subtract vectors
a1-a2
## [1] -1
#you can divide vectors
a1/a2
## [1] 0.5
Please note that you cannot calculate using character vectors. For example,
b1+b2 would not work in R. You can combine multiple values of the same data type into a single vector using the c() function.
#create a numeric vector
x <- c(1,2,3,4,5)
#you can check the outcome of this function
x
[1] 1 2 3 4 5
#create a character vector
y <- c("Life", "is", "good")
You can use a function to vectors in R. A function refers to a block of code that allows us to perform different types of tasks. Let me give you an example demonstrating how we can use R to simplify a task.
# There are many ways to calculate the arithmetic mean of a numeric vector. You can type.
(1+2+3+4+5)/5
## [1] 3
# But, an easier way to accomplish the same task is to apply a function to the vector we created earlier.
mean(x)
## [1] 3
The
mean() function calculates the arithmetic mean or average of a numeric vector or a group of numeric values. You can compute the arithmetic mean by dividing the sum of all values by the total number of values.
