Sequences in R

R

Generating sequences in R is another easy way to get a series of numbers. Vectors - which we already covered in another article - can create a series of numbers. However, they're very simple sequences and only increment by one. If we need a series that increments by 0.5, 10, or even 50 we need to use the seq() function. Unlike a simple vector, with sequences we can specify the interval. Let's see numbers one through three, incrementing by 0.25:

> seq(to=1, from=3, by=0.25)
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00

> seq(1, 3, 0.25)
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00

Something really handy about seq() is that we can feed it to and from inputs, then specify length. The length option tells seq() how many values we want to see between to and from values. Let's use seq() to create a sequence of nine numbers between one and three:

> seq(1, 3, length=9)
[1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00

Sequences are a great alternative to vectors we we need more fine-grained control over the increment.

Tyler Hart

Tyler Hart is a networking and security professional who started working in technology in 2002 with the US DoD and moved to the private sector in 2010. He holds a Business degree in IT Management, as well as the CISSP certification and others from Microsoft, CompTIA, Cisco, (ISC)2, Tenable Network Security, and more. For over 15 years he has worked and consulted with large and small organizations including hospitals and clinics, ISPs and WISPs, U.S. Defense organizations, and state and county governments.

https://www.manitonetworks.com
Previous
Previous

Rolling Dice with R

Next
Next

Vectors in R