Using R to construct Pajek's temporal files

Date and time in R

In Pajek time points are represented by integers. In this note we describe how to convert dates into integers using R.

Besides the basic support for date and time ( help(“Date”) and help(“POSIXt”) ) R has also a package lubridate ( paper/PDF, CRAN ) for more user friendly work with dates and times.

For constructing Pajek network files the basic functions are enough.

> (da <- as.Date(ISOdate(2017,9,14)))
[1] "2017-09-14"
> as.numeric(da)
[1] 17423
> (db <- as.Date(as.character(20170914),"%Y%m%d"))
[1] "2017-09-14"
> as.numeric(db)
[1] 17423
> (dc <- as.Date("14.9.17","%d.%m.%y"))
[1] "2017-09-14"
> (de <- as.Date("14 Sep 2017","%d %b %Y"))
[1] "2017-09-14" 
> (df <- as.Date(17423,origin="1970-01-01"))
[1] "2017-09-14" 

For different options for specifying a date format see R wikibook.

Dates to Pajek

The default value of the origin of dates is “1970-01-01”. Assume that we would like to use in our Pajek network description a granularity by days with the origin January 1, 2017.

We first compute the index of January 1, 2017

> (d0 <- as.Date(ISOdate(2016,12,31)))
[1] "2016-12-31"
> as.numeric(d0)
[1] 17166

to be used in the function nDays(d) where d is a Date. The function nDays(d) returns the number of dates + 1 from January 1, 2017.

Another function nday2date(n) returns the Date corresponding to n-th day from January 1, 2017.

> nDays <- function(d) return(as.numeric(d)-17166)
> nday2date <- function(n) return(as.Date(n,origin="2016-12-31"))
> d <- as.Date(ISOdate(2017,9,14))
> nDays(d)
[1] 257
> (n <- nDays(d))
[1] 257
> nday2date(n)
[1] "2017-09-14"
> d <- as.Date(ISOdate(2017,1,1))
> (n <- nDays(d))
[1] 1
> nday2date(n)
[1] "2017-01-01"

Pajek mail

Request

Subject [Pajek] Pajek Temporal Netowk
From 	Mohsin Adalat
To 	pajek@list.fmf.uni-lj.si
Date 	2017/09/17 08:40

Hello Sir,
I have edge list like this. i want to create temporal network based on date. i have used different tools like txt2pajek3. However when i convert network into temporal, it gives error that vertex one does not have valid time record. Sir i want to create dynamic network based on dates of arcs. Is there any tool available to convert this type of data into temporal network.

a	b	10-10-2016
b	c	20-10-2016
d	e	30-10-2016
a	b	30-10-2016

Thank You.

Answer

Assume that we have a file dates.csv

from;to;date
a;b;10-10-2016
b;c;20-10-2016
d;e;30-10-2016
a;b;30-10-2016

using a short program in R (set your starting date in d0 and your directory in setwd() )

> d0 <- as.numeric(as.Date(ISOdate(2016,10,1)))-1
> nDays <- function(d) return(as.numeric(d)-d0)
> setwd("C:/Users/batagelj/***/Rnet/pajek")
> links <- read.csv2("dates.csv",encoding='UTF-8',colClasses="character")
> L <- levels(factor(c(links$from,links$to))); n <- length(L)
> F <- as.numeric(factor(links$from,levels=L))
> T <- as.numeric(factor(links$to,levels=L))
> net <- file("dates.net","w"); cat('*vertices ',n,'\n',file=net)
> for(v in 1:n) cat(v,' "',L[v],'"\n',sep='',file=net)
> cat('*arcs\n',file=net)
> for(a in 1:nrow(links)) cat(F[a],' ',T[a],' 1 [',
+   nDays(as.Date(links$date[a],"%d-%m-%Y")),']\n',sep='',file=net)
> close(net)

we get a Pajek's network file dates.net

*vertices  5 
1 "a"
2 "b"
3 "c"
4 "d"
5 "e"
*arcs
1 2 1 [10]
2 3 1 [20]
4 5 1 [30]
1 2 1 [30]

URLs

notes/net/rdat.txt · Last modified: 2017/09/17 11:19 by vlado
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki