Lab 1A: R Language Basics (Easy)

1. Data Types

  1. Create and print a numeric.
n <- 1.23

print(n)
## [1] 1.23

2. Data Structures

  1. Create and print a vector.
v <- c(1, 2, 3);

print(v)
## [1] 1 2 3

3. Data Frames

  1. Create a data frame.
df <- data.frame(
  Name = c("Cat", "Dog", "Cow", "Pig"), 
  HowMany = c(5, 10, 15, 20),
  IsPet = c(TRUE, TRUE, FALSE, FALSE))
  1. Print a data frame.
print(df)
##   Name HowMany IsPet
## 1  Cat       5  TRUE
## 2  Dog      10  TRUE
## 3  Cow      15 FALSE
## 4  Pig      20 FALSE
  1. Index the data frame by row 1 and column 2.
df[1, 2]
## [1] 5
  1. Index the data frame by column name (shorthand).
df$HowMany
## [1]  5 10 15 20
  1. Subset rows using the equality operator.
df[df$IsPet == TRUE, ]
##   Name HowMany IsPet
## 1  Cat       5  TRUE
## 2  Dog      10  TRUE