M3 - Coding Challenge Answers
Be sure you made a sincere attempt at these on your own! If you have any questions at all, be sure to follow up with an instructor or TA – or ask on Slack!
Creating vectors & dataframes
1a. Create vectors of high and low temperatures
```{r DataExercise-1a}
#Create vectors
highs_F <- c(68,72,62,58,53,54,55,56,58,58)
lows_F <- c(64,57,56,40,30,36,31,35,37,38)
```
1b. Create a function to convert F to C and apply the function
```{r DataExercise-1b}
#Create a function
f_to_c = function(f){
c <- (f-32)*5/9
return(c)
}
#Apply function to vectors
highs_C <- f_to_c(highs_F)
lows_C <- f_to_c(lows_F)
```
1c. Bring all vectors into a single dataframe
```{r DataExercise-1c}
#Create dataframe from vectors
temperatures <- data.frame(highs_F,lows_F,highs_C,lows_C)
```
1d. Apply suammary()
and sd()
commands
```{r DataExercise-1d}
#Reveal summary
summary(temperatures)
#Standard deviation - has to be a vector, not a dataframe
sd(temperatures$highs_F)
```
Dates with Lubridate
#Ex_3 - on your own...
str_juneteenth <- "19 June 1865"
#Since the format is month-day-year we will use function mdy()
date_juneteenth <- dmy(str_juneteenth)
date_juneteenth
```