Skip to content

replacing a value

11 messages · ani jaya, Jim Lemon, Jeff Newmiller +4 more

#
Dear R-Help,

I try to change a specific value here by following these:

https://www.journaldev.com/39695/replace-in-r
https://stackoverflow.com/questions/5824173/replace-a-value-in-a-data-frame-based-on-a-conditional-if-statement
https://stackoverflow.com/questions/54615462/how-to-replace-certain-values-in-a-specific-rows-and-columns-with-na-in-r
..and many more

however, it is not change anything. I believe it "should" be easy but
I think I am missing something. I am afraid it is my system that has a
problem but restarting r is not solve the problem.
I just want to change 20 to 0 in my data.
c(20, 20, 14.2375646029948, 19.9999999999999, 20, 20, 16.3092078677214,
20, 20, 20, 20, 20, 20, 20, 20, 14.8590932408795, 16.178935255298,
20, 20, 20, 20, 27.6404077886079, 20, 20, 20, 20, 20, 21.9857063037444,
20, 20, 20, 20)

what I did:
a[a==20]<-0 #fail
a<-replace(a,a==20,0) #fail
a[which(a==20)]<-0 #fail
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
system code page: 949

Best,
Ani
#
Hi Ani,
It seems to work for me:

 a<-c(20, 20, 14.2375646029948, 19.9999999999999, 20, 20, 16.3092078677214,
20, 20, 20, 20, 20, 20, 20, 20, 14.8590932408795, 16.178935255298,
20, 20, 20, 20, 27.6404077886079, 20, 20, 20, 20, 20, 21.9857063037444,
20, 20, 20, 20)
[1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
[13]  TRUE  TRUE  TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
[25]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
[1]  0.00000  0.00000 14.23756 20.00000  0.00000  0.00000 16.30921  0.00000
[9]  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000 14.85909
[17] 16.17894  0.00000  0.00000  0.00000  0.00000 27.64041  0.00000  0.00000
[25]  0.00000  0.00000  0.00000 21.98571  0.00000  0.00000  0.00000  0.00000

Notice that it didn't make 19.99999999999999 equal to 20, but did
round it up when printing the result. How do you know that it failed?

Jim
On Wed, Apr 6, 2022 at 12:48 PM ani jaya <gaaauul at gmail.com> wrote:
#
Hello Jim,

Thank you for the try. I am following your step and still have the problem.
But I know for sure now that my R/system is somewhat "broken" or I do
not know for sure.
[1] 20.00000 20.00000 14.23756 20.00000 20.00000 20.00000 16.30921
20.00000 20.00000 20.00000
[11] 20.00000 20.00000 20.00000 20.00000 20.00000 14.85909 16.17894
20.00000 20.00000 20.00000
[21] 20.00000 27.64041 20.00000 20.00000 20.00000 20.00000 20.00000
21.98571 20.00000 20.00000
[31] 20.00000 20.00000
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE
[17] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE FALSE FALSE
[1] 20.00000 20.00000 14.23756 20.00000 20.00000 20.00000 16.30921
20.00000 20.00000 20.00000
[11] 20.00000 20.00000 20.00000 20.00000 20.00000 14.85909 16.17894
20.00000 20.00000 20.00000
[21] 20.00000 27.64041 20.00000 20.00000 20.00000 20.00000 20.00000
21.98571 20.00000 20.00000
[31] 20.00000 20.00000
On Wed, Apr 6, 2022 at 12:16 PM Jim Lemon <drjimlemon at gmail.com> wrote:
#
Hi Ani,
Yes, there is something about the way your system is calculating
equality that is different from mine:

DELL Latitude laptop
Fedora 35 LINUX
R-4.1.3

Jim
On Wed, Apr 6, 2022 at 2:18 PM ani jaya <gaaauul at gmail.com> wrote:
#
I really don't think your R is broken, and I am puzzled why Jim didn't refer you to R FAQ 7.31 [1].

To cut to the chase... using == is not advised with floating point numbers, unless you know that the numbers have no fractional part. 19.9999999999999 prints as 20 given the default number of significant digits used to print numeric vectors, but that doesn't mean it _is_ 20.

is_near <- function( x, a, atol = 1e-6 ) {
  abs( x - a ) < atol
}
a[ is_near( a, 20 ) ] <- 0

Also see [2]... this is by no means unique to R.

[1] https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
[2] https://0.30000000000000004.com/
On April 5, 2022 9:18:01 PM PDT, ani jaya <gaaauul at gmail.com> wrote:

  
    
#
I believe you are not remembering that floating point numbers are not
exactly represented in decimal.
See FAQ 7.31

Here I have rounded a[4] to 12 places before comparing to 20.
The you get the answer you are looking for.
+ 20, 20, 20, 20, 20, 20, 20, 20, 14.8590932408795, 16.178935255298,
+ 20, 20, 20, 20, 27.6404077886079, 20, 20, 20, 20, 20, 21.9857063037444,
+ 20, 20, 20, 20)
[1] 20.00000 20.00000 14.23756 20.00000 20.00000 20.00000
[1] 20.000000000000000 20.000000000000000 14.237564602994800 19.999999999999901 20.000000000000000 20.000000000000000
[1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE
[1]  0.000000e+00  0.000000e+00 -5.762435e+00 -9.947598e-14  0.000000e+00  0.000000e+00
[1] FALSE
[1]  0.000000e+00  0.000000e+00 -5.762435e+00 -9.947598e-14  0.000000e+00  0.000000e+00
[1]  0.000000e+00  0.000000e+00 -5.762435e+00 -9.947598e-14  0.000000e+00  0.000000e+00
[1]  0.000000  0.000000 -5.762435  0.000000  0.000000  0.000000
[1]  0.00000  0.00000 14.23756  0.00000  0.00000  0.00000
#
Dear All,

I will remain sipping the seawater until it changes its taste if this
community does not exist.
Thank you.
[1] 19.999999999999982 19.999999999999982 14.237564602994826
19.999999999999940 19.999999999999982
[6] 19.999999999999982 16.309207867721351 19.999999999999982
19.999999999999982 19.999999999999982
[11] 19.999999999999982 19.999999999999982 19.999999999999982
19.999999999999982 19.999999999999982
[16] 14.859093240879485 16.178935255298033 19.999999999999982
19.999999999999982 20.000000000000028
[21] 19.999999999999982 27.640407788607870 19.999999999999982
19.999999999999982 19.999999999999982
[26] 19.999999999999982 19.999999999999982 21.985706303744429
19.999999999999982 19.999999999999982
[31] 20.000000000000028 19.999999999999982


Best,
Ani
On Wed, Apr 6, 2022 at 1:47 PM Richard M. Heiberger <rmh at temple.edu> wrote:
#
Excellent.
So Ani's answer to a==20 is different than Jim's answer. Jim's answer has some TRUE and some FALSE while Ani's answer is all FALSE. 
I get Jim's answer.

That is the problem, but I don't know how to solve it.
What is str(a)?
Are there any error messages?
What if you close R and restart? Just to be complete, try turning the power off on your computer and restarting the entire system.

Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of ani jaya
Sent: Wednesday, April 6, 2022 12:18 AM
To: Jim Lemon <drjimlemon at gmail.com>
Cc: r-help <r-help at r-project.org>
Subject: Re: [R] replacing a value

[External Email]

Hello Jim,

Thank you for the try. I am following your step and still have the problem.
But I know for sure now that my R/system is somewhat "broken" or I do not know for sure.
[1] 20.00000 20.00000 14.23756 20.00000 20.00000 20.00000 16.30921
20.00000 20.00000 20.00000
[11] 20.00000 20.00000 20.00000 20.00000 20.00000 14.85909 16.17894
20.00000 20.00000 20.00000
[21] 20.00000 27.64041 20.00000 20.00000 20.00000 20.00000 20.00000
21.98571 20.00000 20.00000
[31] 20.00000 20.00000
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [17] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[1] 20.00000 20.00000 14.23756 20.00000 20.00000 20.00000 16.30921
20.00000 20.00000 20.00000
[11] 20.00000 20.00000 20.00000 20.00000 20.00000 14.85909 16.17894
20.00000 20.00000 20.00000
[21] 20.00000 27.64041 20.00000 20.00000 20.00000 20.00000 20.00000
21.98571 20.00000 20.00000
[31] 20.00000 20.00000
On Wed, Apr 6, 2022 at 12:16 PM Jim Lemon <drjimlemon at gmail.com> wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=TACLY8J-wKY5Wmh5GANOzGxKC-MKX-FtV6O1IhiR_evXimgtxfQs8N6uI_OjKech&s=d3cMYTtmc68ekAgzW4KSEq4vJ5NbfzxlNi5yLvWIgK0&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=TACLY8J-wKY5Wmh5GANOzGxKC-MKX-FtV6O1IhiR_evXimgtxfQs8N6uI_OjKech&s=ZD6N8cPoUe4KKw4XqS0wC1YvfUzA_EnM0cZ3CYfr4GI&e=
and provide commented, minimal, self-contained, reproducible code.
#
But if I type a==b, all I get is TRUE. Is this behavior somewhat system dependent?

a <- c(20, 20, 14.2375646029948, 19.9999999999999, 20, 20, 16.3092078677214, 20, 20, 20, 20, 20, 20, 20, 20, 14.8590932408795, 16.178935255298, 20, 20, 20, 20, 27.6404077886079, 20, 20, 20, 20, 20, 21.9857063037444, 20, 20, 20, 20)
b <- c(19.999999999999982, 19.999999999999982, 14.237564602994826,
      19.999999999999940, 19.999999999999982,
      19.999999999999982, 16.309207867721351, 19.999999999999982,
      19.999999999999982, 19.999999999999982,
      19.999999999999982, 19.999999999999982, 19.999999999999982,
      19.999999999999982, 19.999999999999982,
      14.859093240879485, 16.178935255298033, 19.999999999999982,
      19.999999999999982, 20.000000000000028,
      19.999999999999982, 27.640407788607870, 19.999999999999982,
      19.999999999999982, 19.999999999999982,
      19.999999999999982, 19.999999999999982, 21.985706303744429,
      19.999999999999982, 19.999999999999982,
      20.000000000000028, 19.999999999999982)
a==b


Tim

-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of ani jaya
Sent: Wednesday, April 6, 2022 12:58 AM
To: Richard M. Heiberger <rmh at temple.edu>; Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
Cc: r-help <r-help at r-project.org>
Subject: Re: [R] [External] replacing a value

[External Email]

Dear All,

I will remain sipping the seawater until it changes its taste if this community does not exist.
Thank you.
[1] 19.999999999999982 19.999999999999982 14.237564602994826
19.999999999999940 19.999999999999982
[6] 19.999999999999982 16.309207867721351 19.999999999999982
19.999999999999982 19.999999999999982
[11] 19.999999999999982 19.999999999999982 19.999999999999982
19.999999999999982 19.999999999999982
[16] 14.859093240879485 16.178935255298033 19.999999999999982
19.999999999999982 20.000000000000028
[21] 19.999999999999982 27.640407788607870 19.999999999999982
19.999999999999982 19.999999999999982
[26] 19.999999999999982 19.999999999999982 21.985706303744429
19.999999999999982 19.999999999999982
[31] 20.000000000000028 19.999999999999982


Best,
Ani
On Wed, Apr 6, 2022 at 1:47 PM Richard M. Heiberger <rmh at temple.edu> wrote:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=A4GRP2o-ZyuqJvgr9XsPfuhujKPAAdoUZhMkJ0fR1UAVnUMRFNJmIaHS7V1aotNE&s=fuF4ytG9jvStgygMp00bmdHVcovgKvbojK3bSFVJ6-E&e=
PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=A4GRP2o-ZyuqJvgr9XsPfuhujKPAAdoUZhMkJ0fR1UAVnUMRFNJmIaHS7V1aotNE&s=y6027ELiLvmRy2xbH_A8sxKnEIsXNvUOwv7934md2tA&e=
and provide commented, minimal, self-contained, reproducible code.
#
The confusion comes from dput() not distinguishing between numbers that are
quite close to each other.  E.g.,
[1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE
FALSE FALSE
[1]  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00  0.000000e+00
 0.000000e+00
 [7]  0.000000e+00  0.000000e+00 -3.552714e-15  0.000000e+00  0.000000e+00
 0.000000e+00
c(20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20)

You have to use dput's control="digits17" or "exact" to avoid this rounding:
c(20, 20, 20, 20, 20, 20, 20, 20, 20, 19.999999999999996,
19.999999999999996,
19.999999999999996, 19.999999999999996)
c(0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4,
0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.3ffffffffffffp+4, 0x1.3ffffffffffffp+4,
0x1.3ffffffffffffp+4, 0x1.3ffffffffffffp+4)

The solution to your original problem is to specify a small range of values
around 20
that you wish to change to zero.  E.g.,
  a[ abs(a-20) < 20*.Machine$double.eps ] <- 0

-Bill
On Tue, Apr 5, 2022 at 7:48 PM ani jaya <gaaauul at gmail.com> wrote:

            

  
  
#
Hello,

The problem seems to be solved, btu maybe my answer to this thread [1] 
can be of help.

Using Bill's dput'ed data


equals <- function(x, y, tol = .Machine$double.eps^0.5) abs(x - y) < tol

x <- c(0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.4p+4,
        0x1.4p+4, 0x1.4p+4, 0x1.4p+4, 0x1.3ffffffffffffp+4, 
0x1.3ffffffffffffp+4,
        0x1.3ffffffffffffp+4, 0x1.3ffffffffffffp+4)

x == 20
#>  [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE 
FALSE FALSE
#> [13] FALSE
equals(x, 20)
#>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE



[1] https://stat.ethz.ch/pipermail/r-help/2012-September/322938.html


Hope this helps,

Rui Barradas

?s 05:58 de 06/04/2022, ani jaya escreveu: