Rongen Zhang
R is a programming environment
Step by step installation guides from YouTube:
RGui is an interactive R environment that comes with R installation, but it is very basic and not so user-friendly.
RStudio is a development environment for R, and provides many advanced features to improve efficiency and ease of use for R users.
This is the most important panel, because this is where R actually does stuff
Collections of commands (scripts) can be edited and saved.
There are ways to speed up the workflow:
If you don’t select any code, R will just execute the line where the blinking cursor is
Instead of clicking the “Run” icon, you can just use the keyboard shortcut: Ctrl + Enter
Menu (on the top) -> Tools -> Global Options
Like a calculator, R also has many functions that let you do more sophisticated manipulations.
## [1] 2
## [1] 6
## [1] 3
Note:
# (number sign) to comment your codes#There will be many occasions where you want to learn more about a built-in command or function. Type help(function_name) or ?function_name to get more information. For example:
Use two question marks to search the whole help database, especially when you don’t know exactly the function name. For example,
What do you think this command will return?
factorial(round(2.05) + 1)
R always works from the innermost parentheses to the outermost (just like a calculator)
factorial(round(2.05) + 1)
–> factorial(2 + 1)
–> factorial(3)
–> 6
R can recognize different types of data:
Any number, no quotes.
Appropriate for math.
## [1] "numeric"
Any symbols surrounded by single quotes (’) or double quotes (")
## [1] "character"
## [1] 28
## [1] "UNSTRUCTURED DATA MANAGEMENT"
## [1] "Unstructured_Data_Management"
How many characters are in the following strings:
123Emailrzhang6@gsu.eduUse paste to join the following words so that the result looks like How#are#you? (hint: ?paste)
Logical values are either TRUE or FALSE (Note: they are uppercase).
## [1] TRUE
## [1] FALSE
## [1] TRUE
R’s form of categorical data. Saved as an integer with a set of labels (e.g. levels)
## [1] FL GA AZ
## Levels: AZ FL GA
## [1] "factor"
## [1] "AZ" "FL" "GA"
is.XYZ(x) function returns TRUE/FALSE for whether x is of type XYZ
as.XYZ(x) (tries to) “cast” x to type XYZ — to translate it sensibly into a XYZ-type value
## [1] FALSE
## [1] TRUE
## [1] FALSE
## [1] TRUE
## [1] "2.5"
## [1] 2.5
## [1] 5
We can give names to data objects; these give us variables
Variables are created with the assignment operator, <- or =
Be careful that R is a case sensitive language. FOO, Foo, and foo are three different variables!
x = 2 # use the equal sign to assign value
y <- 3 # you can also use an arrow to assign value
x # print the value of a variable by typing its name## [1] 2
## [1] 6
The assignment operator also changes values:
## [1] 2
## [1] 8
Using names and variables makes code: easier to design, easier to debug, less prone to bugs, easier to improve, and easier for others to read
Variable names cannot begin with numbers. Wise to avoid special characters, except for period (.) and underline (_)
Example of valid names:
Example of invalid names:
## [1] "How are you?"
## [1] 3
f_name and l_name with value equals to your own first/last namesf_name and l_name and save them to length_f_name and length_l_name respectivelylength_f_name multiplied by length_l_namelength_f_name divided by length_l_namelength_f_name is greater than length_l_nameThe instructor will share the R code from this lab and post it under the “In class R script” section of the content area.
No lab assignment for this week.