Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit errors #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
12 changes: 3 additions & 9 deletions exercise-1/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
19 changes: 3 additions & 16 deletions exercise-2/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
30 changes: 13 additions & 17 deletions exercise-3/exercise.R
Original file line number Diff line number Diff line change
@@ -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!)