```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## One dimension
### Creation

Create the following 1-d structures:
- `a`: a random vector of integers with 10 elements drawn from 1-20:
  - Use the `sample` function with `set.seed(10)`
  - Name the elements of `a` with a vector of names starting with "V1" and ending with "V10". 
      - Use the `paste0` function to create those names.
      - Create the identical vector of names using the `paste` function.
- `b`: use `a` as index to select from `letters`. 
- `d`: a random vector of integers with 10 elements from a normal distribution with a mean = 100 and an sd of 20:
  - Use `rnorm` function with `set.seed(12)`.
- Create a list `l` from `a`, `b`, `d`. 
    - Assign the name "a", "b", and "d" for the corresponding `l`'s element.

```{r}
## a, vector of integers with 10 elements randomly from 1-20.
set.seed(10)
a <- sample(1:20, 10)
names(a) <- paste0('V', 1:10)
a
names(a) <- paste('V', 1:10, sep = '')
a

## b, random from letters
b <- letters[a]

## d, 10 random number from normal distribution with mean 100 and sd 20
set.seed(12)
d <- rnorm(10, mean = 100, sd = 20)
d

## l, a list made by a, b and d
l <- list(a = a, b = b, d = d)
l
l <- list(a, b ,d)
names(l) <- c('a', 'b', 'd')
```

### Indexing, subsetting, and replacing

- Select the 1st, 2nd, and 10th elements from `a`
- Select the elements of `a` named V1, V2, V3 (use the names)
- Replace the second to last value of `a` with the word "sasquatch"
    - Use code to find the index value, not the actual integer value of the index
- Select from `b` the values "c", "d", "e" if there is any (`%in%`)
- Identify the index position in `b` of values "c", "d", "e" if there is any
- Select the first 5 values of `d` and the last 5 values of `d` into two separate vectors and multiply them.
- Select from `d` all values > 100:
    - How many values are there?
- Select from `d` all values between 95 and 105, and replace them with 100
- Repeat steps 1, 3, 4, and 8 above, but do it by accessing `a`, `b`, and `d` from `l`

```{r}
## a
a[c(1, 2, 10)]
a[c(paste0('V', 1:3))]
a[2:length(a)] <- "sasquatch"
a

## b
b[b %in% c('c', 'd', 'e')] # select out the values
which(b %in% c('c', 'd', 'e')) # give back the index of these values

## d
d[1:5] * d[-5:-1] # d[-5] d[-c(1:4)]
d[d > 100]
length(d[d > 100])
## or
sum(d > 100)
d[d > 95 & d < 105] <- 100
d

## from l
### 1
## within a list, l$a the same as a l[[1]]
l$a[c(1, 2, 10)]

### 3
a[2:length(a)] <- "sasquatch"
l$a[2:length(l$a)] <- "sasquatch"
l

### 4
l$b[l$b %in% c('c', 'd', 'e')]

### 8
l$d[l$d > 95 & l$d < 105] <- 100
```

## Two dimensions
### Creation

Create the following 2-d structures:
- `m`: a matrix with three integer columns named "V1", "V2", "V3"
  - Create each column first as its own vector, then combine
     - `V1` is 1:10
     - `V2` is a random sample between 1:100 using `set.seed(50)`
     - `V3` is drawn from a random uniform distribution between 0 and 50. Use the same seed as before.
  - Inspect the `str` and `class` of `m`
- `dat`, a data.frame built from `V1`, `V2`, `V3`, and `V4`
  - `V4` is a random selection of the letters A-E (Try `LETTERS`?), use the same seed as above.
  
```{r}
## m
V1 <- 1:10
set.seed(50)
V2 <- sample(1:100, 10)
set.seed(50)
V3 <- runif(10, 0, 50)
m <- cbind(V1 = V1, V2 = V2, V3 = V3)
m
str(m)
class(m)

## dat
set.seed(50)
V4 <- sample(LETTERS, 10)
dat <- data.frame(m, V4 = V4)
# or
dat <- data.frame(V1 = V1, V2 = V2, V3 = V3, V4 = V4)
dat
```

### Indexing, subsetting, and replacing

- Select the *first 10 values* from `m`.
- Use a single vector to select the last row, column value from `m`
    - Think about different ways!
- Replace the value selected in 2 above with -99
- Now select row 3, columns 1:2 from `m`, and replace them with their values multiplied by 10
- Do the same, but select the columns by their name, and reset the new values by dividing by 10
- Select from `dat` the values of V3, and square them. Do it using index notation, column name in `[]`, and `$`
- Subset the first two rows and columns of `dat` into a new data.frame `datss`.
- Replace `dat` rows 1:2, column 1:2 with the values -1:-4
- Reset the part of `dat` you just changed with the values in `datss`

```{r}
## m
m[1:10]

m[length(m)]
m[nrow(m), ncol(m)]

m[length(m)] <- -99
m

m[3, 1:2] <- m[3, 1:2] * 10
m

m[3, c('V1', 'V2')] <- m[3, c('V1', 'V2')] / 10
m

dat$V3^2
# or
dat[['V3']]^2
# or
dat[, 'V3']^2
dat['V3']^2

datss <- dat[1:2, 1:2]
datss

dat[1:2, 1:2] <- -1:-4
dat

dat[1:2, 1:2] <- datss
dat
```

