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 8844375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:28:16+00:00 2026-06-14T11:28:16+00:00

Possible Duplicate: Why are these numbers not equal? I’ve come across a very bizarre

  • 0

Possible Duplicate:
Why are these numbers not equal?

I’ve come across a very bizarre situation. If I store a vector (sequence) in a text file with the following code:

fileConn<-file('test.txt')
sink(fileConn,append=T,split=T)
cat('sequence','\n')
cat(as.character(unlist(seq(0,1,0.1))),'\n')
sink()
close(fileConn)

And then load it again:

test=readLines('test.txt')

I then try and extract the same vector I’ve stored in the text file and compare to the original sequence using 2 “different” approaches:

sequence1=laply(strsplit(test[2]," ")[[1]],as.numeric)
sequence2=as.numeric(strsplit(test[2]," ")[[1]])

What’s bizarre is that even though they look and (apparently) are the same type of vectors, R seems to think they’re not!!!

cbind(seq(0,1,0.1),sequence1,sequence2)

          sequence1 sequence2
 [1,] 0.0       0.0       0.0
 [2,] 0.1       0.1       0.1
 [3,] 0.2       0.2       0.2
 [4,] 0.3       0.3       0.3
 [5,] 0.4       0.4       0.4
 [6,] 0.5       0.5       0.5
 [7,] 0.6       0.6       0.6
 [8,] 0.7       0.7       0.7
 [9,] 0.8       0.8       0.8
[10,] 0.9       0.9       0.9
[11,] 1.0       1.0       1.0

apply(cbind(seq(0,1,0.1),sequence1,sequence2),2,class)
          sequence1 sequence2 
"numeric" "numeric" "numeric"



apply(cbind(seq(0,1,0.1),sequence1,sequence2),2,nchar)
        sequence1 sequence2
 [1,] 1         1         1
 [2,] 3         3         3
 [3,] 3         3         3
 [4,] 3         3         3
 [5,] 3         3         3
 [6,] 3         3         3
 [7,] 3         3         3
 [8,] 3         3         3
 [9,] 3         3         3
[10,] 3         3         3
[11,] 1         1         1

sequence1==seq(0,1,0.1)
 [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE

sequence2==seq(0,1,0.1)
 [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE

Does anybody have any clue why this happens and how I can prevent it from happening?
Thanks very much!

  • 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-06-14T11:28:17+00:00Added an answer on June 14, 2026 at 11:28 am

    You want to do the comparison using all.equal(), which allows for some fuzz in the comparison. You can’t use == as that tests for exact equivalence and that is not something
    you should nor want to be doing on floating point data.

    > all.equal(unlist(sequence1), seq(0, 1, 0.1))
    [1] TRUE
    > all.equal(sequence2, seq(0, 1, 0.1))
    [1] TRUE
    

    To get the output you wanted we need to work a little harder:

    > sapply(seq_along(sequence1), function(i, x, y) all.equal(x[[i]], y[i]),
    +        x = sequence1, y = seq(0, 1, 0.1))
     [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
    > sapply(seq_along(sequence2), function(i, x, y) all.equal(x[i], y[i]),
    +        x = sequence2, y = seq(0, 1, 0.1))
     [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
    

    Note those last two a slightly different as sequence1 is a list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why are these numbers not equal? The below expression, which evaluates to
Possible Duplicate: Why are these numbers not equal? Can anyone explain to me, why
Possible Duplicate: Why are these numbers not equal? in Gnu R: which(seq(0, 1600, 0.05)
Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
Possible Duplicate: class System.Random .. why not static? Following on from Generated random numbers
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i ,
Possible Duplicate: How can I count the numbers of rows that a mysql query
Possible Duplicate: Generating Random Numbers in Objective-C Hi I am creating an app where
Possible Duplicate: regular expression for letters, numbers and - _ I am creating a

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.