-
-
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") | |
} |
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??
struck at next step of this