A little func challenge for begginer users+question

So.. I decided to create a function for the Fibonacci sequance and woohoo made it!
I'll share it here because, well, im kinda happy for it but also i have some question.

If you want to take on the challenge and create a functuin (or a loop) to make the Fibonacci seq+Golden ratio, this part is SPOILER

fibonacci <- function(x){
  for(s in 1){
    a <- c(0,1)
    gold <- c(0,1)
    x <- x-2
    for (i in 1:x) {
      a[i+2] <- sum(a[i],a[i+1])
      gold[i+2] <- a[i+1]/a[i]
    }
  
  }
  return(data.frame(Fibonacci=a,"Golden Ratio"=gold))
}
# Example fibonacci(5)

*I actually realised i didnt have to use the first loop. Ill leave it anyway here since its my final version of the function WITHOUT seeing others

My questions are:

  • When i used tibble instead of data.frame it round the numbers to 2 decimals only. can i fix this?

  • even in a data.frame format, and actually in all R calculations, it didnt calculate a more accurate number than 1.618034. can it be fixed?

  • Another issue that i fixed by using x = x-2 is that i cant figure out a kneet way to tell R to run only till x, because i already created the first two numbers in the sequance.

anyway.. if you have cooler ways to make this, please share, and for us newbies i think its a cool chalenge!

The numbers aren't rounded to two decimals. That's just the displayed output.

The equivalent applies for the accuracy.

I see. still, since i want to display the result in my output, its a shame that that tibble, which is much nicer, doesnt show the entire decimals.
thanks though

You can adjust the default display output for tibbles:
Printing tibbles — formatting • tibble (tidyverse.org)
Package options — pillar_options • pillar (r-lib.org)
Significant figures and scientific notation • pillar (r-lib.org)

However, the console output is not the best option for using in publications. You could use one of the many table packages in RMarkdown or export the output as a file, e.g. csv.

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.