@@ -621,6 +621,7 @@ cats <- na.omit(cats)
621621#+end_src
622622
623623** Subsetting data
624+ *** COMMENT Subsetting and Data Structures are linked to Vectorization; regularize the three
624625*** Subset by index
625626#+BEGIN_SRC R
626627v <- 1:5
@@ -778,19 +779,6 @@ See /scripts/curriculum.Rmd
778779 }
779780 #+END_SRC
780781
781- 5. Vectorize your tests
782- #+BEGIN_SRC R
783- x <- 1:4
784-
785- if (any(x < 2)) {
786- print("Some x less than 2")
787- }
788-
789- if (all(x < 2)){
790- print("All x less than 2")
791- }
792- #+END_SRC
793-
794782*** Review Subsetting section
795783Subsetting is frequently an alternative to if-else statements in R
796784
@@ -844,22 +832,35 @@ x + z
844832 all(a)
845833 #+end_src
846834
847- 2. Can you detect missing data?
848- #+begin_src R
849- nan_vec <- c(1, 3, NaN)
835+ 2. Vectorize your tests
836+ #+BEGIN_SRC R
837+ x <- 1:4
850838
851- ## Which elements are NaN?
852- is.nan(nan_vec)
839+ if (any(x < 2)) {
840+ print("Some x less than 2")
841+ }
853842
854- ## Which elements are not NaN?
855- !is.nan(nan_vec)
843+ if (all(x < 2)){
844+ print("All x less than 2")
845+ }
846+ #+END_SRC
856847
857- ## Are any elements NaN?
858- any(is.nan(nan_vec))
848+ 3. Can you detect missing data?
849+ #+begin_src R
850+ nan_vec <- c(1, 3, NaN)
859851
860- ## Are all elements NaN?
861- all(is.nan(nan_vec))
862- #+end_src
852+ ## Which elements are NaN?
853+ is.nan(nan_vec)
854+
855+ ## Which elements are not NaN?
856+ !is.nan(nan_vec)
857+
858+ ## Are any elements NaN?
859+ any(is.nan(nan_vec))
860+
861+ ## Are all elements NaN?
862+ all(is.nan(nan_vec))
863+ #+end_src
863864
864865*** Matrix operations are also element-wise by default
865866#+begin_src R
0 commit comments