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

Kelly Thompson kt1572757 @end|ng |rom gm@||@com
Wed Apr 6 22:13:01 CEST 2022


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!



More information about the R-help mailing list