How to make a vector of two numbers in an inclusive sequence in R?

I have a vector (or matrix, etc.) with two numbers.

Is there a feature or other short β€œsingle line” trick to make these numbers an inclusive sequence?

For instance:

If I have a vector containing numbers 1 and 15. I want to quickly apply a function (or trick) to create a new vector containing 1:15 (1,2,3,4,5,6 ... 15)

I understand that I can use the seq function to create sequences, but this would require me to write both digits from my vector (or matrix, etc.) as "from" and "to".

  • In an example where indexing data.frames, lists, or matrices creates long lines of text, it would be nice not to repeat these lines of text for both of the / arguments.

Example:

 dat <- data.frame(a = 1:5, b = 1:5, c = 1:5, d = 1:5, e = 1:5, f = 1:5) #Too messy/long/time-consuming if my column names are longer/more complex: names(dat)[names(dat) %in% c('a','b','c','d','e')] #Too much coding text (I'm assuming it can be done simpler): names(dat)[seq(from = which(names(dat) == 'a'), to = which(names(dat) == 'e'))] #So how could I do something like the sequence example, but with less text? #perhaps using: which(names(dat) %in% c('a','e')) in some way...? 
+5
source share

All Articles