diff --git a/concepts/set-operations/.meta/config.json b/concepts/set-operations/.meta/config.json new file mode 100644 index 00000000..d9226c05 --- /dev/null +++ b/concepts/set-operations/.meta/config.json @@ -0,0 +1,5 @@ +{ + "authors": ["colinleach"], + "contributors": [], + "blurb": "Core R provides several functions that perform set-like operations on vectors." +} diff --git a/concepts/set-operations/about.md b/concepts/set-operations/about.md new file mode 100644 index 00000000..8c37efa9 --- /dev/null +++ b/concepts/set-operations/about.md @@ -0,0 +1,39 @@ +# Introduction + +R has no separate Set datatype, instead using a variety of functions to perform similar operations on vectors. + +We have already seen `%in%` to test for set membership: + +```R +2 %in% 1:10 # TRUE +12 %in% 1:10 # FALSE +``` + +Relevant functions include `unique` to remove duplicates, plus `union()`, `intersect()` and `setdiff()` to operate on pairs of sets. + +```R +> set_1 <- c("a", "b", "c", "b", "a") +> unique(set_1) # deduplicate +[1] "a" "b" "c" + +> set_2 <- c('a', "c", "d") +> union(set_1, set_2) # values in either set +[1] "a" "b" "c" "d" + +> intersect(set_1, set_2) # values in both sets +[1] "a" "c" + +> setdiff(set_1, set_2) # values in set_1 but not set_2 +[1] "b" + +> setdiff(set_2, set_1) +[1] "d" +``` + +## The `hashtable` package + +The vector operations described above are part of core R. +These are useful for small problems but become slow for large sets. + +The `hashtable` package provides implementations of `hashmap` and `hashset` which scale better. +Like most external packages, this is not available within Exercism, but is worth investigating for real-world problems. diff --git a/concepts/set-operations/introduction.md b/concepts/set-operations/introduction.md new file mode 100644 index 00000000..6b59684a --- /dev/null +++ b/concepts/set-operations/introduction.md @@ -0,0 +1,13 @@ +# Introduction + +R has no separate Set datatype, instead using a variety of functions to perform similar operations on vectors. + +We have already seen `%in%` to test for set membership: + +```R +2 %in% 1:10 # TRUE +12 %in% 1:10 # FALSE +``` + +Relevant functions include `unique` (to remove duplicates), `union()`, `intersect()` and `setdiff()` to operate on pairs of sets. +Details are available online. diff --git a/concepts/set-operations/links.json b/concepts/set-operations/links.json new file mode 100644 index 00000000..24e5d70f --- /dev/null +++ b/concepts/set-operations/links.json @@ -0,0 +1,6 @@ +[ + { + "url": "https://search.r-project.org/CRAN/refmans/generics/html/setops.html", + "description": "Set Operations" + } +] diff --git a/config.json b/config.json index b51aafcc..2f64bcec 100644 --- a/config.json +++ b/config.json @@ -582,6 +582,11 @@ "slug": "basics", "name": "Basics" }, + { + "uuid": "85db3d8c-dfec-424c-9682-caa8611db8f8", + "slug": "set-operations", + "name": "Set Operations" + }, { "uuid": "2751b6f2-7d71-4397-b063-9bf927a57756", "slug": "booleans",