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

Fabio D'Agostino d@go@t|no|@b| @end|ng |rom gm@||@com
Thu Apr 7 00:58:36 CEST 2022


Hi Kelly,
I had a question very similar to your months ago and Jeff replied this
https://stat.ethz.ch/pipermail/r-help/2022-February/473861.html
I hope this helps
Fabio


Il giorno mer 6 apr 2022 alle ore 22:23 Kelly Thompson
<kt1572757 using gmail.com> ha scritto:
>
> 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.



More information about the R-help mailing list