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

David Winsemius dw|n@em|u@ @end|ng |rom comc@@t@net
Thu Apr 7 01:43:55 CEST 2022


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 <jdnewmil using dcn.davis.ca.us>
> Sent: Wednesday, April 6, 2022 5:22 PM
> To: r-help using r-project.org; Ebert,Timothy Aaron <tebert using ufl.edu>; Kelly Thompson <kt1572757 using gmail.com>; r-help using 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" <tebert using ufl.edu> 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 <r-help-bounces using r-project.org> On Behalf Of Kelly Thompson
>> Sent: Wednesday, April 6, 2022 4:13 PM
>> To: r-help using 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_&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=NoPFkG
>> 0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW&s=5lgkxT5A_M
>> SfElILNk1ZM3RGpcBWpMBu713av1DH1mk&e=
>>
>> Q. Is this the intended behavior when subsetting with brackets?
>>
>> Thank you!
>>
>> ______________________________________________
>> R-help using 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&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRz
>> sn7AkP-g&m=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQD
>> MSEW&s=g9IzSC3WrXPLYjys_RdYSmgUoFFjsbwRJZZodqtDRa0&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=9PEhQh2kVeAsR
>> zsn7AkP-g&m=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQ
>> DMSEW&s=uy6rCSNVehGynLn3ZCpLp_r2gHhoGcya4dbRe-tqQRc&e=
>> and provide commented, minimal, self-contained, reproducible code.
>>
>> ______________________________________________
>> R-help using 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&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRz
>> sn7AkP-g&m=QLbQb6DaELgtR0Qo6aQx51ea30OmGIC0oqVB_naWxmKYlQyw_N81MiAKj_5Z
>> 9TXC&s=G7rw7ZKvhE_HMF4z5cA5Gsr0C018E51Ov9ye3Ui2GX0&e=
>> PLEASE do read the posting guide
>> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org
>> _posting-2Dguide.html&d=DwIFaQ&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsR
>> zsn7AkP-g&m=QLbQb6DaELgtR0Qo6aQx51ea30OmGIC0oqVB_naWxmKYlQyw_N81MiAKj_5
>> Z9TXC&s=PKD9VZ1n0p_NfoQ2OvbmPEVr14lVwLZx09mOIWPRZmk&e=
>> and provide commented, minimal, self-contained, reproducible code.
> --
> Sent from my phone. Please excuse my brevity.
> ______________________________________________
> R-help using 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.



More information about the R-help mailing list