You are doing great! Now it's time for a little wrap up of R syntax!
- To assign a value to a variable use the
<-
operator.
- Put string values in double quotes (
"This is a text"
) or single quotes ('This is also a text'
).
- There are two logical values in R:
TRUE
and FALSE
. We can use the following logical operators: <
, >
, <=
, >=
, !=
, &
(and), |
(or).
- A missing or unknown value is denoted by
NA
in R.
- Vectors let us store multiple values in one object. To create a vector use the
c()
function: family.age <- c(15,19,22,67,65)
- To create a factor, use the
factor(vector, levels)
function. The first argument gives values, the other specifies levels. If any value from vector is not listed in levels it would be changed to NA
.
- A data frame is R's way to store table data.
family <- data.frame(
name = c('Anna','Mark','Tom'),
age = c(30,32,28)
height = c(175, 180, 188))
- To access specific elements from a data frame specify their positions in square brackets:
family[c(1,3),c(2,3)]
The first vector is for rows, the second is for columns.
- To narrow a data frame to certain rows, specify the row vector and omit the column vector:
family[c(1,2),]
.
- To narrow a data frame to certain columns, specify the column vector and omit the row vector:
family[,c(1,2)]
.
- To get a whole column from a given data frame use the
$
sign with the column name: family$age
.