Analyze data with R, first project

Hey there,

I have just started the skill path called “Analyze data with R” and currently having trouble with the first project called “Day of the Race.”

Can someone point out what I am doing wrong?

1 Like

You have written for (r in race_results), however in your actual for loop you have never used the element r. Assuming race_results is an array, then the first place is the 0th index. So based on these last two sentences, can you think what small change you need to make to get it to work?

Thank you for your answer.

I realized I haven’t used the element “r” as you have mentioned. race_results is a vector and in R, index starts from 1 so I don’t think there is any problem.

Where I am confused is I want it to check through each positions (in race_results) corresponding string and compare it to function’s parameter, I think that’s where I have problem because I am not able to understand where I should put the “r” element…

You’re exactly right, my apologies! I’ve been working with other programming languages too much it seems.

Upon looking at the full code, I’ve realised your issue is actually 2 things. Firstly, the return statement should be formatted as return(place), I’m not 100% sure if codecademy compensates for this but in RStudio I had to do this to get it to work. And then the other part is your conditional. you’re looping for r in race_results, therefore you don’t need to select the vector element using race_results[place] anyway. As it’s r in race_results, this means that each r is an element of the vector from position 1 to the end. So you simply compare r to runner and then fix your returns and it should work perfectly.

I’m out right now and will be late back home but I’ll definitely try.

Thank you a lot for assisting me! :slight_smile:

Nope. I just can’t seem to get through this. Very frustrating… I don’t wanna move onto the next without getting this one done :confused:

Can you post up your whole code using the </> button in the editor, so I can take it and put it into my RStudio and see where the errors might be!

Yea sure, I have tried it one more time and still there are problems because I’m not doing anything with the loop variable:

---
title: "Day of the Race"
output: html_notebook
---

```{r}
# create friends vector here:
friends <- c("Megan", "Janet", "Tina")
# add on to the list here:
info_list <- list(
  Esther = list(
    jersey = 3432,
    color = "purple"
  ),
  Feng = list(
    jersey = 4221,
    color = "blue"
  ),
  Megan = list(
    jersey = 1363,
    color = "green"
  ),
  Janet = list(
    jersey = 6729,
    color = "green"
  ),
  Tina = list(
    jersey = 7501,
    color = "orange"
  )
)
print_information <- function(name) {
  print(paste(name, "is #", info_list[[name]]$jersey, "wearing the color", info_list[[name]]$color))
}
# call the print_information function on the friends vector:
print_information("Megan")
print_information("Janet")
print_information("Tina")
race_results <- c("Gi", "Francesca", "Lea", "Vivian", "Jessica", "Esther", "Mary", "Yasmina", "Megan", "Janet", "Tiffany", "Kishan", "Feng", "Z", "Tina")
# write find_place() here:
find_place <- function(runner) {
  place <- 1
  for (i in race_results) {
   if (race_results[place] == runner) {
     return place
   } 
   place <- place + 1
  }
  return place
}

# call and apply find_place() here:


It’s just this bit here, your race_results[place] should be i instead (it does work with place in this instance, but it’s good to get used to using best practice). and also your return statements need to be formatted return(place). That should fix it, other than that your code is good!

Thank you for assisting!

You’re right, once I changed the code started to appear on right side as well but when I call the function still nothing happens… :expressionless:

Hmm that is odd, I’m calling it and getting the correct results…what does your call look like?

I’m getting

find_place("Jessica")
// returns 5

find_place("Janet")
// returns 10

I do it exactly the same way… nothing happens.

I tried to insert print statements to see if iterations are happening but nothing pops up as well…

You helped a lot btw, thank you!

find_place <- function(runner) {
  place <- 1
  for (i in race_results) {
   if (race_results[i] == runner) {
     return(place)
   } 
   place <- place + 1
  }
  return(place)
}

I have the same problem!

This here may be your issue! This needs to be if (i == runner). The for loop is looking up for an element i in race_results. This means that the i being returned on the first loop will be the first element i.e. "Gi". As such, race_results[i] evaluates to an error. Change that to just i == runner and that should solve the issue.

Have a look through and try all my replies, if still no luck then post your entire code using the </> button on the reply editor and I can try it out!

No luck… I’m going to set-up Rstudio as well but, here you go (and again, thank you - really appreciating your help):

---
title: "Day of the Race"
output: html_notebook
---

```{r}
# create friends vector here:
friends <- c("Megan", "Janet", "Tina")
# add on to the list here:
info_list <- list(
  Esther = list(
    jersey = 3432,
    color = "purple"
  ),
  Feng = list(
    jersey = 4221,
    color = "blue"
  ),
  Megan = list(
    jersey = 1363,
    color = "green"
  ),
  Janet = list(
    jersey = 6729,
    color = "green"
  ),
  Tina = list(
    jersey = 7501,
    color = "orange"
  )
)
print_information <- function(name) {
  print(paste(name, "is #", info_list[[name]]$jersey, "wearing the color", info_list[[name]]$color))
}
# call the print_information function on the friends vector:
print_information("Megan")
print_information("Janet")
print_information("Tina")
race_results <- c("Gi", "Francesca", "Lea", "Vivian", "Jessica", "Esther", "Mary", "Yasmina", "Megan", "Janet", "Tiffany", "Kishan", "Feng", "Z", "Tina")
# write find_place() here:
find_place <- function(runner) {
  place <- 1
  for (i in race_results) {
   if (race_results[i] == runner) {
     return(place)
   } 
   place <- place + 1
  }
  return(place)
}

# call and apply find_place() here:

It gives me two errors I think:

  1. “attempt to use zero-length variable name” (I got no idea why it says this.)
  2. “Error in if (race_results[i] == runner) { : missing value where TRUE/FALSE needed” (I don’t understand this either, that if statement will be TRUE if it matches and FALSE if it doesn’t, isn’t it that way?)
find_place <- function(runner) {
  place <- 1
  for (i in race_results) {
   if (race_results[i] == runner) {
     return(place)
   } 
   place <- place + 1
  }
  return(place)
}

It should just be your if statement here that needs fixed, it should i == runner and not race_results[i] == runner. I think that should fix both of your issues.

YESS! Thank you. I was so focused on sticking to the skeleton that they have mentioned on the “hint” section of that exercise, I was missing this. Thank you a lot! Wouldn’t be able to do it w/o you!

1 Like

No problem at all! Hints can be useful but they can also tunnel vision us, sometimes all you need is that fresh pair of eyes. Glad you got it!

1 Like