Re: [R] What is the intended behavior, when subsetting using brackets [ ], when the subset criterion has NA's?

2022-04-06 Thread Jeff Newmiller
I suspect you were the victim of your own failure to use plain text email 
format as the Posting Guide warns you to do, or at least you let your email 
client try to convert your reply to HTML format... which often treats text on 
different lines as part of a single paragraph. As David pointed out, your 
version of the code had missing newlines.

On April 6, 2022 4:17:21 PM PDT, "Ebert,Timothy Aaron"  wrote:
>Based on the OP's submission I pasted the following into R and tried to run it.
>
>Here is what I think was posted (comments removed):
>
>my_data <- 1:5
>my_data
>my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria
>equals TRUE my_data[my_subset_criteria == T]
>my_subset_criteria == T) 
>
>Assuming that this is the right code I tried to run it. 
>The third line " my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria" 
>returns an error.
>
>Error: unexpected symbol in " my_subset_criteria <- c( F, F, T, NA, NA) 
>my_subset_criteria"
>
>Regards,
>Tim
>
>-Original Message-
>From: Jeff Newmiller  
>Sent: Wednesday, April 6, 2022 5:22 PM
>To: r-help@r-project.org; Ebert,Timothy Aaron ; Kelly Thompson 
>; r-help@r-project.org
>Subject: Re: [R] What is the intended behavior, when subsetting using brackets 
>[ ], when the subset criterion has NA's?
>
>[External Email]
>
>Mmmm, ESP on the blink.
>
>WHAT error? from which line of code?
>
>BTW see The R Inferno, section 8.2.32
>
>On April 6, 2022 1:52:47 PM PDT, "Ebert,Timothy Aaron"  wrote:
>>I get an error with this:
>>my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria
>>
>>
>>Tim
>>
>>-Original Message-
>>From: R-help  On Behalf Of Kelly Thompson
>>Sent: Wednesday, April 6, 2022 4:13 PM
>>To: r-help@r-project.org
>>Subject: [R] What is the intended behavior, when subsetting using brackets [ 
>>], when the subset criterion has NA's?
>>
>>[External Email]
>>
>>I noticed that I get different results when subsetting using subset, compared 
>>to subsetting using  "brackets" when the subset criteria have NA's.
>>
>>Here's an example
>>
>>#START OF EXAMPLE
>>my_data <- 1:5
>>my_data
>>
>>my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria
>>
>>#subsetting using subset returns the data where my_subset_criteria 
>>equals TRUE my_data[my_subset_criteria == T]
>>
>>#subsetting using brackets returns the data where my_subset_criteria 
>>equals TRUE, and also NA where my_subset_criteria is NA subset(my_data, 
>>my_subset_criteria == T)
>>
>>#END OF EXAMPLE
>>
>>This behavior is also mentioned here
>>https://urldefense.proofpoint.com/v2/url?u=https-3A__statisticaloddsand
>>ends.wordpress.com_2018_10_07_subsetting-2Din-2Dthe-2Dpresence-2Dof-2Dn
>>as_=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=NoPFkG
>>0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW=5lgkxT5A_M
>>SfElILNk1ZM3RGpcBWpMBu713av1DH1mk=
>>
>>Q. Is this the intended behavior when subsetting with brackets?
>>
>>Thank you!
>>
>>__
>>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>>https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
>>an_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRz
>>sn7AkP-g=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQD
>>MSEW=g9IzSC3WrXPLYjys_RdYSmgUoFFjsbwRJZZodqtDRa0=
>>PLEASE do read the posting guide 
>>https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
>>_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsR
>>zsn7AkP-g=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQ
>>DMSEW=uy6rCSNVehGynLn3ZCpLp_r2gHhoGcya4dbRe-tqQRc=
>>and provide commented, minimal, self-contained, reproducible code.
>>
>>__
>>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 
>>https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
>>an_listinfo_r-2Dhelp=DwIFaQ=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRz
>>sn7AkP-g=QLbQb6DaELgtR0Qo6aQx51ea30OmGIC0oqVB_naWxmKYlQyw_N81MiAKj_5Z
>>9TXC=G7rw7ZKvhE_HMF4z5cA5Gsr0C018E51Ov9ye3Ui2GX0=
>>PLEASE do read the posting guide 
>>https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
>>_posting-2Dguide.html=DwIFaQ=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsR
>>zsn7AkP-g=QLbQb6DaELgtR0Qo6aQx51ea30OmGIC0oqVB_naWxmKYlQyw_N81MiAKj_5
>>Z9TXC=PKD9VZ1n0p_NfoQ2OvbmPEVr14lVwLZx09mOIWPRZmk=
>>and provide commented, minimal, self-contained, reproducible code.
>
>--
>Sent from my phone. Please excuse my brevity.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] What is the intended behavior, when subsetting using brackets [ ], when the subset criterion has NA's?

2022-04-06 Thread David Winsemius



On 4/6/22 16:17, Ebert,Timothy Aaron wrote:

Based on the OP's submission I pasted the following into R and tried to run it.

Here is what I think was posted (comments removed):

my_data <- 1:5
my_data
my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria
equals TRUE my_data[my_subset_criteria == T]
my_subset_criteria == T)

Assuming that this is the right code I tried to run it.
The third line " my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria" 
returns an error.



As it should. You put two commands on the same line with no correct 
separator:Either of these should succeed:


my_subset_criteria <- c( F, F, T, NA, NA)
my_subset_criteria

# OR
 
my_subset_criteria <- c( F, F, T, NA, NA) ; my_subset_criteria


(The original poster had the correct syntax.)

--
David




Error: unexpected symbol in " my_subset_criteria <- c( F, F, T, NA, NA) 
my_subset_criteria"

Regards,
Tim

-Original Message-
From: Jeff Newmiller 
Sent: Wednesday, April 6, 2022 5:22 PM
To: r-help@r-project.org; Ebert,Timothy Aaron ; Kelly Thompson 
; r-help@r-project.org
Subject: Re: [R] What is the intended behavior, when subsetting using brackets 
[ ], when the subset criterion has NA's?

[External Email]

Mmmm, ESP on the blink.

WHAT error? from which line of code?

BTW see The R Inferno, section 8.2.32

On April 6, 2022 1:52:47 PM PDT, "Ebert,Timothy Aaron"  wrote:

I get an error with this:
my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria


Tim

-Original Message-
From: R-help  On Behalf Of Kelly Thompson
Sent: Wednesday, April 6, 2022 4:13 PM
To: r-help@r-project.org
Subject: [R] What is the intended behavior, when subsetting using brackets [ ], 
when the subset criterion has NA's?

[External Email]

I noticed that I get different results when subsetting using subset, compared to 
subsetting using  "brackets" when the subset criteria have NA's.

Here's an example

#START OF EXAMPLE
my_data <- 1:5
my_data

my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria

#subsetting using subset returns the data where my_subset_criteria
equals TRUE my_data[my_subset_criteria == T]

#subsetting using brackets returns the data where my_subset_criteria
equals TRUE, and also NA where my_subset_criteria is NA subset(my_data,
my_subset_criteria == T)

#END OF EXAMPLE

This behavior is also mentioned here
https://urldefense.proofpoint.com/v2/url?u=https-3A__statisticaloddsand
ends.wordpress.com_2018_10_07_subsetting-2Din-2Dthe-2Dpresence-2Dof-2Dn
as_=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=NoPFkG
0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW=5lgkxT5A_M
SfElILNk1ZM3RGpcBWpMBu713av1DH1mk=

Q. Is this the intended behavior when subsetting with brackets?

Thank you!

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
an_listinfo_r-2Dhelp=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRz
sn7AkP-g=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQD
MSEW=g9IzSC3WrXPLYjys_RdYSmgUoFFjsbwRJZZodqtDRa0=
PLEASE do read the posting guide
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsR
zsn7AkP-g=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQ
DMSEW=uy6rCSNVehGynLn3ZCpLp_r2gHhoGcya4dbRe-tqQRc=
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailm
an_listinfo_r-2Dhelp=DwIFaQ=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRz
sn7AkP-g=QLbQb6DaELgtR0Qo6aQx51ea30OmGIC0oqVB_naWxmKYlQyw_N81MiAKj_5Z
9TXC=G7rw7ZKvhE_HMF4z5cA5Gsr0C018E51Ov9ye3Ui2GX0=
PLEASE do read the posting guide
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
_posting-2Dguide.html=DwIFaQ=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsR
zsn7AkP-g=QLbQb6DaELgtR0Qo6aQx51ea30OmGIC0oqVB_naWxmKYlQyw_N81MiAKj_5
Z9TXC=PKD9VZ1n0p_NfoQ2OvbmPEVr14lVwLZx09mOIWPRZmk=
and provide commented, minimal, self-contained, reproducible code.

--
Sent from my phone. Please excuse my brevity.
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] What is the intended behavior, when subsetting using brackets [ ], when the subset criterion has NA's?

2022-04-06 Thread Ebert,Timothy Aaron
I get an error with this:
my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria


Tim

-Original Message-
From: R-help  On Behalf Of Kelly Thompson
Sent: Wednesday, April 6, 2022 4:13 PM
To: r-help@r-project.org
Subject: [R] What is the intended behavior, when subsetting using brackets [ ], 
when the subset criterion has NA's?

[External Email]

I noticed that I get different results when subsetting using subset, compared 
to subsetting using  "brackets" when the subset criteria have NA's.

Here's an example

#START OF EXAMPLE
my_data <- 1:5
my_data

my_subset_criteria <- c( F, F, T, NA, NA) my_subset_criteria

#subsetting using subset returns the data where my_subset_criteria equals TRUE 
my_data[my_subset_criteria == T]

#subsetting using brackets returns the data where my_subset_criteria equals 
TRUE, and also NA where my_subset_criteria is NA subset(my_data, 
my_subset_criteria == T)

#END OF EXAMPLE

This behavior is also mentioned here
https://urldefense.proofpoint.com/v2/url?u=https-3A__statisticaloddsandends.wordpress.com_2018_10_07_subsetting-2Din-2Dthe-2Dpresence-2Dof-2Dnas_=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW=5lgkxT5A_MSfElILNk1ZM3RGpcBWpMBu713av1DH1mk=

Q. Is this the intended behavior when subsetting with brackets?

Thank you!

__
R-help@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=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW=g9IzSC3WrXPLYjys_RdYSmgUoFFjsbwRJZZodqtDRa0=
PLEASE do read the posting guide 
https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_posting-2Dguide.html=DwICAg=sJ6xIWYx-zLMB3EPkvcnVg=9PEhQh2kVeAsRzsn7AkP-g=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW=uy6rCSNVehGynLn3ZCpLp_r2gHhoGcya4dbRe-tqQRc=
and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] What is the intended behavior, when subsetting using brackets [ ], when the subset criterion has NA's?

2022-04-06 Thread Kelly Thompson
I noticed that I get different results when subsetting using subset,
compared to subsetting using  "brackets" when the subset criteria have
NA's.

Here's an example

#START OF EXAMPLE
my_data <- 1:5
my_data

my_subset_criteria <- c( F, F, T, NA, NA)
my_subset_criteria

#subsetting using subset returns the data where my_subset_criteria equals TRUE
my_data[my_subset_criteria == T]

#subsetting using brackets returns the data where my_subset_criteria
equals TRUE, and also NA where my_subset_criteria is NA
subset(my_data, my_subset_criteria == T)

#END OF EXAMPLE

This behavior is also mentioned here
https://statisticaloddsandends.wordpress.com/2018/10/07/subsetting-in-the-presence-of-nas/

Q. Is this the intended behavior when subsetting with brackets?

Thank you!

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] [External] replacing a value

2022-04-06 Thread Rui Barradas

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.3p+4, 
0x1.3p+4,

   0x1.3p+4, 0x1.3p+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:

Dear All,

I will remain sipping the seawater until it changes its taste if this
community does not exist.
Thank you.


print(a,digit=18)

[1] 19.982 19.982 14.237564602994826
19.940 19.982
[6] 19.982 16.309207867721351 19.982
19.982 19.982
[11] 19.982 19.982 19.982
19.982 19.982
[16] 14.859093240879485 16.178935255298033 19.982
19.982 20.028
[21] 19.982 27.640407788607870 19.982
19.982 19.982
[26] 19.982 19.982 21.985706303744429
19.982 19.982
[31] 20.028 19.982


Best,
Ani

On Wed, Apr 6, 2022 at 1:47 PM Richard M. Heiberger  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.


a <- c(20, 20, 14.2375646029948, 19.9, 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)

a[1:6]

[1] 20.0 20.0 14.23756 20.0 20.0 20.0

print(a[1:6], digits=17)

[1] 20.000 20.000 14.237564602994800 19.901 
20.000 20.000

a[1:6] == 20

[1]  TRUE  TRUE FALSE FALSE  TRUE  TRUE

a[1:6] - 20

[1]  0.00e+00  0.00e+00 -5.762435e+00 -9.947598e-14  0.00e+00  
0.00e+00

a[4] == 20

[1] FALSE

round(a[1:6], digits=14) - 20

[1]  0.00e+00  0.00e+00 -5.762435e+00 -9.947598e-14  0.00e+00  
0.00e+00

round(a[1:6], digits=13) - 20

[1]  0.00e+00  0.00e+00 -5.762435e+00 -9.947598e-14  0.00e+00  
0.00e+00

round(a[1:6], digits=12) - 20

[1]  0.00  0.00 -5.762435  0.00  0.00  0.00

b <- a[1:6]
b[round(b,digits=12) == 20] <- 0
b

[1]  0.0  0.0 14.23756  0.0  0.0  0.0





On Apr 06, 2022, at 00:18, ani jaya  wrote:


c(20, 20, 14.2375646029948, 19.9, 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)




__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] I need help with this

2022-04-06 Thread Kimmo Elo
Hi!

No question. No idea, what you want to achieve. So do not be surprised
that we cannot give you any help.

Kimmo

ti, 2022-04-05 kello 21:59 -0400, ihejirika christopher via R-help
kirjoitti:
> install.packages("titanic")
> library(titanic)
> 
> training_set <- titanic_train#Saves the train set as a variable so
> that we do not affect the original data
> testing_set <- titanic_test #Saves the test set as a variable so that
> we do not affect the original data
> names(training_set) #Prints the name of each catergory
> 
> Sent from Mail for Windows
> 
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] I need help with this

2022-04-06 Thread ihejirika christopher via R-help
install.packages("titanic")
library(titanic)

training_set <- titanic_train#Saves the train set as a variable so that we do 
not affect the original data
testing_set <- titanic_test #Saves the test set as a variable so that we do not 
affect the original data
names(training_set) #Prints the name of each catergory

Sent from Mail for Windows


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.