SSGC 8802 A Brief Introduction To R - GUI
SSGC 8802 A Brief Introduction To R - GUI
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.)
• 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.
• 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.
• 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.
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.
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.
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()
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.,
>?`<-`
>?`+`
>?`?`
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
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.