Ebert,Timothy Aaron
2022-Apr-06 20:52 UTC
[R] What is the intended behavior, when subsetting using brackets [ ], when the subset criterion has NA's?
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 at r-project.org> On Behalf Of Kelly Thompson Sent: Wednesday, April 6, 2022 4:13 PM To: r-help at 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 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=NoPFkG0n9RFRaacmiiQ9Hp1cGniz9ED5YGN11-Jh6rD_zkTTE8e5egsKqzQDMSEW&s=g9IzSC3WrXPLYjys_RdYSmgUoFFjsbwRJZZodqtDRa0&ePLEASE 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&eand provide commented, minimal, self-contained, reproducible code.
Jeff Newmiller
2022-Apr-06 21:21 UTC
[R] What is the intended behavior, when subsetting using brackets [ ], when the subset criterion has NA's?
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 at 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 at r-project.org> On Behalf Of Kelly Thompson >Sent: Wednesday, April 6, 2022 4:13 PM >To: r-help at 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 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=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. > >______________________________________________ >R-help at 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.-- Sent from my phone. Please excuse my brevity.