user5875780
user5875780

Reputation:

How to write a sequence

I want to write this sequence in R, 1, 1/3, 1/5,1/7,...,1/21
I have:

x <- seq(1,21,by=2)  
y<- 1/x
print(y)

I got:

1.00000000 0.33333333 0.20000000 0.14285714 0.11111111 0.09090909 0.07692308 0.06666667 0.05882353 0.05263158 0.04761905 

which is the same as the sequence,

Now is there another way to get the numbers as fraction without using the fraction package?

Upvotes: 2

Views: 556

Answers (1)

Onyambu
Onyambu

Reputation: 79208

Try using the as.fractions in the MASS package

 x <- seq(1,21,by=2)
  y<- 1/x
  MASS::as.fractions(y)
  [1]    1  1/3  1/5  1/7  1/9 1/11 1/13 1/15 1/17 1/19 1/21

Upvotes: 5

Related Questions