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")
}
@Dragonmasterx87
Copy link

Dragonmasterx87 commented Oct 26, 2019

Guys chill.

evaluate <- function(func, dat){
  func(dat)
}

here 'func' is itself a R function, sd, mean, median all that jazz. lol. You dont need to program it, its already ingrained in R.

After making the function, try evaluate(sd, c(1.4, 3.6, 7.9, 8.8)) it runs just fine. I was like since when does swirl have a bug lmao. 🤣

Some of the comments here are F$%^ing lit.

🐉

@mmartino97
Copy link

why does the telegram function not work when I'm copying exactly what others are posting, which is the same as what's posted in the discussion forums, and I've double checked what directory R is working with, and just incase, saved the script in several folders. I feel like an idiot writing down exactly what people have written above. But, I assure you it's the exact same code, lol.
Any ideas?
I had problems with the fun dat task and it finally worked when I made a hybrid code out of two peoples solutions!? I couldn't follow my own code after that but it worked...

@JPSantistebanQ
Copy link

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

Thanks a lot !**

THANKS!!!!!

@gopi28
Copy link

gopi28 commented Apr 9, 2020

rm(list=ls())

This is working

@Ankita18Mandal
Copy link

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

Thanks a lot !**

THANKS!!!!!

@Ankita18Mandal
Copy link

Thanks a lot!

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

This works!
Thanks a lot!

@charanhu
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

@nahin29
Copy link

nahin29 commented Nov 18, 2020

### please help. I stuck at my_mean function
my_mean <- function(my_vector) {
x <- sum(my_vector)/length(my_vector)
x
}

submit()

| Sourcing your script...

| You're close...I can feel it! Try it again.

| Use the sum() function to find the sum of all the numbers
| in the vector. Use the length() function to find the size
| of the vector

@AlphaWang777
Copy link

I submitted mad_libs and it seemed to be ok, I entered the function and arguments in the console but the variables were not returned as part of the sentence, just blanks. still said I was on a roll which was very encouraging

Try this:

mad_libs(place = "place name", adjective = "lots of", noun = "somthing")
it will work.
Because the ... does not give them an order, so we should call their names when we convert the vector

@OSotnychenko
Copy link

OSotnychenko commented Jan 27, 2021

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!

@Alisher555
Copy link

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.

@Alisher555
Copy link

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!

@albert456m
Copy link

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.

The answer should be:

evaluate(function(x){x[length(x)]}, c(8,4,0))
[1] 0

@Alisher555
Copy link

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.

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.

@PhuthiKulo
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

@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