How to create sets using vector values in R?

R ProgrammingServer Side ProgrammingProgramming

A set in mathematics is defined as the collection of unique elements and the order of the elements does not matter. In R, we can create sets using set_power function of sets package. For example, if we have a vector x that contains A, B, C then the sets using the vector x can be created by using set_power(x).

Loading sets package −

library(sets)

Examples

x1<−c("A","B","C")
set_power(x1)
{{}, {"A"}, {"B"}, {"C"}, {"A", "B"}, {"A", "C"}, {"B", "C"}, {"A",
"B", "C"}}
x2<−LETTERS[1:5]
x2
[1] "A" "B" "C" "D" "E"
set_power(x2)
{{}, {"A"}, {"B"}, {"C"}, {"D"}, {"E"}, {"A", "B"}, {"A", "C"}, {"A",
"D"}, {"A", "E"}, {"B", "C"}, {"B", "D"}, {"B", "E"}, {"C", "D"},
{"C", "E"}, {"D", "E"}, {"A", "B", "C"}, {"A", "B", "D"}, {"A", "B",
"E"}, {"A", "C", "D"}, {"A", "C", "E"}, {"A", "D", "E"}, {"B", "C",
"D"}, {"B", "C", "E"}, {"B", "D", "E"}, {"C", "D", "E"}, {"A", "B",
"C", "D"}, {"A", "B", "C", "E"}, {"A", "B", "D", "E"}, {"A", "C", "D",
"E"}, {"B", "C", "D", "E"}, {"A", "B", "C", "D", "E"}}
x3<−letters[1:6]
x3
[1] "a" "b" "c" "d" "e" "f"
set_power(x3)
{{}, {"a"}, {"b"}, {"c"}, {"d"}, {"e"}, {"f"}, {"a", "b"}, {"a", "c"},
{"a", "d"}, {"a", "e"}, {"a", "f"}, {"b", "c"}, {"b", "d"}, {"b",
"e"}, {"b", "f"}, {"c", "d"}, {"c", "e"}, {"c", "f"}, {"d", "e"},
{"d", "f"}, {"e", "f"}, {"a", "b", "c"}, {"a", "b", "d"}, {"a", "b",
"e"}, {"a", "b", "f"}, {"a", "c", "d"}, {"a", "c", "e"}, {"a", "c",
"f"}, {"a", "d", "e"}, {"a", "d", "f"}, {"a", "e", "f"}, {"b", "c",
"d"}, {"b", "c", "e"}, {"b", "c", "f"}, {"b", "d", "e"}, {"b", "d",
"f"}, {"b", "e", "f"}, {"c", "d", "e"}, {"c", "d", "f"}, {"c", "e",
"f"}, {"d", "e", "f"}, {"a", "b", "c", "d"}, {"a", "b", "c", "e"},
{"a", "b", "c", "f"}, {"a", "b", "d", "e"}, {"a", "b", "d", "f"},
{"a", "b", "e", "f"}, {"a", "c", "d", "e"}, {"a", "c", "d", "f"},
{"a", "c", "e", "f"}, {"a", "d", "e", "f"}, {"b", "c", "d", "e"},
{"b", "c", "d", "f"}, {"b", "c", "e", "f"}, {"b", "d", "e", "f"},
{"c", "d", "e", "f"}, {"a", "b", "c", "d", "e"}, {"a", "b", "c", "d",
"f"}, {"a", "b", "c", "e", "f"}, {"a", "b", "d", "e", "f"}, {"a", "c",
"d", "e", "f"}, {"b", "c", "d", "e", "f"}, <<set(6)>>}
x4<−1:5
x4
[1] 1 2 3 4 5
set_power(x4)
{{}, {1L}, {2L}, {3L}, {4L}, {5L}, {1L, 2L}, {1L, 3L}, {1L, 4L}, {1L,
5L}, {2L, 3L}, {2L, 4L}, {2L, 5L}, {3L, 4L}, {3L, 5L}, {4L, 5L}, {1L,
2L, 3L}, {1L, 2L, 4L}, {1L, 2L, 5L}, {1L, 3L, 4L}, {1L, 3L, 5L}, {1L,
4L, 5L}, {2L, 3L, 4L}, {2L, 3L, 5L}, {2L, 4L, 5L}, {3L, 4L, 5L}, {1L,
2L, 3L, 4L}, {1L, 2L, 3L, 5L}, {1L, 2L, 4L, 5L}, {1L, 3L, 4L, 5L},
{2L, 3L, 4L, 5L}, {1L, 2L, 3L, 4L, 5L}}
x5<−sample(c(0,1),6,replace=TRUE)
x5
[1] 1 0 1 1 0 0
set_power(x5)
{{}, {0}, {1}, {0, 1}}
x6<−sample(0:9,6,replace=TRUE)
x6
[1] 7 0 2 4 8 6
set_power(x6)
{{}, {0L}, {2L}, {4L}, {6L}, {7L}, {8L}, {0L, 2L}, {0L, 4L}, {0L, 6L},
{0L, 7L}, {0L, 8L}, {2L, 4L}, {2L, 6L}, {2L, 7L}, {2L, 8L}, {4L, 6L},
{4L, 7L}, {4L, 8L}, {6L, 7L}, {6L, 8L}, {7L, 8L}, {0L, 2L, 4L}, {0L,
2L, 6L}, {0L, 2L, 7L}, {0L, 2L, 8L}, {0L, 4L, 6L}, {0L, 4L, 7L}, {0L,
4L, 8L}, {0L, 6L, 7L}, {0L, 6L, 8L}, {0L, 7L, 8L}, {2L, 4L, 6L}, {2L,
4L, 7L}, {2L, 4L, 8L}, {2L, 6L, 7L}, {2L, 6L, 8L}, {2L, 7L, 8L}, {4L,
6L, 7L}, {4L, 6L, 8L}, {4L, 7L, 8L}, {6L, 7L, 8L}, {0L, 2L, 4L, 6L},
{0L, 2L, 4L, 7L}, {0L, 2L, 4L, 8L}, {0L, 2L, 6L, 7L}, {0L, 2L, 6L,
8L}, {0L, 2L, 7L, 8L}, {0L, 4L, 6L, 7L}, {0L, 4L, 6L, 8L}, {0L, 4L,
7L, 8L}, {0L, 6L, 7L, 8L}, {2L, 4L, 6L, 7L}, {2L, 4L, 6L, 8L}, {2L,
4L, 7L, 8L}, {2L, 6L, 7L, 8L}, {4L, 6L, 7L, 8L}, {0L, 2L, 4L, 6L, 7L},
{0L, 2L, 4L, 6L, 8L}, {0L, 2L, 4L, 7L, 8L}, {0L, 2L, 6L, 7L, 8L}, {0L,
4L, 6L, 7L, 8L}, {2L, 4L, 6L, 7L, 8L}, <<set(6)>>}
x7<−sample(LETTERS[1:26],6,replace=TRUE)
x7
[1] "X" "C" "G" "W" "B" "K"
set_power(x7)
{{}, {"B"}, {"C"}, {"G"}, {"K"}, {"W"}, {"X"}, {"B", "C"}, {"B", "G"},
{"B", "K"}, {"B", "W"}, {"B", "X"}, {"C", "G"}, {"C", "K"}, {"C",
"W"}, {"C", "X"}, {"G", "K"}, {"G", "W"}, {"G", "X"}, {"K", "W"},
{"K", "X"}, {"W", "X"}, {"B", "C", "G"}, {"B", "C", "K"}, {"B", "C",
"W"}, {"B", "C", "X"}, {"B", "G", "K"}, {"B", "G", "W"}, {"B", "G",
"X"}, {"B", "K", "W"}, {"B", "K", "X"}, {"B", "W", "X"}, {"C", "G",
"K"}, {"C", "G", "W"}, {"C", "G", "X"}, {"C", "K", "W"}, {"C", "K",
"X"}, {"C", "W", "X"}, {"G", "K", "W"}, {"G", "K", "X"}, {"G", "W",
"X"}, {"K", "W", "X"}, {"B", "C", "G", "K"}, {"B", "C", "G", "W"},
{"B", "C", "G", "X"}, {"B", "C", "K", "W"}, {"B", "C", "K", "X"},
{"B", "C", "W", "X"}, {"B", "G", "K", "W"}, {"B", "G", "K", "X"},
{"B", "G", "W", "X"}, {"B", "K", "W", "X"}, {"C", "G", "K", "W"},
{"C", "G", "K", "X"}, {"C", "G", "W", "X"}, {"C", "K", "W", "X"},
{"G", "K", "W", "X"}, {"B", "C", "G", "K", "W"}, {"B", "C", "G", "K",
"X"}, {"B", "C", "G", "W", "X"}, {"B", "C", "K", "W", "X"}, {"B", "G",
"K", "W", "X"}, {"C", "G", "K", "W", "X"}, <<set(6)>>}
x8<−sample(rnorm(10,2,0.50),6,replace=TRUE)
x8
[1] 1.950736 1.394277 1.950736 2.179080 2.420796 1.957366
set_power(x8)
{{}, {1.394277}, {1.950736}, {1.957366}, {2.17908}, {2.420796},
{1.394277, 1.950736}, {1.394277, 1.957366}, {1.394277, 2.17908},
{1.394277, 2.420796}, {1.950736, 1.957366}, {1.950736, 2.17908},
{1.950736, 2.420796}, {1.957366, 2.17908}, {1.957366, 2.420796},
{2.17908, 2.420796}, {1.394277, 1.950736, 1.957366}, {1.394277,
1.950736, 2.17908}, {1.394277, 1.950736, 2.420796}, {1.394277,
1.957366, 2.17908}, {1.394277, 1.957366, 2.420796}, {1.394277,
2.17908, 2.420796}, {1.950736, 1.957366, 2.17908}, {1.950736,
1.957366, 2.420796}, {1.950736, 2.17908, 2.420796}, {1.957366,
2.17908, 2.420796}, {1.394277, 1.950736, 1.957366, 2.17908},
{1.394277, 1.950736, 1.957366, 2.420796}, {1.394277, 1.950736,
2.17908, 2.420796}, {1.394277, 1.957366, 2.17908, 2.420796},
{1.950736, 1.957366, 2.17908, 2.420796}, {1.394277, 1.950736,
1.957366, 2.17908, 2.420796}}
raja
Published on 06-Nov-2020 13:54:34
Advertisements