Wednesday, 1 August 2018

Data Management : Sequences in R Language

Sequences

The regular sequences can be generated in R.

Syntax :-

seq ( )

seq (from = 1, to = 1, by = ( ( to - from) / (length.out - 1) ) , length.out = NULL, along.with = NULL, ....)

Examples:

> seq (10)
   [1]  1  2  3  4  5  6  7  8  9  10

is the same as
> seq (1 : 10)



Assignment of an index-vector

> x <- c (9,8,7,6)
> ind <- seq (along = x)
> ind
   [1]  1 2 3 4


Accessing a value in the vector through index vector
→ Accessing an element of an index-vector
> x [ind [2] ]
   [1] 8 


Generating sequence of dates

Generating current time and date

Sys.time ( )  command provides the current time and date from the computer system.
> Sys.time ( )
  [1]  "2017-01-01  09:17:01  IST"

Sys.Date ( ) command provides the current date from the computer system.
> Sys.Date ( )
    [1]  "2017-01-01"

Usage
seq (from, to, by, length.out = NULL, along.with = NULL, ...)

Arguments
from             starting date (Required)
to                  end date (Optional)
by                 Increment of the sequence.  "day" , "week" , "month" , "quarter" or "year".
length.out     Integer, optional. Desired length of the sequence.
along.with    take the length from the length of this argument.

Sequence of first day of years

> seq (as.Date ("2010-01-01") , as.Date ("2017-01-01"), by = "Years")
[1] "2010-01-01"  "2011-01-01"  "2012-01-01"  "2013-01-01"
[5] "2014-01-01"  "2015-01-01"  "2016-01-01"  "2017-01-01"


Sequence of days

> seq (as.Date ("2017-01-01") , by = "days", length = 6)


Sequence of months

> seq (as.Date ("2017-01-01") , by = "days", length = 6)



Sequence of years

> seq (as.Date ("2017-01-01") , by = "years", length = 6)



To find sequence with defining start and end dates

0 Comments:

Post a Comment

Popular Posts

Categories

AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (893) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses