Skip to content

Instantly share code, notes, and snippets.

@seankross
Created March 21, 2015 15:35
Show Gist options
  • Save seankross/912fe3b9f19a91b0a7bd to your computer and use it in GitHub Desktop.
Save seankross/912fe3b9f19a91b0a7bd to your computer and use it in GitHub Desktop.
Swirl R Programming Script Answers
"%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")
}
@pandhari416
Copy link

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

@pandhari416
Copy link

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

@EonJaw
Copy link

EonJaw commented Jul 28, 2022

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...

@Carmendom
Copy link

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=" ")
}

@drdanbad
Copy link

u save before hitting submit (

I am having the same issue and I have saved my script - any other solutions?

@EleniB98
Copy link

EleniB98 commented Feb 5, 2024

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??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment