W1: R Functions Flashcards
*
/
+
-
- * : Multiply
- ^ : Power
- / : Divide
- : Addition
- : Subtraction
How does R display output at the start
With this: (>)
Example
> 6 - 4/2
[1] 4
What are logical operators. == != > >= < <=
Provide True or False Response
- == Equality
- != Inequality
- > Greater than
- >= Greater than or equal to
- < Less than
- <= Lesser than or equal to
Example
> 10 < 100
[1] TRUE
> 2 + 2 == 5
[1] FALSE
How do we combine logical operators?
&
–
– !
- &: AND
- | :OR
- !: NOT
Example
> c (T & T, T | T, T & !T, T | !T)
[1] TRUE TRUE FALSE TRUE
What are functions and arguments
Output = Recipe (Ingredient 1, 2, 3, 4)
- Recipe
- Function
- Ingredients
- Arguments
Example
> sqrt(25)
- Square root
- Function
- 25
- Argument
In round (3.14159), which indicate the decimals
- round(3.14159)
- [1] 3
- round (3.14159, 2)
- [1] 3.14
- 2 indicates decimal place
- round (2, 3.14159)
- [1] 2
If argument names are used explicitly, like round (digits = 2, x = 3.14159, then it doesn’t matter
What is sqrt( round(4.65, 2) )
Nested functions goes from inner most to outer most
- sqrt( round(4.65, 2) )
- sqrt (4.65)
- 2.15
What are variables and values in R. How are they assigned?
Variables
- Storage to store Values
Values are assigned to variables by <-
Example
Box <- “Gandi”
Variable Box now contains value “Gandi”
What are the three types of variables are there?
- Numeric
- Store Numbers
- Box <- 212.23
- Store Numbers
- Character
- Store Text
- Box <- “Gandi”
- Store Text
- Logical
- Store True/False Values
- Box <- TRUE
- Store True/False Values
What are the 5 requirements for Variable Names
- Name must start with letter or a period
- a to z, or A to Z
- ”.”
- Letters, periods, numbers (0-9) can be used after
- Underscore (i.e, “_”) can also be used, but is best avoided (better to use a period)
- Name cannot contain a space, or characters like ?, +, !, #, $, %, (, ), etc.
- Name cannot be a reserved word, e.g., TRUE or function, that is used in R.
• Names are case-sensitive, therefore, e.g., myColour is different to mycolour, which are both different to
Mycolour, etc.
What are Vectors.
What are elements.
What are some requirements
Hence, what are the types of vectors
- Vectors
- Variable with >1 values
- Elements
- Values in the Vector
- Requirement
- Must be the same type, if not R will force them into the same type (Usually character)
Example
- Numeric (Automic) Vector
- > age <- c (34,2)
- > age
- [1] 34 2
- Character (Automic) Vector
- > Name <- c(“Dan”, “Alex”)
- > Name
- [1] “Dan” “Alex”
- Logical (Automic) Vector
- > Nerd <- c(TRUE, FALSE)
- > Nerd
- [1] TRUE FALSE
What are advantage of variables
- Store multiple values (vectors), and
- Be combined into data frames.
Vectors:
- Identifying
- Dropping
- Selecting
Identify / Select
- Elements can be identified by its position in the vector
- e.g. [1] means show 1st
- If elements have been named, can be identified by name
- e.g. [“home”]
- Elements can be selected by its position in the vector
- e.g. [c (1,3)] means show 1st and 3rd
- Elements can be selected by logical values or logical statements
- e.g. [c (TRUE, TRUE, FALSE)] or if like [boredom <.9] will say [TRUE] to those that are true and [FALSE] to vectors which are false
Drop
- Putting a “-“ sign
- e.g. [-2] means all vectors except the second one
In the environmental pane, what do Type, Length, Size, and Values indicate
Type
- Variable type
Length
- No. of elements in the variable
Size
- Amount of computer memory used by the variable.
Values
- (some of) the contents of the variable.
What are Data Frames
Collection of variables “bundled” together column-wise.
Organised as a case-by-variables matrix
Row:
- Values for one case or person on all variables.
Column
- Values for one variable over all cases.