0% found this document useful (0 votes)
22 views28 pages

SSGC 8802 A Brief Introduction To R - GUI

a brief introduction to R

Uploaded by

yc47398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views28 pages

SSGC 8802 A Brief Introduction To R - GUI

a brief introduction to R

Uploaded by

yc47398
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

A Brief Introduction to R:

Graphical User Interface


and Console
SSGC 8802
CHEUNG SHU FAI
2 0 2 4 - 2 0 2 5 1 ST S E M E S T E R
DE PA RTM ENT O F P SYC H O LOGY
FACU LT Y O F S O C I A L S C I E N C ES
U N I V ERSITY OF MACAU

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 1


Learning Outcomes

1) Can navigate the R Default GUI


environment.
2) Understand the basic ideas in
working in R.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 2


Outline
1) What is R? Why R?
2) Getting Started, Getting R
3) The R environment
4) Using R

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 3


Scope
•I will focus on Windows for introduction. Whether you
use Windows, Mac, or Linux should have no impact on
learning the course materials.
•Things can get complicated in real research. Be prepared
to search the Internet for problems and issues not
covered here.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 4


1) What is R? Why R
◦ R is a computing environment for data analysis. It is open
source and free to download and use.
◦ Everybody can contribute and improve it, and many experts
have contributed.
◦ Official website:
◦ https://www.r-project.org/
◦ Why R?
◦ In 2024, the question may be “Why not R?” … but may also be “Why not
Python”? “Why not X”?

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 5


1) What is R? Why R: Pros
1) R is so flexible that a lot of different techniques can be done in
one single environment.
2) Some techniques are only available in R, at least for now.
3) Free.
4) Cross-platform. Can be used in Windows, Mac, and Linux.
5) It forces us to think carefully what we are doing. Good for learning
(although difficult to learn).

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 6


1) What is R? Why R? Cons …
1) Not as user friendly as GUI oriented packages such as SPSS
2) Not as user friendly as Amos (for SEM)
3) Not as fast as Mplus for some models
4) Not as versatile as Python, which can be used in much much more
situations
•So, in my opinion, the question is not “Which one is the best.” If possible,
learn more, so you have options.
• E.g., if you usually use Program Y but you want use Method A, available only in
Program X, then you can still use Method A, and use Program X just for this
purpose.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 7


2) Getting Started, Getting R
Where to get it?
◦ The latest version can be downloaded from the official website:
◦ Go to https://www.r-project.org/ and find the link “download R.”

Versions
◦ For most of the operations, the versions of R should not matter (a lot).
◦ Fortunately, in Windows, by default each version of R is installed in its own
folder. Therefore, unlike other popular programs, you can install as many
different versions of R as you want and use them as independent programs.
(This is also possible in Mac and Linux, but you may need to take some
additional steps.)

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 8


2) ... My Opinions on Learning R...
1) Use it, by typing, testing, making mistakes, understanding error messages,
and find solutions. The more you use it, the better your (muscle) memory of
how to use it.
2) Don't force yourself to know every single option (arguments) of a function.
You can’t (I can't), or even if you can, you don't need to.
a) You should know enough basic options for the basic use of a function.
b) For other options of a function, learn and use them on demand. That is, when you suspect
you may need an option, check the help documentation to see if such an option exists.
3) For any problem, there are usually more than one way to solve it. Find one
that fits your working style. The most efficient solution may not be the most
readable solution.
4) Search the internet! No kidding. R is driven by the community of users.
Many users ask questions, and many helpful users answer questions.
SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 9
3) The R Environment
I will focus on the default environment first.
◦ Depending on your own preference, you may like working in RStudio, VS
Code, or even Positron, to be introduced. Unlike the common practice, for
this handout, I will focus on the default R environment. We will cover
other environments later.

Three windows (only the first appears by default)


◦ Console
◦ Editor
◦ Graph

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 10


3) The R Environment: Console
The console window works in an interactive
mode.
- You type something, and
- R does something.
Complicated? Most of you are already familiar
with a similar environment. ☺

• Though you may find typing something inefficient for complicated analyses,
sometimes, this mode is enough for quick tasks and can do them efficiently.
• Note: We need to save useful output ourselves, by copy and paste or other advanced option,
such as passing the output to a computer file.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 11


3) The R Environment: Editor
The editor window is simply, well,
a text editor.
But you can use this as a place to
write the commands, to organize
your analysis, and send them to
the console on demand, one by
one, block by block, or all at once.

• Even I myself rarely use this editor window. It is much more efficient to use the
script window in RStudio or other IDE (e.g., VS Code), covered later.
• Nevertheless, if you just need to try something quickly, this editor may be
enough.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 12


3) The R Environment: Graph
The graph window appears
whenever R generates a graph.

It is interactive. Whenever you


resize a window, the graph
resizes automatically.

• In real research, this graph window is usually just for a preview of a graph.
We will learn later how to use Quarto to generate one or many graphs
and save them to one single file.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 13


3) The R Environment: Menus
Most of the options in the menu are self-explanatory. I will not
discuss them one-by-one.
Most of the tasks can be done by issuing a command. I find typing
the commands sometimes faster than using the pull-down menu.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 14


4) Using R: "Commands" in the Console
(NOTE: If you have learned Python, be careful. There are some major differences in assigning values
to variables. Try to put away what you have learned in Python, for now.)
Most of the times, we use R by calling a function, sometimes with the output
(an object) assigned to a variable
E.g.,
> x <- c(1,2,3,4,5)
> mean(x)
In the first line, we use the function c() to combine (concatenate) five
numbers into one object and assign it to the variable x .
In the second line, we use the function mean() to compute the mean of x,
that is, the five numbers.
If we type "x_m <- mean(x)", then instead of printing the mean, the mean
will be assigned to another variable, x_m .

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 15


4) Using R: "Commands" in the Console
In reality, we assign an object to a name (a “symbol”), or “variable”
to some disciplines. The variable can be assigned to something else
later.
E.g.,
> x <- c(1,2,3,4,5)
> x <- c(2,4,6,8,10)

◦ Now x no longer holds (1,2,3,4,5). It holds (2,4,6,8,10).

Note: I use variable in this handout, though symbol is a better term.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 16


4) More On Functions
Many operations in R are function calls.
A function call is usually of this form:
> abc(x = 123, y = "this", z = my_var)

In this example, we call the function abc(), and the values for the argument
x, y, z, are 123, "this", and the variable my_var, respectively.
The value can be any expression. Therefore, this is also valid:
> abc(x = 123 + 300, y = c("this", "that), z = my_var)

The value of x is 423, and the value of y is the vector with "this" and "that".
Note: Some simply use abc, its name, to denote a function. Some add "()" to the end to remind readers
that they are functions. This is just a matter of style.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 17


4) More On Functions
For many functions, we can also omit the names. R will match the values to the arguments by
positions.

Assuming that the first argument of abc() is x, and the second argument is y. If we just use
abc(123, 456), R will assign 123 to x and 456 to y.

If you are sure about the order, you can omit the names. This is certainly OK for the first one or two
arguments of a function. If you are not sure, check the help page (described elsewhere), and use
the names in setting the argument values.

Some arguments have default values or are optional. For example, if y of abc() has the default
value of 0, then abc(123) is equivalent to abc(123, 0) or abc(x = 123, y = 0).

Nevertheless, if you find it difficult to remember what the default values are, or you want to
remind the readers what the default values are, then you can explicitly state the argument values
in the call.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 18


4) More On Functions
Try this and see what happens:
>mean(1, 2, 3, 4)

Is the answer what you expect? Why mean() returns that result?
Check the help page of mean, by typing this:
>?mean

SF Cheung 2024-
SSGC 8802 Why Quantitative Methods 08
19
4) abc() and abc Are Different
Some functions can be called without setting an argument. E.g.,
>ls()

This lists the variables (their names) in the current environment.


Note that we still have to use () to tell R that it is a function call.
Therefore, "ls()" and "ls" are different.
◦ ls() # calls the function ls
◦ ls # shows the content of ls

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 20


4) Help Pages Of Functions
If we want to learn more about a function, we can use ?, which we
used before:
>?mean

Many operations are functions, including <-, +, and even ?. To find the
help pages of these operators, enclose them in backticks, `, usually
placed on the top left of a keyboard, to the left of the 1 key. E.g.,
>?`<-`
>?`+`
>?`?`

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 21


4) To Assign or Not To Assign
It depends. Sometimes, we just want to print the output of a function call.
> mean(c(1, 2, 3))

R will call the function mean and print the results. The results are then "gone".
Sometimes, we just want the side effect of a function call.
> library(psych)

This will load the package psych (introduced later). That's it.
Sometimes, we want to keep the results and use the results later.
> x_mean <- mean(c(1, 2, 3))

The variable x_mean now refers to the results, i.e., the mean of 1, 2, and 3

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 22


4) Using R: "Commands" in the Console
Reminders:
1) In R, variable names are case sensitive. The variables EducationalLevel,
educationallevel, and EDUCATIONALLEVEL can coexist and will be
treated as three different variables.
2) You can use "." and underscore "_"in variable names. E.g., edu.level,
year1_gpa
3) Use single or double quotes to specify a string. E.g.,
"edu" is the 3-character word edu, but edu is the object named edu.
4) Function names are also case sensitive. Actually, when a function is defined, it is
assigned to a name, like other data. When you use mean(c(1,2,3), you tell R
to find the function that mean refers to.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 23


4) Using R: Scripts in the Editor
Imagine a drama without a script. You need to remember every lines and
movements, and you may forget what you did. With a script, you have a plan.
1) Sometimes it is more efficient to write a script and run the analysis by
submitting the commands.
2) With a script, you can also save an analysis and re-run it later. Script to R is
like syntax file to SPSS.
3) You can also use comments to annotate a script, using "#“ to “comment out”
a line.
◦ E.g.,
◦ # This line will not be run in R.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 24


4) Using R: The Global Environment (a.k.a. workspace)
What is the environment? To most users, it can Global Environment
be understood as a collection of all objects you
created. It can be saved and loaded. project3
That is, you can do some analyses, save the
x
workspace. Take a rest, and then load the cup
workspace and continue your work. All objects
are still there. cat coffee
However, this approach is no longer popular
(or not recommended, by som). To ensure Save
reproducibility, using a self-contained script or
Load
Quarto file is much more reliable. We will
cover this later. Disk or other storage
SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 25
4) Using R: The Working Directory
To save and load a file, we can use a fully qualified path, e.g.,
"C:/Temp/data.txt" (this and other examples on paths are for Windows).
But this is inconvenient to keep typing C:/Temp/ every time we save or load a
file.
We can set the working directory to the folder where we will save and load the
files. Then we can simply use the file names, e.g., "data.txt", and R will
automatically save and load the files in the working directory.
Functions:
◦ setwd("C:/Temp"): Set the current working directory to "C:/Temp"
◦ In Windows, we can also use setwd(choose.dir()) .
◦ getwd(): Show the current working directory.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 26


4) Using R: Packages
The major advantage of R lies in the use of packages.
A package is a collection of functions and other objects written to perform some tasks.
Functions
◦ install.packages("psych")
◦ Install the psych package. You will be prompted to select the CRAN mirror to download the package.
◦ Note that for this function, the name is passed as a string, “psych” .
◦ library(psych)
◦ Load the psych package. We cannot access objects in the package before loading it.
◦ Note that for this function, the name is passed as is, no quotes.
Depending on your computer, the installed packages may continue to exist after shutting
down, and so no need to install it again.
When you install a package, it is usually installed only in the copy of R you are using.
Therefore, if you install a new version of R, you need to install this package again. This can be
time consuming. We will talk about more efficient ways to install new versions of R later.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 27


4) Using R: Help!!
What to do when I need help for a function? Read some books.
Browse some websites, or type “?”
◦ E.g., Type this in the console: >?mean

If you just want to search for all documentations with the word
mean, you can type ??mean
You don’t need to understand all the details. Just focus on relevant
information.
There are also a lot of useful ebooks in UM library and a lot of
websites on how to use R.

SF Cheung 2024-08 SSGC 8802: A BRIEF INTRODUCTION TO R 28

You might also like