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

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Wed Apr 6 22:50:55 CEST 2022


Hi

safer way with NA values is using which

 my_data[which(my_subset_criteria == T)]
[1] 3

Cheers
Petr

AFAIK it is intended.

________________________________________
Od: R-help <r-help-bounces using r-project.org> za uživatele Kelly Thompson <kt1572757 using gmail.com>
Odesláno: 6. dubna 2022 22:13
Komu: r-help using r-project.org
Předmět: [R] What is the intended behavior, when subsetting using brackets [ ], when the subset criterion has NA's?

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 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.
Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/



More information about the R-help mailing list