Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want to have a user-defined function that will return the first integer of a subset of a vector so that the values of the subset are unique, increasing by 1, and of a detailed length.

For example, using the input data:

v <- c(3, 4, 5, 6, 15, 16, 25, 26, 27)

l <- 3

The feasible subset of the vector would be:

c(3, 4, 5)

c(4, 5, 6)

c(25, 26, 27)

and I try to get the function to choose one of these at random and return the first/lowest number, which is either 3, 4, or 25.

1 Answer

0 votes
by (107k points)

I can suggest you use cgwtools::seqle as this is like rle() but you can define the desired increment in a run. Something like seqle(x, incr = 0,..) is the same as rle(x)

After that, just grab the run lengths and the starting values from the result.

If you want to know more about R then do refer to the R programming course.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Apr 12, 2020 in R Programming by ashely (50.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...