-
-
Save seankross/912fe3b9f19a91b0a7bd to your computer and use it in GitHub Desktop.
"%p%" <- function(left, right){ | |
paste(left, right) | |
} |
boring_function <- function(x) { | |
x | |
} |
evaluate <- function(func, dat){ | |
func(dat) | |
} |
mad_libs <- function(...){ | |
# Do your argument unpacking here! | |
args <- list(...) | |
place <- args[["place"]] | |
adjective <- args[["adjective"]] | |
noun <- args[["noun"]] | |
# Don't modify any code below this comment. | |
# Notice the variables you'll need to create in order for the code below to | |
# be functional! | |
paste("News from", place, "today where", adjective, "students took to the streets in protest of the new", noun, "being installed on campus.") | |
} |
my_mean <- function(my_vector) { | |
sum(my_vector)/length(my_vector) | |
} |
remainder <- function(num, divisor = 2) { | |
num %% divisor | |
} |
telegram <- function(...){ | |
paste("START", ..., "STOP") | |
} |
Hello, need help with the below. Thanks.
| Now try using evaluate() along with an anonymous function to return the last element
| of the vector c(8, 4, 0). Your anonymous function should only take one argument
| which should be a variable x
.
evaluate(function(x) {x[3]}, c(8, 4, 0))
[1] 0
| You almost had it, but not quite. Try again. Or, type info() for more options.
| You may need to recall how to index vector elements. Remember that your anonymous
| function should only have one argument, and that argument should be named x
. Using
| the length() function in your anonymous function may help you.
remainder <- function(num, divisor = 2) {
num %% divisor
}
[save]
submit()| Sourcing your script...
| Nice try, but that's not exactly what I was hoping for. Try again.
| Remember to set the appropriate default values!
This worked with me:
remainder <- function(num,divisor = 2) { num %% divisor}
submit()
| Sourcing your script...
| You're the best!
Hello, need help with the below. Thanks.
| Now try using evaluate() along with an anonymous function to return the last element
| of the vector c(8, 4, 0). Your anonymous function should only take one argument
| which should be a variablex
.evaluate(function(x) {x[3]}, c(8, 4, 0))
[1] 0| You almost had it, but not quite. Try again. Or, type info() for more options.
| You may need to recall how to index vector elements. Remember that your anonymous
| function should only have one argument, and that argument should be namedx
. Using
| the length() function in your anonymous function may help you.
The answer should be:
evaluate(function(x){x[length(x)]}, c(8,4,0))
[1] 0
Hello, need help with the below. Thanks.
| Now try using evaluate() along with an anonymous function to return the last element
| of the vector c(8, 4, 0). Your anonymous function should only take one argument
| which should be a variablex
.evaluate(function(x) {x[3]}, c(8, 4, 0))
[1] 0| You almost had it, but not quite. Try again. Or, type info() for more options.
| You may need to recall how to index vector elements. Remember that your anonymous
| function should only have one argument, and that argument should be namedx
. Using
| the length() function in your anonymous function may help you.The answer should be:
evaluate(function(x){x[length(x)]}, c(8,4,0))
[1] 0
Thank you! It worked!
Would you mind breaking it down for me? The part I am having problem understanding is that the length of c(8,4,0) is 3 but yet the result of the code is "0". Why? Thanks.
Now edit and save the R script bin_op.R.
Write your own binary operator below from absolute scratch! Your binary
operator must be called %p% so that the expression:
"Good" %p% "job!"
will evaluate to: "Good job!"
"%p%" <- function(){ # Remember to add arguments!
paste(left, right, sep = " ")
Sourcing your script...
| Not exactly. Give it another go.
| Remember: 'Hello' %p% 'student!' is how you use the binary operator.
Please help me I have tried everything
i have tried with reinstalling swirl package,
tried all the help extended by you people.
still unable to resolve the issue with anoynomus functions after evaluate function
evaluate(function(x){x+1},6)
can any one tell step by step to dos
I have resolved this problem. The correct code for evaluate function is:
evaluate <- function(func, dat) {
func(dat)
}
all you have to do is to clear the environment variables before submit().
Just execute the command rm(list=ls()) and then save the above code of evaluate function and then write submit(). It will work.thank you it worked for me
struck at next step of this
Would you mind breaking it down for me? The part I am having problem understanding is that the length of c(8,4,0) is 3 but yet the result of the code is "0". Why? Thanks.
Got stuck in this same spot since it wouldn't take "evaluate(function(x){x[length(c(8,4,0))]},c(8,4,0))" even though that seems to me to fulfil all stated requirements (although admittedly it is inelegant). The reason the function returns "0" is because while the length function determines that the vector consists of three elements, the x[stuff] piece specifies that what is returned from the vector is the item in the position identified by the value within the brackets - in this case, the 3rd value. The function of the brackets is to return a subset.
What I still don't understand is how it knows that the length of "function(x){x[length(x)]}" is 3. I thought "x" referred to the function as an unpopulated equation and that the dat element provided - the vector - was a separate element from the function designated by x. I guess once the vector is plugged in, it is part of the function, but at the point the length is taken, the data element has not yet been reached.
...just a counter-intuitive order of operations, I guess. Here I am stuck in that old Excel way of thinking...
Now edit and save the R script bin_op.R.
Write your own binary operator below from absolute scratch! Your binary
operator must be called %p% so that the expression:
"Good" %p% "job!"
will evaluate to: "Good job!"
"%p%" <- function(){ # Remember to add arguments! paste(left, right, sep = " ") Sourcing your script...
| Not exactly. Give it another go.
| Remember: 'Hello' %p% 'student!' is how you use the binary operator. Please help me I have tried everything
This worked
"%p%" <- function(left,right){ # Remember to add arguments!
paste(left,right,sep=" ")
}
u save before hitting submit (
I am having the same issue and I have saved my script - any other solutions?
Copy the code you received at the end of the swirl lesson and paste it into the corresponding question below.
What is the answer??
Its for quiz after week 1
I submited the swrl exercises but what code is the LAST code??
remainder <- function(num, divisor = 2) {
num %% divisor
}
[save]
submit()
| Sourcing your script...
| Nice try, but that's not exactly what I was hoping for. Try again.
| Remember to set the appropriate default values!