====== 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'' ( [[http://vita.had.co.nz/papers/lubridate.pdf|paper/PDF]], [[https://cran.r-project.org/web/packages/lubridate/|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 [[https://en.wikibooks.org/wiki/R_Programming/Times_and_Dates#Format|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 ===== * https://stackoverflow.com/questions/12976542/how-to-convert-in-both-directions-between-year-month-day-and-dates-in-r * https://www.r-bloggers.com/date-formats-in-r/ * http://mgimond.github.io/ES218/Week02c.html * http://daniellequinn.github.io/RLessons/FormattingDates/FormattingDates.html * https://stats.idre.ucla.edu/r/faq/how-can-i-format-a-string-containing-a-date-into-r-date-object/ * http://biostat.mc.vanderbilt.edu/wiki/pub/Main/ColeBeck/datestimes.pdf * https://campus.datacamp.com/courses/intermediate-r-for-finance/dates?ex=6 * http://www.aridhia.com/technical-tutorials/working-with-dates-in-r/ * https://www.rdocumentation.org/packages/base/versions/3.4.1/topics/as.Date * https://blog.exploratory.io/5-most-practically-useful-operations-when-working-with-date-and-time-in-r-9f9eb8a17465 * http://data.library.virginia.edu/working-with-dates-and-time-in-r-using-the-lubridate-package/ * http://www.cyclismo.org/tutorial/R/time.html * http://vita.had.co.nz/papers/lubridate.pdf * https://crmda.dept.ku.edu/guides/38.R_datetime/38.R_datetime.html * https://en.wikibooks.org/wiki/R_Programming/Times_and_Dates * http://yyao.info/r/2015/02/08/r-dates * http://www.noamross.net/blog/2014/2/10/using-times-and-dates-in-r---presentation-code.html * https://books.google.si/books?id=w9aGDQAAQBAJ&pg=PA72&lpg=PA72&dq=r+convert+year,+month,+day+to+date&source=bl&ots=uD0hfOhUPw&sig=RnwrwcI7Eoa_aMZa9e63k6Uc4ho&hl=en&sa=X&ved=0ahUKEwicwtTa2aPWAhUoL8AKHURjAfk4MhDoAQg6MAQ#v=onepage&q=r%20convert%20year%2C%20month%2C%20day%20to%20date&f=false