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

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Wed Apr 6 22:52:47 CEST 2022


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__statisticaloddsandends.wordpress.com_2018_10_07_subsetting-2Din-2Dthe-2Dpresence-2Dof-2Dnas_&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW&s=5lgkxT5A_MSfElILNk1ZM3RGpcBWpMBu713av1DH1mk&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_mailman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAsRzsn7AkP-g&m=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW&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=9PEhQh2kVeAsRzsn7AkP-g&m=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW&s=uy6rCSNVehGynLn3ZCpLp_r2gHhoGcya4dbRe-tqQRc&e=
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list