Doubt in exercise of R

Asked

Viewed 81 times

0

I’m having trouble doing the following exercise, someone with R knowledge could help in this case

In a long jump competition each athlete is entitled to five jumps. The result of the athlete will be determined by the average of the five remaining values. You must make a program that receives the name and the five distances reached by the athlete in its jumps and then enter the name, the jumps and the average of the jumps. The program must be closed when the name of the athlete is not given. The output of the program must follow the example below:

Atleta: Rodrigo Curvêllo

1 Salto: 6.5
2 Salto: 6.1
3 Salto: 6.2
4 Salto: 5.4
5 Salto: 5.3

Final result:

Atleta: Rodrigo Curvêllo
Saltos: 6.5 - 6.1 - 6.2 - 5.4 - 5.3
Média dos saltos: 5.9 m

I made it that far

while(TRUE){


atletas[1,1] <- readline(prompt = "digite o nome do atleta: ")  

if(atletas == '') {
  break
}

if (atletas != '')
  for (i in 2:6)
  {  
  atletas[1,i] <- readline(prompt = "digite o salto: ") 
  }
} 

1 answer

0

I wrote the code based on snippet that you wrote in your question.

while(TRUE) {
  atleta <- readline(prompt = "digite o nome do atleta: ")
  if (atleta == '') {
    break
  }
  dframe <- matrix(nrow=5, ncol=2)
  for (i in seq(5)) {
    salto <- readline(prompt = "digite o salto: ")
    dframe[, 1] <- atleta
    dframe[i, 2] <- salto
      if (i == 5) {
        print(paste('Atleta: ', dframe[1,1]))
        print(paste(dframe[, 2], sep=' - '))
        print(paste('Media: ', mean(as.numeric(dframe[, 2]))))
        dframe <- matrix(nrow=5, ncol=2)
      }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.