goodSegment <- function(badNumbers, l, r) { # Cast input as integers badNumbers <- as.integer(badNumbers) l <- as.integer(l) r <- as.integer(r) # Throw errors if any constraints are not met stopifnot(length(l) == 1 & length(r) == 1) stopifnot(length(badNumbers) < 1e5 | length(badNumbers) == 0) stopifnot(min(badNumbers) > 0 | max(badNumbers) < 1e9) stopifnot(length(badNumbers) == length(unique(badNumbers))) stopifnot(r > l) stopifnot(r < 1e9 & l > 0) # Find longest run using run-length encoding badNumbers <- badNumbers[badNumbers >= l & badNumbers <= r] runs <- rle(tabulate((l:r)[-badNumbers])) return<-max(runs$lengths[runs$values == 1]) } getval=goodSegment(badNumbers = c(8,6,20,12), l = 1, r = 30) print(getval) #goodSegment(badNumbers = c(99, 140, 400, 424, 484, 493, 555, 564, 583, 634, 861, 994, 1035, 1127, 1270, 1309, 1328, 1363, 1427, 1731, 1739, 1795, 1840, 1870, 1930, 2095, 2111, 2159, 2206, 2238, 2243, 2390, 2450, 2553, 2569, 2606, 2931, 2962, 2988, 3135, 3157, 3233, 3369, 3401, 3558, 3573, 3587, 3610, 3681, 3747, 3765, 3809, 3812, 3818, 3873, 3996, 4021, 4081, 4090, 4125, 4255, 4329, 4351, 4447, 4496, 4674, 4688, 4799, 4850, 4967, 5068, 5102, 5147, 5157, 5246, 5247, 5281, 5316, 5318, 5358, 5362, 5501, 5530, 5611, 5638, 5686, 5724, 5842, 5922, 5999, 6090, 6223, 6240, 6296, 6372, 6410, 6520, 6617, 6684, 6796, 6805, 6896, 6988, 6999, 7074, 7130, 7233, 7277, 7443, 7482, 7512, 7549, 7567, 7568, 7572, 7630, 7651, 8050, 8082, 8149, 8216, 8298, 8376, 8452, 8502, 8528, 8769, 8783, 8802, 8854, 8970, 9273, 9297, 9304, 9306, 9378, 9402, 9510, 9515, 9585, 9605, 9606, 9695, 9759, 9773, 9813, 9899, 9916, 9920, 10055, 10065, 10068, 10135, 10269, 10313, 10345, 10371, 10482, 10539, 10805, 10831, 10966, 10977, 11014, 11069, 11177, 11252, 11413, 11447, 11448, 11482, 11566, 11669, 11800, 11826, 11901, 12003, 12120, 12195, 12378, 12398, 12431, 12453, 12460, 12473, 12503, 12755, 12890, 12923, 12941, 12986, 13039, 13107, 13109, 13141, 13189, 13338, 13369, 13378, 13490, 13544, 13547, 13608, 13615, 13635, 13770, 13772, 13868, 14008, 14127, 14220, 14238, 14272, 14405, 14488, 14508, 14571, 14587, 14599, 14614, 14660, 14697, 14736, 14800, 14818, 14892, 14904, 14909, 14952, 15207, 15223, 15231, 15237, 15279, 15367, 15515, 15529, 15532, 15562, 15596, 15613, 15620, 15670, 15681, 15689, 15827, 15862, 15864, 16056, 16131, 16230, 16282, 16301, 16383, 16541, 16618, 16640, 16776, 17108, 17176, 17202, 17349, 17471, 17559, 17605, 17721, 17762, 17984, 18037, 18062, 18132, 18153, 18180, 18328, 18361, 18404, 18410, 18631, 18667, 18787, 18874, 19005, 19032, 19041, 19049, 19316, 19376, 19488, 19550, 19642, 19969, 20084, 20300, 20329, 20343, 20362, 20369, 20418, 20475, 20502, 20585, 20742, 20770, 20988, 21030, 21039, 21123, 21164, 21193, 21197, 21214, 21271, 21378, 21461, 21590, 21697, 21891, 22060, 22129, 22165, 22168, 22179, 22225, 22345, 22383, 22406, 22438, 22450, 22452, 22459, 22572, 22579, 22645, 22730, 22761, 22816, 22837, 23163, 23373, 23517, 23642, 23654, 23724, 23746, 23949, 23986, 24174, 24199, 24201, 24314, 24363, 24412, 24440, 24482, 24488, 24808, 24816, 24904, 24906, 25161, 25204, 25329, 25375, 25433, 25458, 25471, 25552, 25557, 25758, 25763, 25777, 25938, 26046, 26086, 26091, 26128, 26208, 26287, 26443, 26452, 26552, 26571, 26599, 26695, 26696, 26702, 26720, 26724, 26757, 26763, 26812, 26818, 26829, 26875, 26975, 27029, 27043, 27125, 27161, 27213, 27218, 27264, 27354, 27374, 27515, 27584, 27627, 27756, 27772, 27820, 27883, 27931, 27947, 28025, 28131, 28203, 28388, 28420, 28527, 28563, 28602, 28662, 28663, 28729, 28740, 28785, 28794, 28819, 28878, 28889, 28947, 28991, 29112, 29123, 29147, 29165, 29173, 29271, 29568, 29626, 29669, 29940, 30074, 30092, 30186, 30390, 30408, 30424, 30467, 30507, 30569, 30681, 30832, 30880, 30959, 30995, 31054, 31217, 31299, 31342, 31392, 31419, 31423, 31486, 31572, 31713, 31725, 31742, 31804, 31866, 31877, 31894, 32067, 32109, 32110, 32338, 32391, 32398, 32458, 32477, 32485, 32507, 32531, 32612, 32755, 32773, 32889, 32893, 33034, 33202, 33244, 33265, 33289, 33355, 33359, 33387, 33404, 33528, 33648, 33765, 33942, 34064, 34149, 34154, 34231, 34234, 34348, 34464, 34602, 34625, 34726, 34832, 35015, 35221, 35301, 35377, 35544, 35600, 35629, 35793, 35824, 35920, 35946, 36086, 36184, 36357, 36445, 36622, 36702, 36706, 36756, 36774, 36780, 36910, 37004, 37084, 37091, 37102, 37188, 37231, 37259, 37263, 37426, 37486, 37500, 37524, 37715, 37846, 37989, 38009, 38130, 38223, 38238, 38267, 38319, 38356, 38507, 38560, 38571, 38721, 38794, 38855, 39032, 39074, 39183, 39241, 39295, 39516, 39583, 39633, 39720, 39772, 39851, 39885, 39890, 39996, 40041, 40109, 40209, 40248, 40532, 40549, 40596, 40644, 40710, 40813, 40994, 41005, 41118, 41204, 41220, 41377, 41503, 41555, 41594, 41664, 41696, 41803, 42023, 42035, 42045, 42053, 42135, 42167, 42170, 42234, 42315, 42559, 42562, 42736, 42758, 42770, 42826, 42841, 42887, 42926, 42979, 42991, 43022, 43127, 43188, 43256, 43460, 43560, 43579, 43598, 43615, 43953, 44215, 44343, 44372, 44398, 44497, 44706, 44952, 44989, 45217, 45381, 45428, 45639, 45727, 45783, 45878, 45926, 45944, 45950, 45971, 46000, 46032, 46145, 46195, 46207, 46246, 46343, 46408, 46515, 46557, 46612, 46637, 46711, 46747, 46791, 46844, 46921, 46930, 46959, 47131, 47226, 47228, 47371, 47443, 47465, 47466, 47606, 47682, 47728, 47990, 48031, 48248, 48281, 48290, 48384, 48496, 48529, 48541, 48636, 48641, 48706, 48745, 48757, 48869, 48882, 48932, 49041, 49260, 49313, 49374, 49459, 49516, 49560, 49615, 49669, 49692, 49766, 49898, 50041, 50077, 50164, 50222, 50238, 50284, 50356, 50369, 50568, 50590, 50616, 50667, 50720, 50732, 50789, 50841, 50854, 50884, 50937, 50994, 51111, 51146, 51167, 51183, 51311, 51317, 51506, 51577, 51660, 51671, 51801, 51876, 52047, 52297, 52448, 52542, 52566, 52655, 52736, 52939, 52976, 53028, 53105, 53174, 53199, 53201, 53274, 53316, 53329, 53332, 53447, 53455, 53496, 53692, 53813, 53905, 54012, 54101, 54290, 54552, 54556, 54678, 54753, 54801, 54808, 54831, 54926, 54932, 54963, 55010, 55155, 55166, 55207, 55251, 55389, 55500, 55571, 55633, 55766, 55768, 55840, 55868, 55934, 55974, 55979, 56154, 56157, 56189, 56212, 56246, 56294, 56315, 56521, 56590, 56593, 56621, 56664, 56719, 56722, 56902, 56916, 56962, 57049, 57051, 57132, 57196, 57263, 57395, 57408, 57433, 57457, 57498, 57549, 57552, 57561, 57615, 57618, 57625, 57639, 57649, 57656, 57703, 57706, 57791, 57862, 57873, 57958, 58086, 58359, 58506, 58581, 58666, 58708, 58812, 58847, 59007, 59018, 59034, 59147, 59230, 59277, 59285, 59359, 59409, 59412, 59414, 59521, 59523, 59532, 59861, 59876, 59922, 59979, 60061, 60128, 60156, 60275, 60332, 60365, 60436, 60629, 60669, 60696, 60817, 60955, 61032, 61175, 61194, 61205, 61238, 61259, 61279, 61422, 61564, 61704, 61767, 61804, 61830, 61854, 61929, 61970, 62024, 62115, 62171, 62217, 62241, 62350, 62419, 62632, 62804, 62862, 62936, 63131, 63174, 63276, 63302, 63533, 63672, 63754, 63847, 63893, 63991, 64052, 64128, 64236, 64366, 64418, 64435, 64549, 64559, 64786, 64858, 64927, 65212, 65252, 65349, 65644, 65927, 65997, 66239, 66267, 66367, 66373, 66461, 66633, 66763, 66800, 66828, 66922, 66929, 67026, 67059, 67071, 67141, 67169, 67269, 67270, 67322, 67401, 67454, 67465, 67538, 67616, 67668, 67725, 67804, 67823, 67887, 68026, 68141, 68156, 68197, 68257, 68284, 68407, 68445, 68477, 68603, 68611, 68663, 68731, 68833, 68904, 68916, 68930, 69118, 69277, 69298, 69411, 69432, 69448, 69510, 69518, 69525, 69586, 69745, 69825, 69843, 69941, 69950, 70050, 70182, 70247, 70332, 70367, 70418, 70578, 70666, 70727, 70772, 70885, 70903, 70954, 70957, 70994, 71061, 71073, 71085, 71292, 71437, 71466, 71480, 71516, 71521, 71612, 71635, 71655, 71746, 71753, 71764, 71835, 71995, 71999, 72029, 72149, 72151, 72239, 72383, 72400, 72413, 72463, 72469, 72613, 72641, 72695, 72731, 72801, 72985, 73131, 73141, 73195, 73218, 73221, 73309, 73316, 73509, 73616, 73695, 73813, 73974, 74017, 74160, 74228, 74230, 74232, 74462, 74494, 74533, 74543, 74622, 74657, 74679, 74862, 74878, 74994, 75051, 75142, 75147, 75201, 75210, 75356, 75391, 75470, 75574, 75620, 75621, 75727, 75807, 75829, 76039, 76057, 76067, 76123, 76311, 76352, 76380, 76509, 76515, 76536, 76547, 76596, 76606, 76627, 77026, 77039, 77109, 77181, 77199, 77244, 77259, 77264, 77321, 77396, 77460, 77503, 77547, 77683, 77737, 78033, 78076, 78236, 78239, 78279, 78334, 78448, 78486, 78540, 78625, 78733, 78763, 78818, 78890, 78936, 78954, 78976, 78991, 79029, 79032, 79105, 79137, 79196, 79255, 79257, 79270, 79338, 79341, 79346, 79382, 79403, 79606, 79750, 79800, 79846, 79896, 79902, 79995, 80028, 80036, 80074, 80229, 80272, 80387, 80410, 80437, 80479, 80483, 80573, 80650, 80764, 80793, 80803, 80872, 80990, 81084, 81110, 81138, 81157, 81256, 81273, 81501, 81529, 81702, 81704, 81718, 81812, 81813, 81873, 81905, 81972, 82039, 82156, 82191, 82405, 82531, 82545, 82562, 82926, 83040, 83081, 83097, 83179, 83199, 83315, 83323, 83408, 83437, 83440, 83477, 83658, 83756, 83812, 83849, 83854, 83932, 84109, 84123, 84146, 84155, 84325, 84349, 84400, 84430, 84434, 84506, 84591, 84787, 84825, 84910, 85001, 85058, 85071, 85294, 85342, 85443, 85654, 85886, 86094, 86208, 86335, 86353, 86456, 86471, 86474, 86492, 86519, 86604, 86651, 86784, 86886, 86981, 87038, 87128, 87147, 87162, 87349, 87374, 87496, 87501, 87571, 87662, 87669, 87738, 87761, 87801, 87805, 87851, 87859, 87871, 88014, 88075, 88117, 88180, 88368, 88658, 88703, 88792, 88800, 88815, 88820, 88842, 88932, 88960, 88966, 88991, 89020, 89027, 89068, 89149, 89305, 89364, 89387, 89410, 89458, 89485, 89492, 89504, 89514, 89573, 89633, 89736, 89747, 89886, 89912, 90018, 90092, 90103, 90187, 90297, 90312, 90421, 90422, 90429, 90514, 90601, 90666, 90752, 90804, 90880, 90913, 90952, 91033, 91380, 91435, 91462, 91503, 91656, 91657, 91661, 91697, 91713, 91733, 91811, 91913, 91948, 92011, 92446, 92463, 92465, 92552, 92652, 92666, 92757, 92785, 92813, 92859, 92946, 92968, 93074, 93253, 93301, 93414, 93659, 93767, 93853, 93880, 94075, 94084, 94098, 94136, 94189, 94282, 94322, 94513, 94528, 94575, 94793, 94856, 94894, 95082, 95277, 95341, 95499, 95642, 95656, 95791, 95825, 95939, 95951, 95959, 95981, 96011, 96024, 96026, 96035, 96141, 96143, 96157, 96211, 96293, 96331, 96415, 96582, 96630, 96689, 96709, 96713, 96780, 96858, 96982, 97102, 97364, 97462, 97538, 97589, 97654, 97738, 97775, 97844, 97913, 97928, 98122, 98201, 98340, 98791, 98848, 98939, 98945, 99034, 99084, 99123, 99255, 99285, 99378, 99442, 99569, 99757, 99945), l = 13671, r = 55878)
Write, Run & Share R Language code online using OneCompiler's R Language online compiler for free. It's one of the robust, feature-rich online compilers for R language, running on the latest version 3.4. Getting started with the OneCompiler's R Language compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as R
and start coding.
R is very popular for data analytics which was created by Ross Ihaka and Robert Gentleman in 1993. Many big companies like Google, Facebook, Airbnb etc uses this language for data analytics. R is good for software developers, statisticians and data miners.
Data type | Description | Usage |
---|---|---|
Numeric | To represent decimal values | x=1.84 |
Integer | To represent integer values, L tells to store the value as integer | x=10L |
Complex | To represent complex values | x = 10+2i |
Logical | To represent boolean values, true or false | x = TRUE |
Character | To represent string values | x <- "One compiler" |
raw | Holds raw bytes |
Variables can be assigned using any of the leftward, rightward or equal to operator. You can print the variables using either print or cat functions.
var-name = value
var-name <- value
value -> var-name
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
#code
}
if(conditional-expression){
#code if condition is true
} else {
#code if condition is false
}
if(condition-expression1) {
#code if above condition is true
} elseif(condition-expression2){
#code if above condition is true
}
elseif(condition-expression3) {
#code if above condition is true
}
...
else {
#code if all the conditions are false
}
Switch is used to execute one set of statement from multiple conditions.
switch(expression, case-1, case-2, case-3....)
For loop is used to iterate a set of statements based on a condition.
for (value in vector) {
# code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
# code
}
Repeat is used tyo iterate a set of statements with out any condition. You can write a user-defined condition to exit from the loop using IF
.
repeat {
#code
if(condition-expression) {
break
}
}
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
func-name <- function(parameter_1, parameter_2, ...) {
#code for function body
}
function_name (parameters)
Vector is a basic data strucre where sequence of data values share same data type.
For example, the below statement assigns 1 to 10 values to x.
You can also use se() function to create vectors.
x <- 1:10
#using seq() function
x <- seq(1, 10, by=2)
the above statement prints the output as [1] 1 3 5 7 9
.