diff --git a/README.md b/README.md index b9f20a3..43619df 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ As you may have noticed, R comes with a large number of functions that are built | --- | --- | --- | | `sum(a,b,...)` | Calculates the sum of all input values | `sum(1, 5)` returns `6`| | `round(x,digits)` | Rounds the first argument to the given number of digits | `round(3.1415, 3)` returns `3.142` | -| `toupper(str)` | Returns the characters in lowercase | `toupper("hi there")` returns `"HI THERE"` | +| `toupper(str)` | Returns the characters in uppercase | `toupper("hi there")` returns `"HI THERE"` | | `paste(a,b,...)` | _Concatenate_ (combine) characters into one value | `paste("hi", "there")` returns `"hi there"` | | `nchar(str)` | Counts the number of characters in a string | `nchar("hi there")` returns `8` (space is a character!) | | `c(a,b,...)` | _Concatenate_ (combine) multiple items into a _vector_ (see [module-7](https://github.com/info201-w17/module7-vectors)) | `c(1, 2)` returns `1, 2` | diff --git a/exercise-1/exercise.R b/exercise-1/exercise.R index da535d4..7357ab3 100644 --- a/exercise-1/exercise.R +++ b/exercise-1/exercise.R @@ -2,21 +2,15 @@ # Define a function `AddThree` that takes a single argument and # returns a value 3 greater than that input -AddThree <- function(value) { - return(value + 3) -} + # Create a variable `ten` that is the result of passing 7 to your `AddThree` function -ten <- AddThree(7) + # Define a function `ImperialToMetric` that takes in two arguments: a number of feet # and a number of inches # The function should return the total length in meters -ImperialToMetric <- function(feet, inches) { - total.inches <- feet*12 + inches - return(total.inches * 0.0254) -} + # Create a variable `height.in.meters` by passing your height in imperial to the # `ImperialToMetric` function -height.in.meters <- ImperialToMetric(5,11) diff --git a/exercise-2/exercise.R b/exercise-2/exercise.R index 2fcf7a9..6ff1839 100644 --- a/exercise-2/exercise.R +++ b/exercise-2/exercise.R @@ -3,27 +3,14 @@ # Define a function `CompareLength` that takes in 2 character strings, and returns # the sentence of the form # "The difference in length is N" -CompareLength <- function(str1, str2) { - diff <- abs(nchar(str1) - nchar(str2)) - diff.string <- paste('The difference in length is', diff) - return(diff.string) -} + # Pass two strings of different lengths to your `CompareLength` function -CompareLength("Hey", "Hello ladies and gentlemen, how are you today?") + # Define a function `DescribeDifference` that will return one of the following statements: # "Your first string is longer by N characters" # "Your second string is longer by N characters" -DescribeDifference <- function(a,b) { - diff <- nchar(a) - nchar(b) - if(diff > 0) { - sentence <- paste('Your first string is longer by', diff, 'characters') - } else { - sentence <- paste('Your second string is longer by', -diff, 'characters') - } - return(sentence) -} + # Pass two strings of different lengths to your `DescribeDifference` function -DescribeDifference("Abracadabra!", "Presto!") diff --git a/exercise-3/exercise.R b/exercise-3/exercise.R index 0a9cb13..34ebd68 100644 --- a/exercise-3/exercise.R +++ b/exercise-3/exercise.R @@ -1,44 +1,40 @@ # Exercise-3: Calling built-in functions # Create a variable `my.name` that contains your name -my.name <- "Joel Ross" -# Create a variable `name.length` that holds how many letters are in your name (use the `nchars()` function) -name.length <- nchar(my.name) + +# Create a variable `name.length` that holds how many letters are in your name (use the `nchar()` function) + # Print the number of letters in your name -print(name.length) + # Create a variable `now.doing` that is your named followed by "is programming!" to the end of your name # (use the `paste()` function) -now.doing <- paste(my.name, "is programming!") + # Make the `now.doing` variable upper case -toupper(now.doing) + ### Bonus # Pick two of your favorite numbers (between 1 and 100) and assign them to `fav.1` and `fav.2` -fav.1 <- 12 -fav.2 <- 87 + # Divide each number by the square root of 201 and save it as the same variable -fav.1 <- fav.1/sqrt(201) -fav.2 <- fav.2/sqrt(201) + # Create a variable `raw.sum` that is the sum of those two variables. Use the `sum()` function for practice -raw.sum <-sum(fav.1, fav.2) + # Create a variable `round.sum` that is the `raw.sum` rounded to 1 decimal place. Use the `round()` function -round.sum <- round(raw.sum, 1) + # Create two new variables `round.1` and `round.2` that are your `fav.1` and `fav.2` variables rounded # to 1 decimal place -round.1 <- round(fav.1, 2) -round.2 <- round(fav.2, 2) + # Create a variable `sum.round` that is the sum of the rounded values -sum.round <- sum(round.1, round.2) -# Which is bigger, `round.sum` or `sum.round`? (You can use the `max()` function!) -max(sum.round, round.sum) + +# Which is bigger, `round.sum` or `sum.round`? (You can use the `max()` function!) \ No newline at end of file