Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5964779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:29:43+00:00 2026-05-22T19:29:43+00:00

Is it okay to use floating-point numbers as indices or when creating factors in

  • 0

Is it okay to use floating-point numbers as indices or when creating factors in R?

I don’t mean numbers with decimal parts; that would clearly be odd, but instead numbers which really are integers (to the user, that is), but are being stored as floating point numbers.

For example, I’ve often used constructs like (1:3)*3 or seq(3,9,by=3) as indices, but you’ll notice that they’re actually being represented as floating point numbers, not integers, even though to me, they’re really integers.

Another time this could come up is when reading data from a file; if the file represents the integers as 1.0, 2.0, 3.0, etc, R will store them as floating-point numbers.

(I posted an answer below with an example of why one should be careful, but it doesn’t really address if simple constructs like the above can cause trouble.)

(This question was inspired by this question, where the OP created integers to use as coding levels of a factor, but they were being stored as floating point numbers.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T19:29:44+00:00Added an answer on May 22, 2026 at 7:29 pm

    It’s always better to use integer representation when you can. For instance, with (1L:3L)*3L or seq(3L,9L,by=3L).

    I can come up with an example where floating representation gives an unexpected answer, but it depends on actually doing floating point arithmetic (that is, on the decimal part of a number). I don’t know if storing an integer directly in floating point and possibly then doing multiplication, as in the two examples in the original post, could ever cause a problem.

    Here’s my somewhat forced example to show that floating points can give funny answers. I make two 3’s that are different in floating point representation; the first element isn’t quite exactly equal to three (on my system with R 2.13.0, anyway).

    > (a <- c((0.3*3+0.1)*3,3L))
    [1] 3 3
    > a[1] == a[2]
    [1] FALSE
    

    Creating a factor directly works as expected because factor calls as.character on them which has the same result for both.

    > as.character(a)
    [1] "3" "3"
    > factor(a, levels=1:3, labels=LETTERS[1:3])
    [1] C C
    Levels: A B C
    

    But using it as an index doesn’t work as expected because when they’re forced to an integer, they are truncated, so they become 2 and 3.

    > trunc(a)
    [1] 2 3
    > LETTERS[a]
    [1] "B" "C"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to ask if is it okay that I use native PHP
OKay I don't use actionscript and trying to help my friend edit a flash
Okay, so here's my problem: We use FOP for creating pretty report output. We
I recall reading somewhere that in HTML5 it was no longer okay to use
Is it okay to use array without single or double quotion like $array[key]? I
This might be a n00bish question, but whatever. Is okay to use exceptions for
Okay, I currently use an eggdrop IRC bot on my server. I want to
Okay so, I have to use an interface in a code I'm making involving
Okay so what i am trying to achieve is use the youtube api to
Okay, here's what I'm trying to do: I'm trying to use PHP to develop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.