64071

How to create a combined sequence of constant length but starting at different values in R?

Question:

If I have a vector:

c(17,18,19)

And I want to get

c(17:17+5, 18:18+5, 19:19+5)

Or in other words:

c(17, 18, 19, 20, 21, 22, 18, 19, 20, 21, 22, 23, 19, 20, 21, 22, 23, 24)

How would I accomplish this in one line? Perhaps an essential R function I am missing? This can be done by sapply I am sure, but wondering if there was a non-iteration method.

Answer1:

c(outer(0:5, x, `+`))

or

rep(x, each = 6) + rep(0:5, 3)

Answer2:

There are probably a few easier ways, but here's an mapply method.

> x <- c(17,18,19) > c(mapply(seq, from = x, to = x + 5)) # [1] 17 18 19 20 21 22 18 19 20 21 22 23 19 20 21 22 23 24

Or even quicker

> c(mapply(`:`, from = x, to = x + 5))

mapply is basically a <em>multi-apply</em>, for applying a function to multiple vector or list arguments.

The following actually proved slightly faster than mapply

> c(sapply(x, function(y) `:`(y, y+5)))

Recommend

  • H2O-R: Apply custom library function on each row of H2OFrame
  • How to take a word and create an indicator variable based on the word's presence in comments?
  • How can I transform an array of characters with a few lines of code to a data.frame?
  • Get Distinct rows from a result of JOIN in SQL Server
  • How do I conditionally select a field from one of two tables?
  • Changing Machine Type on Google Cloud
  • Data Type of Columns in a List - R
  • Exception handling as per java coding standards
  • wpf: update multiple controls via dispatcher
  • Remove characters after a specific character in column
  • R Impute NA's by Linear Increase Depending on Time Interval
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Whats the right place for testhelper-classes? (phpunit/best practise)
  • Converting datatype Char to Nvarchar
  • Is mp4 stream able with ffserver?
  • Insert records if not exist SQL Server 2005
  • Iterate twice through a DataReader
  • SQL query to group by maximal sets of a column having inner consecutive distances below a threshold
  • What is this strange character in chrome's resource css viewer?
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • Getting unread count in Sent Folder using Google Apps Script - GMail
  • Can my PDF ping my server when it is opened?
  • JSON encode and decode on PHP
  • Two Tables Serving as one Model in Rails
  • Basic many-to-many left join query
  • Approximate Order-Preserving Huffman Code
  • calculate gradient output for Theta update rule
  • Calling Worksheet functions from vba in foreign language versions of Excel
  • MongoDb aggregation
  • Update CALayer sublayers immediately
  • Trying to switch camera back to front but getting exception
  • SVN: Merging two branches together
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • PHP: When would you need the self:: keyword?
  • Python: how to group similar lists together in a list of lists?
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • Understanding cpu registers
  • Does armcc optimizes non-volatile variables with -O0?
  • Programmatically clearing map cache